LOCKFILE(1)
π LOCKFILE(1) β conditional semaphore-file creator, General Commands Manual edition β¨
πΈ NAME πΈ
lockfile β conditional semaphore-file creator. Thatβs the whole vibe: it makes semaphore files so your scripts can take turns like civilized little icons. π
β¨ SYNOPSIS β¨
lockfile -sleeptime | -r retries |
-l locktimeout | -s suspend | -! | -ml | -mu | filename ...
π οΈ DESCRIPTION π οΈ
okay so bestie, lockfile can be used to create one or more semaphore files. If lockfile canβt create all the specified files (in the specified order), it waits sleeptime (defaults to 8) seconds and retries the last file that didnβt succeed. You can specify the number of retries to do until failure is returned. If the number of retries is -1 (default, i.e., -r-1) lockfile will retry forever. No cap, forever forever. π
If the number of retries expires before all files have been created, lockfile returns failure and removes all the files it created up till that point. Cleanup queen behavior fr fr. π§Ή
Using lockfile as the condition of a loop in a shell script can be done easily by using the -! flag to invert the exit status. To prevent infinite loops, failures for any reason other than the lockfile already existing are not inverted to success but rather are still returned as failures. That distinction is lowkey important, so donβt sleep on it. π
All flags can be specified anywhere on the command line, they will be processed when encountered. The command line is simply parsed from left to right. Very βread the room, left to rightβ of it. π
flag lineup, bestie: π«Ά
-sleeptimeβ sets thesleeptimewait before retrying the last file that didnβt succeed; defaults to8seconds.-r retriesβ number of retries to do until failure is returned;-1(default, i.e.,-r-1) means retry forever. Retries arenβt reset for following files, but you can reset with-rnewretriesafter every file on the command line (see NOTES).-l locktimeoutβ if you specify alocktimeout, a lockfile gets removed by force afterlocktimeoutseconds have passed since the lockfile was last modified/created.lockfileis clock skew immune. β°-s suspendβ after a lockfile has been removed by force, a suspension ofsuspendseconds (defaults to16) is taken into account, so another program doesnβt inadvertently immediately remove any newly created lockfile (compareSUSPENDinprocmail(1)).-!β inverts the exit status; multiple-!flags will toggle the return status. Use it whenlockfileis the condition of a loop, not as a general βmake failure successβ button β see WARNINGS.-mlβ lock your system mailbox, if the permissions on the system mail spool directory allow it, or iflockfileis suitablysetgid.-muβ unlock your system mailbox, same permissions/setgid situation.-h,-?β display a command-line help page.-vβ display version information.-rnewretriesβ resets the retry count after every file on the command line; the exact wording is in NOTES, and yes itβs-rnewretries, no space, character for character. πfilename ...β one or more semaphore files to create, in the specified order.
All files created by lockfile will be read-only, and therefore will have to be removed with rm -f. Not rm, not rm -r, bestie: rm -f. π«‘
If you specify a locktimeout then a lockfile will be removed by force after locktimeout seconds have passed since the lockfile was last modified/created (most likely by some other program that unexpectedly died a long time ago, and hence could not clean up any leftover lockfiles). Lockfile is clock skew immune. After a lockfile has been removed by force, a suspension of suspend seconds (defaults to 16) is taken into account, in order to prevent the inadvertent immediate removal of any newly created lockfile by another program (compare SUSPEND in procmail(1)). π
π¬ Mailbox locks π¬
If the permissions on the system mail spool directory allow it, or if lockfile is suitably setgid, it will be able to lock and unlock your system mailbox by using the options -ml and -mu respectively. Mailbox main character energy, honestly. π
π EXAMPLES π
Suppose you want to make sure that access to the file "important" is serialised, i.e., no more than one program or shell script should be allowed to access it. For simplicityβs sake, letβs suppose that it is a shell script. In this case you could solve it like this:
...
lockfile important.lock
...
access_"important"_to_your_hearts_content
...
rm -f important.lock
...
Now if all the scripts that access "important" follow this guideline, you will be assured that at most one script will be executing between the lockfile and the rm commands. Thatβs the whole serotonin boost: mutual exclusion, but make it cute. β¨
π ENVIRONMENT π
LOGNAMEβ used as a hint to determine the invokerβs loginname.
π FILES π
/etc/passwdβ to verify and/or correct the invokerβs loginname (and to find out hisHOMEdirectory, if needed)./var/spool/mail/$LOGNAME.lockβ lockfile for the system mailbox; the environment variables present in here will not be taken from the environment, but will be determined by looking in/etc/passwd.
π SEE ALSO π
rm(1), mail(1), sendmail(8), procmail(1)
π DIAGNOSTICS π
Filename too long, ...β Use shorter filenames.Forced unlock denied on "x"β No write permission in the directory where lockfile"x"resides, or more than one lockfile trying to force a lock at exactly the same time.Forcing lock on "x"β Lockfile"x"is going to be removed by force because of a timeout (compareLOCKTIMEOUTinprocmail(1)).Out of memory, ...β The system is out of swap space.Signal received, ...β Lockfile will remove anything it created till now and terminate.Sorry, ...β The retries limit has been reached.Truncating "x" and retrying lockβ"x"does not seem to be a valid filename.Try praying, ...β Missing subdirectories or insufficient privileges.
π¦ BUGS π¦
Definitely less than one. Thatβs it. Thatβs the bug list. Iconic. β¨
π¨ WARNINGS π¨
The behavior of the -! flag, while useful, is not necessarily intuitive or consistent. When testing lockfileβs return value, shell script writers should consider carefully whether they want to use the -! flag, simply reverse the test, or do a switch on the exact exitcode. In general, the -! flag should only be used when lockfile is the conditional of a loop. This is your βread the warning before you vibeβ moment. π«Ά
π§ MISCELLANEOUS π§
Lockfile is NFS-resistant and eight-bit clean. Sheβs robust, sheβs clean, sheβs serving. π
π NOTES π
- Calling up
lockfilewith the-hor-?options will cause it to display a command-line help page. Calling it up with the-voption will cause it to display its version information. - Multiple
-!flags will toggle the return status. - Since flags can occur anywhere on the command line, any filename starting with a
-has to be preceded by./. - The number of retries will not be reset when any following file is being created (i.e., they are simply used up). It can, however, be reset by specifying
-rnewretriesafter every file on the command line. - Although files with any name can be used as lockfiles, it is common practice to use the extension
.lockto lock mailfolders (it is appended to the mailfolder name). In case one does not want to have to worry about too long filenames and does not have to conform to any other lockfilename convention, then an excellent way to generate a lockfilename corresponding to some already existing file is by taking the prefixlock.and appending the i-node number of the file which is to be locked.
π₯ SOURCE π₯
This program is part of the procmail mail-processing-package (v3.24) available at http://www.procmail.org/ or ftp.procmail.org in pub/procmail/.
π MAILINGLIST π
There exists a mailinglist for questions relating to any program in the procmail package:
<procmail-users@procmail.org>β for submitting questions/answers.<procmail-users-request@procmail.org>β for subscription requests.
If you would like to stay informed about new versions and official patches send a subscription request to procmail-announce-request@procmail.org (this is a readonly list).
π AUTHORS π
Stephen R. van den Berg <srb@cuci.nl>
xoxo, BuGless π β this has been LOCKFILE(1), from the General Commands Manual, procmail mail-processing-package v3.24 era, signed off with maximum semaphore-file sparkle π«Άβ¨
π¦ where this page comes from
- centos-stream 10: procmail 3.24-8.el10 Β· gpl-2.0-or-later
- fedora 43: procmail 3.24-9.fc43 Β· gpl-2.0-or-later
- fedora 44: procmail 3.24-10.fc44 Β· gpl-2.0-or-later
- nixos 26.05: procmail 3.24
- nixpkgs unstable: procmail 3.24
- opensuse-leap 16.0: procmail 3.24-160000.2.2 Β· Artistic-1.0 OR GPL-2.0-or-later
- opensuse-leap 16.0: procmail 3.24-160000.2.2 Β· Artistic-1.0 OR GPL-2.0-or-later
- opensuse-leap 16.0: procmail 3.24-160000.2.2 Β· Artistic-1.0 OR GPL-2.0-or-later
- opensuse-leap 16.0: procmail 3.24-160000.2.2 Β· Artistic-1.0 OR GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms