Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #This script outlines a very basic service system that only allows one version to be run at a time.
- #Base location of lock file.
- loc=`dirname $0`
- #Function to run when this script exits.
- function rmlock {
- if [[ "$remove" -eq 1 ]]; then
- kill `cat "$loc/.lock"`
- rm "$loc/.lock";
- fi
- }
- #Trap exit signals.
- trap rmlock EXIT
- #Check if the lock file exists.
- remove=1
- if [[ -e "$loc/.lock" ]]; then
- echo "Nope. One is already running with PID:" `cat "$loc/.lock"`
- remove=0
- else
- touch "$loc/.lock"
- #The script or program to run.
- echo "Do thing" &
- echo $! >> "$loc/.lock"
- wait
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement