Categories
Posts

lockrun

Two years ago I mentioned Solo, which uses TCP/IP port binding to prevent a program from running more than once.

For those that don’t want to use up ports or would rather stick to file locks for other reasons you can accomplish the same thing with lockrun.

An example cron entry using lockrun:

*/5 * * * * /usr/local/bin/lockrun –lockfile=/var/run/cacti.lockrun — /usr/local/bin/cron-cacti-poller

IMPORTANT: Be sure to place the lockfile in a directory that only that user has the rights to, and not in /tmp (as this Tech Tip has previously suggested). If a different user has the ability to write to the directory, s/he could create a symbolic link from the target lockfile to some system file that uses locks the same way, and get the cron job to foul up the locking.
The file used, /var/run/cacti.lockrun, is created (if necessary), the lock acquired, and closed when finished. At no time does lockrun perform any file I/O: the file exists only to be the subject of locking requests.

Overall I like the Solo approach better, but it might not be a good option in every situation. In those cases lockrun looks like a good alternative.

Leave a Reply

Your email address will not be published. Required fields are marked *