Benutzer-Werkzeuge

Webseiten-Werkzeuge


commands-shell-snippets

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

commands-shell-snippets [2018/02/04 17:36] (aktuell)
alex angelegt
Zeile 1: Zeile 1:
 +====== Sammlung nützlicher Befehle ======
  
 +===== Shell Script Snippets =====
 +
 +=== allow only one instance of a script running – use lock file with “noclobber” option ===
 +<code bash>
 +#!/bin/sh
 +LOCKFILE="/​tmp/​mutex-script.lock"​
 +## noclobber prevents that an existing file gets overwritten when using the ">"​ operator
 +if /bin/sh -c "set -o noclobber; echo \"​$$\"​ > \"​$LOCKFILE\"";​ then
 +    echo "​aquired $LOCKFILE"​
 +    trap "rm -f \"​$LOCKFILE\";​ echo \"​released $LOCKFILE\";​ exit \$?" INT TERM EXIT KILL
 +else
 +    echo "could not aquire $LOCKFILE"​
 +    exit 1
 +fi
 +echo "DO STUFF"
 +sleep 20
 +echo "​DONE"​
 +</​code>​
 +
 +=== allow only one instance of a script running – use lock directory ===
 +<code bash>
 +#!/bin/sh
 +LOCKDIR="/​tmp/​mutex-script.lock"​
 +## "​mkdir"​ is atomic
 +if mkdir "​$LOCKDIR";​ then
 +    echo "​aquired $LOCKDIR"​
 +    trap "rmdir \"​$LOCKDIR\";​ echo \"​released $LOCKDIR\";​ exit \$?" INT TERM EXIT KILL
 +else
 +    echo "could not aquire $LOCKDIR"​
 +    exit 1
 +fi
 +echo "DO STUFF"
 +sleep 20
 +echo "​DONE"​
 +</​code>​
commands-shell-snippets.txt · Zuletzt geändert: 2018/02/04 17:36 von alex