Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. queue_file='/tmp/nbody.queue'
  4. cmd="./test_cmd"
  5. lock_file="/tmp/nbody.lock"
  6.  
  7. remove_first_in_queue() {
  8. tail -n +2 "$queue_file" > "$queue_file.tmp" && mv "$queue_file.tmp" "$queue_file"
  9. }
  10.  
  11. run_command() {
  12. (
  13. flock -n 9 || exit 1
  14. eval "$cmd"
  15. remove_first_in_queue
  16. ) 9>"$lock_file"
  17. }
  18.  
  19. append_user_to_queue() {
  20. user_is_in_queue=$(grep "$USER" "$queue_file")
  21.  
  22. if [ -z "$user_is_in_queue" ]; then
  23. echo "$USER" >> $queue_file
  24. fi
  25. }
  26.  
  27. first_in_queue=$(head -1 "$queue_file")
  28.  
  29. if [ "$first_in_queue" == "$USER" ]; then
  30. run_command
  31. else
  32. append_user_to_queue
  33. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement