Cronjobs
July 9, 2008 12:48 AM   Subscribe

Cron Jobs - separate processes?

This seems a pretty simple question, but I somehow can't find any info on this: Is every job launched by a (Linux) cron daemon started as a new process? What happens in the (theoretical) case where a repeating tasks overlaps with its own next iteration?
posted by uncle harold to Computers & Internet (5 answers total) 3 users marked this as a favorite
 
Yes, they are separate processes, typically mere shell scripts. If the tasks overlap then you should decrease the frequency of the job.
posted by rhizome at 1:00 AM on July 9, 2008


Best answer: Yes they're all separate processes, typically scripts.

If your overlapping cronjob is a script (or something that can be wrapped in one), another way to deal with this is to write (`touch`) a lockfile to /var/run/mycronjob.lock and check for it at the beginning of the script. If the file is there, exit the script and it will run the next iteration. Be sure to delete the file after the original process is done!

Taking it further you can guard against crashed cronjobs leaving the lockfile behind by having the job write its PID to the lockfile above. If the PID is the same as the running script then it can delete it as its own when it's done. If the next (overlapping) process sees the file it can check the PID against its own and, if different, make sure the previous job is actually running. If the PID is there but the previous process isn't running then the current process can overwrite the file and continue on.
posted by rhizome at 1:08 AM on July 9, 2008 [1 favorite]


Response by poster: Thanks!
posted by uncle harold at 3:15 AM on July 9, 2008


The following script uses the semi-standard program 'lockfile' to manage the creation and removal of the lock file. The removal of the lock file is done when the script exits (including if it is forced to exit by 'kill' and a variety of signals--that's the magic of "trap"). It doesn't do comparison of process IDs like rhizome suggested above--if the lockfile doesn't get removed for some reason, then the cleanup must be done manually.
#!/bin/sh
set -xe
cd /home/jepler/emc2-docbuild
lockfile build.lock; trap "rm -f build.lock" 0 1 2 3 9 15
cvs -q up 2>&1
make -C src docs

posted by jepler at 5:00 AM on July 9, 2008


At least in Ubuntu, lockfile is a part of the procmail package.
posted by PueExMachina at 9:24 PM on July 10, 2008


« Older Can you recomend any Korean language learning...   |   Why does her colleague get paid more? Newer »
This thread is closed to new comments.