Advertisement
devinteske

srbug

Jun 14th, 2015
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.26 KB | None | 0 0
  1. #!/bin/sh
  2. # -*- tab-width:  4 -*- ;; Emacs
  3. # vi: set tabstop=4     :: Vi/ViM
  4. ############################################################ INFORMATION
  5. #
  6. # A simple script to launch a sub-shell in the background that continually
  7. # updates the sudo(8) (or other sr-like utility) timestamp to prevent timeout.
  8. #
  9. # This is most useful if you know that you're going to need to execute many
  10. # commands but spread-out over a sizable time period but want to prevent the
  11. # requirement to enter your password every time.
  12. #
  13. # After launching this program, there should be a backgrounded process visible
  14. # by standard job-control utilities (such as `jobs'). When you exit your shell
  15. # this job will be terminated (unless configured otherwise in your shell
  16. # configuration).
  17. #
  18. # Usage (requires sh or bash):
  19. #   . path/to/srbug
  20. #
  21. ############################################################ CONFIGURATION
  22.  
  23. #
  24. # How long to sleep in-between executions
  25. #
  26. SLEEP_SECONDS=10
  27.  
  28. #
  29. # Maximum duration to run (in seconds).
  30. # Set to zero to disable (not recommended).
  31. #
  32. TIME_LIMIT=9000 # 2.5 hours
  33.  
  34. ############################################################ GLOBALS
  35.  
  36. #
  37. # Global exit status variables
  38. #
  39. SUCCESS=0
  40. FAILURE=1
  41.  
  42. #
  43. # Standard utility pathnames
  44. #
  45. SR=sudo
  46. SLEEP=sleep
  47.  
  48. ############################################################ MAIN SOURCE
  49.  
  50. #
  51. # Execute once before launching the background process (in case we are
  52. # presented with a password prompt).
  53. #
  54. $SR -v || exit $FAILURE
  55.  
  56. #
  57. # Launch a sub-shell in the background
  58. #
  59. (
  60.     n=0
  61.     while [ $n -le $TIME_LIMIT -o $TIME_LIMIT -eq 0 ]; do
  62.         #
  63.         # Exit if we lost our controlling TTY
  64.         #
  65.         tty=$( ps -p$! -otty= 2> /dev/null )
  66.         case "$tty" in
  67.         *ty*|*ts*) : do nothing;;
  68.         *) exit $FAILURE;;
  69.         esac
  70.  
  71.         $SLEEP 1
  72.  
  73.         [ $(( $n % $SLEEP_SECONDS )) -eq 0 ] && $SR -v
  74.         n=$(( $n + 1 ))
  75.     done
  76. ) &
  77.  
  78. #
  79. # Inform the user of what we did
  80. #
  81. echo "Backgrounded process $!"
  82.  
  83. ################################################################################
  84. # END
  85. ################################################################################
  86. #
  87. # $Header$
  88. #
  89. # $Copyright: 2011 Devin Teske. All rights reserved. $
  90. #
  91. # $Log$
  92. #
  93. ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement