Da eine Socket-Lösung wird noch nicht erwähnt worden ist es beachtenswert, dass Steckdosen als wirksamer mutexes verwendet werden. Socket-Erstellung ist eine atomare Operation, wie mkdir
ist wie Gunstick hingewiesen, so dass eine Steckdose als Schloss oder Mutex verwendet werden kann.
Tim Kay's Perl-Skript 'Solo' ist ein sehr kleines und effektives Skript, um sicherzustellen, dass nur eine Kopie eines Skripts gleichzeitig ausgeführt werden kann. Es wurde speziell für die Verwendung mit Cron-Jobs entwickelt, obwohl es auch für andere Aufgaben perfekt funktioniert, und ich habe es sehr effektiv für Non-Crob-Jobs verwendet.
Solo hat einen Vorteil gegenüber den anderen bisher genannten Techniken, da die Überprüfung außerhalb des Skripts erfolgt, von dem Sie nur eine Kopie ausführen möchten. Wenn das Skript bereits ausgeführt wird, wird nie eine zweite Instanz dieses Skripts gestartet. Dies ist im Gegensatz zum Isolieren eines Codeblocks innerhalb des Skripts, der durch eine Sperre geschützt ist. BEARBEITEN: Wenn flock
in einem Cron-Job und nicht innerhalb eines Skripts verwendet wird, können Sie damit auch verhindern, dass eine zweite Instanz des Skripts gestartet wird - siehe Beispiel unten.
Hier ist ein Beispiel dafür, wie Sie es mit cron verwenden könnte:
*/5 * * * * solo -port=3801 /path/to/script.sh args args args
# "/path/to/script.sh args args args" is only called if no other instance of
# "/path/to/script.sh" is running, or more accurately if the socket on port 3801
# is not open. Distinct port numbers can be used for different programs so that
# if script_1.sh is running it does not prevent script_2.sh from starting, I've
# used the port range 3801 to 3810 without conflicts. For Linux non-root users
# the valid port range is 1024 to 65535 (0 to 1023 are reserved for root).
* * * * * solo -port=3802 /path/to/script_1.sh
* * * * * solo -port=3803 /path/to/script_2.sh
# Flock can also be used in cron jobs with a distinct lock path for different
# programs, in the example below script_3.sh will only be started if the one
# started a minute earlier has already finished.
* * * * * flock -n /tmp/path.to.lock -c /path/to/script_3.sh
Links:
Hoffnung das hilft.
möglich Duplikat of [Schnell und sicher, dass nur eine Instanz eines Shell-Skripts gleichzeitig ausgeführt wird] (http://stackoverflow.com/questions/185451/quick-and-dirtyway-to-ensure-only- one-of-shell-script-is-running-at) – user