From: Regina Kleinhappel Date: Tue, 29 Jul 2014 21:30:43 +0000 (+0200) Subject: Using lockfiles for unique instance. X-Git-Url: https://git.treefish.org/~alex/backmeupscotty.git/commitdiff_plain/333d926de93d4427e4c32c52e7dd18649132a00b Using lockfiles for unique instance. --- diff --git a/backmeupscotty b/backmeupscotty index ed41ae2..c1fa9da 100644 --- a/backmeupscotty +++ b/backmeupscotty @@ -18,22 +18,6 @@ function scottyerror { echo $(upperme): $@ >&2 } -if [ $(pidof -x $(basename $0) | wc -w) -gt 2 ]; then - scottyerror Another instance of $(basename $0) is already running! - exit 1 -fi - -while getopts "qn:" opt; do - case $opt in - q) - exec > /dev/null - ;; - n) - BACKUP_RUNEVERYNTHDAY=$OPTARG - ;; - esac -done - function grepbackups { ssh $REMOTE_HOST "ls $REMOTE_DIR" | grep -E '[0-9]+-[0-9]+' } @@ -116,6 +100,12 @@ function scottysync { done } +function deleteLock { + if ! rmdir /tmp/$(basename $0).lock; then + scottyerror "Could not delete lockfile /tmp/$(basename $0).lock!" + fi +} + function prepare { scottyinfo "Preparing for sync" } @@ -126,11 +116,14 @@ function cleanup_abrt { function _cleanup_abrt { scottyerror "Caught exit signal! Cleaning up..." + cleanup_abrt + if [ $(jobs -p) ]; then scottyerror TERMinating remaining child processes... kill $(jobs -p) fi + deleteLock exit } @@ -148,6 +141,7 @@ function cleanup_normal { function _cleanup_normal { cleanup_normal + deleteLock } function backmeupscotty { @@ -164,3 +158,19 @@ function backmeupscotty { trap - EXIT } + +if ! mkdir /tmp/$(basename $0).lock; then + scottyerror "Another instance of $(basename $0) is still running!" + exit 1 +fi + +while getopts "qn:" opt; do + case $opt in + q) + exec > /dev/null + ;; + n) + BACKUP_RUNEVERYNTHDAY=$OPTARG + ;; + esac +done diff --git a/backmeupscotty-example b/backmeupscotty-example index b9acea7..9418571 100755 --- a/backmeupscotty-example +++ b/backmeupscotty-example @@ -10,4 +10,16 @@ BACKUP_RUNEVERYNTHDAY=1 SYNC_SRC="/home/regina/backtest/source" SYNC_EXC="/gack,/gugu/*" +function prepare { + echo "prepare" +} + +function cleanup_normal { + echo "cleanup" +} + +function cleanup_abrt { + echo "cleanup_abrt" +} + backmeupscotty