CrunchBang Linux Pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

CrunchBang Linux Pastebin

Posted by jmbarnes on Thu 27th Jan 21:02 (modification of post by view diff)
download | new post

  1. #!/bin/bash
  2. #Bash sleep timer
  3. #
  4. #Written by J. Barney.
  5. #
  6. #This script is originally made to be run
  7. #on a default installation of Crunchbang Linux (statler)
  8. #Depends upon: pm-utils, cvlc, xscreensaver, and bc
  9. #
  10. #Things you may need / want to edit:
  11. #   *the functions for actions to take after the timer (suspend etc)
  12. #   *command line flags in getopts section
  13. ########
  14.  
  15. ########
  16. #Function for after-timer actions
  17. ########
  18. suspend ()
  19. {
  20.     echo "$pass" | sudo -S pm-suspend
  21. #dbus-send --system --print-reply --dest=org.freedesktop.Hal \
  22. #/org/freedesktop/Hal/devices/computer \
  23. #org.freedesktop.Hal.Device.SystemPowerManagement.Suspend \
  24. #int32:0
  25. }
  26.  
  27. hibernate ()
  28. {
  29.     echo "$pass" | sudo -S pm-hibernate
  30. #Password-les dbus command
  31. #dbus-send --system --print-reply --dest=org.freedesktop.Hal \
  32. #/org/freedesktop/Hal/devices/computer \
  33. #org.freedesktop.Hal.Device.SystemPowerManagement.Hibernate \
  34. #int32:0
  35. }
  36.  
  37. alarm ()
  38. {
  39.     cvlc --repeat MUSICPATH
  40.     vlicpid=$!
  41.     sleep 2s
  42.     clear
  43.     read -p "Press enter to kill the alarm" dismiss
  44.     kill $vlcpid
  45. }
  46.  
  47. lock ()
  48. {
  49.     xscreensaver-command -lock
  50. }
  51.  
  52. shutdown ()
  53. {
  54.     echo "$pass" | sudo -S poweroff
  55. }
  56.  
  57. ########
  58. #Command line flag reading
  59. ########
  60. while getopts m:w:shatlp OPTION ; do
  61.     case $OPTION in
  62.     #Can be made more robust.
  63.         m) MINUTES=$OPTARG ;;
  64.         t) ROUTINE="timer" ;;
  65.         w) ROUTINE="wait"
  66.             PIDSCRIPT=$OPTARG ;;
  67.         s) ACTION="suspend" ;;
  68.         h) ACTION="hibernate" ;;
  69.         a) ACTION="alarm" ;;
  70.         l) ACTION="lock" ;;
  71.         p) ACTION="shutdown" ;;
  72.         \?) echo "Usage: timer.sh minutes -w wait -shalp"
  73.             echo "Options: -shalp are exclusive. Specify one"
  74.             echo "-s (suspend) -h (hibernate) -a (alarm) -l (lock)"
  75.             echo "-p (poweroff)."
  76.             echo "-m: requires number of minutes to wait (e.g. 50)"
  77.             echo "-w: requires PID to watch or script to run"
  78.             echo ""
  79.             echo "Example: timer.sh -m 40 -w ~/backup.sh -s"
  80.             echo "Will suspend after the backup or 40 minutes,"
  81.             echo "whichever is later."
  82.             exit 0
  83.         ;;
  84.     esac
  85. done
  86.  
  87.  
  88. ########
  89. #Assume default routine of timer unless specified otherwise)
  90. ########
  91. if [ "$ROUTINE" = "" ]; then
  92.    ROUTINE="timer"
  93. fi
  94.  
  95. ########
  96. #Getting neccesary unspecified variables
  97. ########
  98. if [ "$MINUTES" = "" ]; then
  99.     read -p "How many minutes until action? " MINUTES
  100.     echo ""
  101. fi
  102. if [ "$ACTION" = "" ]; then
  103.     echo "No post-timer action specified."
  104.     PS3="Choose:"
  105.     echo "Select action from the following:"
  106.     select ACTION in "suspend" "hibernate" "lock" "shutdown" "alarm"
  107.     do
  108.         break
  109.     done
  110.     echo ""
  111. fi
  112.  
  113. ########
  114. #Getting root privleges if necessary
  115. ########
  116. #Code below prompts for password while hidding entry.
  117. #Password unnecessary with direct accessing of Hal/Dbus instead of
  118. #using the pm-suspend from command line.
  119. case $ACTION in
  120.         shutdown | suspend | hibernate )
  121.                 echo ""
  122.                 echo "This action requires root privileges."
  123.                 stty_orig=`stty -g`
  124.                 stty -echo
  125.                 read -p "Enter sudo password:" pass
  126.                 stty $stty_orig
  127.         echo ""
  128.         ;;
  129. esac
  130.  
  131. ########
  132. #A few handy calculations
  133. ########
  134. SECTIME=`date +%s`
  135. SECOFF=$(($SECTIME+($MINUTES*60)))
  136. DATEOFF=`date --date=@$SECOFF`
  137.  
  138. echo "Will $ACTION after $MINUTES minutes ($DATEOFF)."
  139.  
  140. ########
  141. #Running the routines
  142. ########
  143. case $ROUTINE in
  144.     #The 'timer' routine
  145.         timer )
  146.                 elapsedtime=0
  147.                 echo ""
  148.                 echo  -n "Minutes left...$MINUTES..."
  149.                 while [ $elapsedtime -lt $MINUTES ]
  150.                 do
  151.                     sleep 1m
  152.                     echo -n "$(($MINUTES-$elapsedtime)).."
  153.             elapsedtime=$(($elapsedtime+1))
  154.                 done
  155.                 ;;
  156.     #The 'wait' routine
  157.         wait )
  158.                 if [ "$PIDSCRIPT" -eq "$PIDSCRIPT" ]; then
  159.                         echo "You passed a PID ($PIDSCRIPT)"
  160.                         else
  161.                                 echo "Running the passed script ($PIDSCRIPT) in a new terminal..."
  162.                                 sleep 2s
  163.                                 x-terminal-emulator --command="$PIDSCRIPT" &
  164.                                 PIDSCRIPT=$!
  165.                                 sleep 5s
  166.                                 clear
  167.                 fi
  168.                 RUNNING=1
  169.                 echo ""
  170.                 echo -n "Task still running..."
  171.                 while [ $RUNNING -ge 1 ]
  172.                         do
  173.                                 echo -n "..."
  174.                                 sleep 5s
  175.                                 RUNNING=`ps $PIDSCRIPT | grep -c $PIDSCRIPT`
  176.                         done
  177.                 echo "Task complete...comparing with set time $DATEOFF ."
  178.                 postbackuptime=`date +%s`
  179.                 if [ $SECOFF -gt $postbackuptime ]; then
  180.                         timedifference=$(($SECOFF-($postbackuptime)))
  181.                         minleft=$( echo "$timedifference/60" | bc )
  182.                         echo ""
  183.                         echo -n "Sleeping for $minleft minutes..."
  184.                         timeleft=1
  185.                         while [ "$timeleft" -le "$minleft" ]
  186.                                 do
  187.                                         sleep 1m
  188.                                         echo -n "$(($minleft-$timeleft))..."
  189.                                         timeleft=$(($timeleft+1))
  190.                                 done
  191.                 fi
  192.                 ;;
  193. esac
  194.  
  195. ########
  196. #A friendly warning before action
  197. ########
  198. echo ""
  199. echo -n "Will $ACTION in 10 seconds..."
  200. lastten=10
  201. while [ $lastten -gt 0 ]
  202.         do
  203.                 sleep 1s
  204.                 lastten=$(($lastten-1))
  205.                 echo -n "$lastten..."
  206.         done
  207. echo ""
  208.  
  209. ########
  210. #Running the post-routine action (based on functions at top)
  211. ########
  212. $ACTION
  213.  
  214. exit 0
  215.  
  216. #end

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me