diff options
Diffstat (limited to 'lib')
-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 |