Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. create_lock_or_wait () {
  2. path="$1"
  3. wait_time="${2:-10}
  4. while true; do
  5. if mkdir "${path}.lock.d"; then
  6. break;
  7. fi
  8. sleep $wait_time
  9. done
  10. }
  11.  
  12. remove_lock () {
  13. path="$1"
  14. rmdir "${path}.lock.d"
  15. }
  16.  
  17. #!/bin/bash
  18.  
  19. # Makes sure we exit if flock fails.
  20. set -e
  21.  
  22. (
  23. # Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds
  24. flock -x -w 10 200
  25.  
  26. # Do stuff
  27.  
  28. ) 200>/var/lock/.myscript.exclusivelock
  29.  
  30. #!/bin/sh
  31. LOCKFILE=$HOME/.myscript/lock
  32. mkdir -p `dirname $LOCKFILE`
  33.  
  34. echo Waiting for lock $LOCKFILE...
  35. if lockfile -1 -r15 $LOCKFILE
  36. then
  37. # Do protected stuff here
  38. echo Doing protected stuff...
  39.  
  40. # Then, afterward, clean up so another instance of this script can run
  41. rm -f $LOCKFILE
  42. else
  43. echo "Failed to acquire lock! lockfile(1) returned $?"
  44. exit 1
  45. fi
  46.  
  47. lockfile ~/.config/mylockfile.lock
  48. .....
  49. rm -f important.lock
  50.  
  51. flom -- command_to_serialize
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement