Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #This script outlines a very basic service system that only allows one version to be run at a time.
  4.  
  5. #Base location of lock file.
  6. loc=`dirname $0`
  7.  
  8.  
  9. #Function to run when this script exits.
  10. function rmlock {
  11. if [[ "$remove" -eq 1 ]]; then
  12. kill `cat "$loc/.lock"`
  13. rm "$loc/.lock";
  14. fi
  15. }
  16. #Trap exit signals.
  17. trap rmlock EXIT
  18.  
  19.  
  20. #Check if the lock file exists.
  21. remove=1
  22. if [[ -e "$loc/.lock" ]]; then
  23. echo "Nope. One is already running with PID:" `cat "$loc/.lock"`
  24. remove=0
  25. else
  26. touch "$loc/.lock"
  27. #The script or program to run.
  28. echo "Do thing" &
  29. echo $! >> "$loc/.lock"
  30. wait
  31. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement