WITHLOCK(1)
π WITHLOCK β locking wrapper script π
okay bestie, gather round, because withlock(1) is about to become your new fave little sysadmin sidekick and I am so excited to tell you about her. she's a wrapper script, she's polite, she's tidy, and she will absolutely hold the door so your cron jobs stop piling up on each other like it's a Black Friday sale. π
β¨ NAME β¨
withlock β locking wrapper script. that's it, that's the whole vibe. π
π¦ SYNOPSIS π¦
withlock lockfile command [args...]
withlock [-w <seconds>|--wait=seconds] [-q|--quiet] [-v|--verbose]
lockfile command [args...]
withlock [-h|--help]
withlock [--version]
like. look at her. so simple. so clean. iconic. β¨
πΈ DESCRIPTION πΈ
so withlock(1) is a wrapper script whose whole entire job is making sure some program isn't run more than once. she is literally the ideal girlie to stop those periodic jobs spawned by cron(8) from stacking up on top of each other. no more seventeen copies of the same backup script fighting in the group chat. π
the way she does it is so clever: the locks she uses are only valid while the wrapper is running. which means she will never require extra cleanup β not even after a system crash, not even after a reboot. zero crumbs left behind. this is what makes her safe and easy to use, and honestly better than implementing some half-hearted locking situation inside your own scripts. no cap. π―
and because sometimes you're not in a rush? the script can wait a defined time for a lock to become "free". patient queen. π
π₯ SECURITY π₯
okay this part is important, bestie, listen up. π¨
lockfiles, generally, MUST NOT be placed in a publicly writable directory. why? because that would open the door to a symlink attack. and for that exact reason, withlock(1) simply disallows lockfiles in such locations. she said no thank you, we're not doing that. periodt. π
not me being dramatic about it, it's literally just true. publicly writable dir = no. she won't even let you. π«Ά
π OPTIONS π
-v,--verboseβ print debug messages tostderr. she'll narrate her whole little journey for you. gossipy in the best way. π-q,--quietβ if the lock can't be acquired immediately, silently quit without error. she just. leaves. no drama, no error, no nothing. ghosting, but politely and on purpose. π§--versionβ show the program's version number and exit. her little birth certificate. π-h,--helpβ show command synopsis and exit. the cheat sheet. π¦-w seconds,--wait=secondsβ wait for a maximum of seconds for the lock to be acquired. she'll hang out by the door for as long as you tell her to, and not one second more. β³
β EXAMPLES β
instead of running your command like this:
<command> [<args>...]
you simply use:
withlock <lockfile> <command> [<args>...]
and honestly? that's the whole trick. slay. β¨
cron(8) jobs β especially the long-running ones β are frequently run under withlock(1). here's an example:
-*/10 * * * * root withlock LOCK-serverstatus /usr/bin/log_server_status2
43 5,17 * * * mirror withlock LOCK-rsync-edu-isos rsync -rlptoH --no-motd rsync.opensuse-education.org::download/ISOs/ /srv/mirrors/opensuse-education/ISOs -hi
see how tidy?? one lockfile, one job, no stacking. she ate and left no crumbs. π₯
π HISTORY π
so the tea is: this wrapper got implemented because no comparable solution was found β even after looking around for a long time. and after suffering through problems caused by missing or insufficient locking for years. years, bestie. π
the only thing that came close was with-lock-ex.c, a C implementation written by Ian Jackson and placed in the public domain by him. with-lock-ex.c is traditionally available on Debian in a package named chiark-utils-bin. parts of withlock's locking strategy and parts of the usage semantics were inspired by that program. π«Ά
this implementation was chosen to be done in Python because it's universally available, easy to adapt, and doesn't require being compiled. no build step, no toolchain, just vibes and a working interpreter. β¨
then β plot twist β somebody pointed out that there's flock(1), a small tool written in C that does the same thing. and yes, the author had apparently managed to completely miss that tool during a decade's worth of Linux hacking (you discover something new every day, don't you?). the functionality is mainly the same indeed. π
however, withlock(1) has some advantages:
withlock(1)is a little easier and more logical to use thanflock(1). the semantics are more suited for sysadmins. πwithlock(1)always cleans up its lock files.flock(1)always lets them lie around. and honestly? the author admits they like that withwithlock(1)you can always tell by the presence of the lock files which jobs are running. little status indicators. we love a visible paper trail. πflock(1)doesn't prevent using unsafe directories. π«withlock(1)is easily extensible. π±- it could be used as a Python module (not implemented so far). π
flock(1)doesn't exist on Solaris and OSX. π
π BUGS π
withlock(1) has been tested on many platforms, including Debian, Ubuntu, openSUSE, CentOS, Fedora, FreeBSD, OSX, and OpenCSW Solaris β and she's been in production use since 2009. π
there don't seem to be race conditions (the author tried very hard to break it) β but there are no guarantees. so like, she's tough, she's been through a lot, but don't @ her if the universe decides to be weird. π
π AUTHOR π
this program was written by Peter PΓΆml peter@poeml.de in 2009. a legend, fr fr. π
π«Ά SEE ALSO π«Ά
flock(1)with-lock-ex(1)from Debian packagechiark-utils-bin
and that's the whole page, bestie!! π this was the withlock(1) manual, dated June 2015, and if you made it all the way down here you are officially a certified unix girlie. go lock something. responsibly. mwah. πβ¨π¦πΈπ
π¦ where this page comes from
- fedora 43: withlock 0.5-21.fc43 Β· Apache-2.0
- fedora 44: withlock 0.5-22.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms