Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # -*- tab-width: 4 -*- ;; Emacs
- # vi: set tabstop=4 :: Vi/ViM
- ############################################################ INFORMATION
- #
- # A simple script to launch a sub-shell in the background that continually
- # updates the sudo(8) (or other sr-like utility) timestamp to prevent timeout.
- #
- # This is most useful if you know that you're going to need to execute many
- # commands but spread-out over a sizable time period but want to prevent the
- # requirement to enter your password every time.
- #
- # After launching this program, there should be a backgrounded process visible
- # by standard job-control utilities (such as `jobs'). When you exit your shell
- # this job will be terminated (unless configured otherwise in your shell
- # configuration).
- #
- # Usage (requires sh or bash):
- # . path/to/srbug
- #
- ############################################################ CONFIGURATION
- #
- # How long to sleep in-between executions
- #
- SLEEP_SECONDS=10
- #
- # Maximum duration to run (in seconds).
- # Set to zero to disable (not recommended).
- #
- TIME_LIMIT=9000 # 2.5 hours
- ############################################################ GLOBALS
- #
- # Global exit status variables
- #
- SUCCESS=0
- FAILURE=1
- #
- # Standard utility pathnames
- #
- SR=sudo
- SLEEP=sleep
- ############################################################ MAIN SOURCE
- #
- # Execute once before launching the background process (in case we are
- # presented with a password prompt).
- #
- $SR -v || exit $FAILURE
- #
- # Launch a sub-shell in the background
- #
- (
- n=0
- while [ $n -le $TIME_LIMIT -o $TIME_LIMIT -eq 0 ]; do
- #
- # Exit if we lost our controlling TTY
- #
- tty=$( ps -p$! -otty= 2> /dev/null )
- case "$tty" in
- *ty*|*ts*) : do nothing;;
- *) exit $FAILURE;;
- esac
- $SLEEP 1
- [ $(( $n % $SLEEP_SECONDS )) -eq 0 ] && $SR -v
- n=$(( $n + 1 ))
- done
- ) &
- #
- # Inform the user of what we did
- #
- echo "Backgrounded process $!"
- ################################################################################
- # END
- ################################################################################
- #
- # $Header$
- #
- # $Copyright: 2011 Devin Teske. All rights reserved. $
- #
- # $Log$
- #
- ################################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement