in reply to Cron Jobs That Run For Too Long
The first way is to have a lockfile that's always around. When your script starts, it tries to lock that file with flock; if it's already locked it exits, otherwise it does its work. The OS will automatically remove the lock when the process exits, even if it crashes or is killed.
The second way is to write the PID of the process into the lockfile. Then when another copy starts up, it checks if the lockfile exists, and if so it reads the PID and verifies that that PID is still running and is actually itself. If it finds that the process is no longer running, it just creates a new lock file and goes on. When the process finishes it removes the lockfile.
The third way is to get a list of all processes, and see if any of those are executing that script. If so, exit.
I generally favor the first of these, because it's easy and efficient.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Cron Jobs That Run For Too Long
by ruoso (Curate) on Dec 20, 2005 at 22:07 UTC | |
by adamk (Chaplain) on Dec 20, 2005 at 23:22 UTC | |
by LanX (Saint) on Apr 23, 2021 at 16:02 UTC | |
by Bod (Parson) on Apr 23, 2021 at 17:52 UTC | |
by LanX (Saint) on Apr 23, 2021 at 19:21 UTC | |
| |
by astroboy (Chaplain) on Dec 23, 2005 at 18:40 UTC | |
by Anonymous Monk on Jul 12, 2007 at 15:36 UTC | |
by marto (Cardinal) on Jul 12, 2007 at 15:46 UTC | |
by Anonymous Monk on Oct 21, 2016 at 19:08 UTC |