Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #!/bin/bash
  2. set -eou pipefail
  3.  
  4. # usage: auto-restart.sh python program.py
  5.  
  6. trap 'exit 1' SIGINT
  7.  
  8. count=$(ps -eo args | grep ^/bin/bash | grep "auto-restart $*"$ | grep -v grep | wc -l)
  9. if (($count > 2)); then
  10. echo already running: auto-restart $@ >&2
  11. exit 1
  12. fi
  13.  
  14. reset_count_after_seconds=${reset_count_after_seconds:-10}
  15. max_retries=${max_retries:-5}
  16. now() { date +%s; }
  17.  
  18. last=$(now)
  19. retry_count=0
  20. while true; do
  21. if eval "$@"; then
  22. echo exited 0: "$@" >&2
  23. else
  24. echo exited $?: "$@" >&2
  25. fi
  26. seconds_since_last_bounce=$(($(now) - $last))
  27. if (($seconds_since_last_bounce > $reset_count_after_seconds)); then
  28. echo resetting bounce count since $seconds_since_last_bounce seconds have passed since the last bounce >&2
  29. retry_count=0
  30. fi
  31. retry_count=$(($retry_count + 1))
  32. if (($retry_count > $max_retries)); then
  33. echo exceeded max max_retries $max_retries
  34. exit 1
  35. fi
  36. last=$(now)
  37. echo sleep for retry_count=$retry_count seconds >&2
  38. sleep $retry_count
  39. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement