diff options
author | Silvio Rhatto <rhatto@riseup.net> | 2013-08-25 22:28:21 -0300 |
---|---|---|
committer | Silvio Rhatto <rhatto@riseup.net> | 2013-08-25 22:28:21 -0300 |
commit | 18cf5a99d0eee6560ee2826969620bae30b91938 (patch) | |
tree | 334094b1aee331623f637abd9fc91b6d861a85aa /lib/httracker/functions | |
parent | 05ce52fe580b015d5cc21e0c31a999970a34f4d5 (diff) | |
download | httruta-18cf5a99d0eee6560ee2826969620bae30b91938.tar.gz httruta-18cf5a99d0eee6560ee2826969620bae30b91938.tar.bz2 |
Initial lockfile support
Diffstat (limited to 'lib/httracker/functions')
-rw-r--r-- | lib/httracker/functions | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/lib/httracker/functions b/lib/httracker/functions index 8e8964e..e3c81c4 100644 --- a/lib/httracker/functions +++ b/lib/httracker/functions @@ -109,12 +109,60 @@ function httracker_setup_folders { } # Set basic environment -function httracker_set_env { +function httracker_initialize { source `dirname $0`/config || exit 1 # Create folders httracker_setup_folders + + # Lockfile + LOCKFILE="${TMP}/`basename $0`.lock" + httracker_check_lockfile + httracker_set_lockfile +} + +# Cleanup environment +function httracker_teardown { + httracker_unset_lockfile + rm -rf $URLS +} + +# Create lockfile +function httracker_set_lockfile { + if [ ! -z "$LOCKFILE" ]; then + mkdir -p `dirname $LOCKFILE` + if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) &> /dev/null; then + trap 'httracker_unset_lockfile' INT TERM EXIT + else + echo "Could not create lockfile $LOCKFILE, exiting" + exit 1 + fi + fi +} + +# Remove lockfile +function httracker_unset_lockfile { + if [ ! -z "$LOCKFILE" ]; then + $rm -f $LOCKFILE || echo "Could not remove lockfile $LOCKFILE" + fi +} + +# Check lockfile +function httracker_check_lockfile { + local pid process + + if [ ! -z "$LOCKFILE" ] && [ -f "$LOCKFILE" ]; then + pid="`cat $LOCKFILE`" + process="`ps --no-headers -o comm $pid`" + if [ "$?" == "0" ] && [ "`ps --no-headers -o comm $$`" == "$process" ]; then + echo "Another program is running for $LOCKFILE, skipping run" + exit + else + echo "Found old lockfile $LOCKFILE, removing it" + unset_LOCKFILE + fi + fi } # Initialize -httracker_set_env +httracker_initialize |