Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if mkdir $LOCKDIR
- then
- # Do important, exclusive stuff
- if rmdir $LOCKDIR
- then
- echo "Victory is mine"
- else
- echo "Could not remove lock dir" >&2
- fi
- else
- # Handle error condition
- ...
- fi
- #Remove the lock directory
- function cleanup {
- if rmdir $LOCKDIR; then
- echo "Finished"
- else
- echo "Failed to remove lock directory '$LOCKDIR'"
- exit 1
- fi
- }
- if mkdir $LOCKDIR; then
- #Ensure that if we "grabbed a lock", we release it
- #Works for SIGTERM and SIGINT(Ctrl-C)
- trap "cleanup" EXIT
- echo "Acquired lock, running"
- # Processing starts here
- else
- echo "Could not create lock directory '$LOCKDIR'"
- exit 1
- fi
- #!/bin/bash
- # Exit if /tmp/lock.file exists
- [ -f /tmp/lock.file ] && exit
- # Create lock file, sleep 1 sec and verify lock
- echo $$ > /tmp/lock.file
- sleep 1
- [ "x$(cat /tmp/lock.file)" == "x"$$ ] || exit
- # Do stuff
- sleep 60
- # Remove lock file
- rm /tmp/lock.file
- for LINE in `lsof -c <your_script> -F p`; do
- if [ $$ -gt ${LINE#?} ] ; then
- echo "'$0' is already running" 1>&2
- exit 1;
- fi
- done
- #!/bin/bash
- me=$(basename $0);
- running=$(ps h -C $me | grep -v $$ | wc -l);
- [[ $running > 1 ]] && exit;
- # do stuff below this comment
- #!/bin/sh
- [ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock "$0" "$0" "$@" || exit 1
- echo "servus!"
- sleep 10
- #!/bin/bash
- # Check if another instance of script is running
- pidof -x $0 >/dev/null && echo "ERROR: Script $0 already running" && exit 1
- ...
Add Comment
Please, Sign In to add comment