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 19th Aug 03:48 (modification of post by jmbarnes view diff)
diff | download | new post

  1. #!/bin/bash
  2. #This is a script that acts a a sleep timer or basic alarm.
  3. #It is written for crunchbang linux installs--but should move
  4. #easily to most any linux distributions.
  5. #For most users you'll want to edit the series of commands
  6. #below to use whatever programs/utilities you favor
  7.  
  8. ######DEFAULT COMMANDS#####
  9.  
  10. #The path to the alarm noise (e.g. a beep, song, etc.)
  11. NOISE=~/music/WhatIGot.m4a
  12. #Program for playing the alarm
  13. ALARM="vlc --repeat $NOISE"
  14. #Screen locking command
  15. SCREENLOCK="xscreensaver-command -lock"
  16. #The suspending command
  17. SUSPEND="pm-suspend"
  18. #The hibernation command
  19. HIBERNATE="pm-hibernate"
  20. #Command or series of commands for shutting down
  21. SHUTDOWN="gdm-control --shutdown && openbox --exit"
  22. #Common use scripts (1st a backup script) You can
  23. #add more here and then duplicate lines 147-9, and edit accordingly.
  24. BACKUPSCRIPT="/home/jake/bin/remotebackup.sh"
  25.  
  26.  
  27. ########SCRIPT GENERAL INPUTS#####
  28. #Select action to take
  29. PS3="Choose:"
  30. echo ""
  31. echo "What action would you like to take at the end?"
  32. select act in "suspend" "hibernate" "lock screen" "shutdown" "play alarm"
  33. do
  34.         break
  35. done
  36.  
  37. #################################
  38. #Translating the "acts" to  proper commands
  39. #################################
  40. if [ "$act" = "suspend" ]; then
  41.         actcommand=$SUSPEND
  42. fi
  43. if [ "$act" = "hibernate" ]; then
  44.         actcommand=$HIBERNATE
  45. fi
  46. if [ "$act" = "lock screen" ]; then
  47.         actcommand=$SCREENLOCK
  48. fi
  49. if [ "$act" = "shutdown" ]; then
  50.         actcommand=$SHUTDOWN
  51. fi
  52. if [ "$act" = "play alarm" ]; then
  53.         actcommand=$ALARM
  54. fi
  55.  
  56. ##Asking for passwords for those commands which need root privileges
  57. if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then
  58.         echo "Selected action requires root privileges."
  59.         stty_orig=`stty -g`
  60.         stty -echo
  61.         read -p "Enter sudo password:" pass
  62.         stty $stty_orig
  63. fi
  64.  
  65. #Select when you would like to do it
  66. PS3="Choose: "
  67. echo ""
  68. echo "When would you like to do it?"
  69. select when in "after time" "after task" "whichever is later"
  70. do
  71.         break
  72. done
  73. echo ""
  74.  
  75. #Show the current alarm path
  76. if [ "$act" = "play alarm" ]; then
  77.         echo "Current path of alarm is set to $NOISE"
  78.         echo ""
  79. fi
  80.  
  81. #For 'whens' that require a time input
  82. if [ "$when" = "after time" ] || [ "$when" = "whichever is later" ]; then
  83.         read -p "How long of a delay (in minutes): " minutes   
  84.         echo ""
  85.         sectime=`date +%s`
  86.         secoff=$(($sectime+($minutes*60)))
  87.         dateoff=`date --date=@$secoff`
  88. fi
  89.  
  90. ########################
  91. #####The  After Time Routine
  92. ########################
  93. if [ "$when" = "after time" ]; then
  94.         #The Main Timer
  95.         elapsedtime=1
  96.         echo ""
  97.         echo "" 
  98.         echo "Computer will $act at $dateoff."
  99.         echo -n "Minutes left..."
  100.         echo -n "$minutes..."
  101.         while [ $elapsedtime -lt $minutes ]
  102.                 do
  103.                     sleep 1m
  104.                     echo -n "$(($minutes-$elapsedtime)).."
  105.             elapsedtime=$(($elapsedtime+1))
  106.                 done
  107.         #The last 60 seconds....
  108.         echo ""
  109.         echo ""
  110.         if [ $minutes -gt 0 ]; then
  111.                 echo "Will $act in (seconds)..."
  112.                 lastsixty=1
  113.                 while [ $lastsixty -le 60 ]
  114.                         do
  115.                     sleep 1s
  116.                            echo -n "$((60-$lastsixty))..."
  117.                     lastsixty=$(($lastsixty+1))
  118.                         done
  119.         fi
  120.         #The Action
  121.         if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then
  122.                 echo "$pass" | sudo -S $actcommand
  123.                 exit 0
  124.                 else
  125.                         $actcommand
  126.                         exit 0
  127.         fi
  128. fi
  129.  
  130. #####################
  131. #Inputing the task to follow
  132. #####################
  133. if [ "$when" = "after task" ] || [ "$when"  = "whichever is later" ]; then
  134.         PS3="Choose: "
  135.         echo ""
  136.         echo "Which task do you want to follow?"
  137.         select task in "Already running task (pid)" "A new task" "Backup script"
  138.         do
  139.                 break
  140.         done
  141.         if [ "$task" = "Already running task (pid)" ]; then
  142.                 read -p "Enter pid of task" taskpid
  143.         fi
  144.         if [ "$task" = "A new task" ]; then
  145.                 taskpid=0
  146.                 read -p "Enter (terminal) command to run: " taskcommand
  147.         fi
  148.         if [ "$task" = "Backup script" ]; then
  149.                 taskpid=0
  150.                 taskcommand=$BACKUPSCRIPT       
  151.         fi
  152. fi
  153.  
  154. ########################
  155. #####The After Task Routine
  156. ########################
  157. if [ "$when" = "after task" ]; then
  158.         if [ "$taskpid" = "0" ]; then
  159.                 terminator --command="$taskcommand" &
  160.                 clear
  161.                 taskpid=$!
  162.         fi
  163.         RUNNING=1
  164.         while [ $RUNNING -ge 1 ]
  165.                 do
  166.                         echo "Task still running"
  167.                         sleep 5s
  168.                         RUNNING=`ps $taskpid | grep -c $taskpid`
  169.                 done
  170.         echo "Task complete...will now $act ."
  171.                 if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then
  172.                 echo "$pass" | sudo -S $actcommand
  173.                 exit 0
  174.                 else
  175.                         $actcommand
  176.                         exit 0
  177.                 fi
  178. fi
  179.  
  180. ###############################
  181. #####The Whichever Is Later Routine
  182. ###############################
  183. if [ "$when"  = "whichever is later" ]; then
  184.         if [ "$taskpid" = "0" ]; then
  185.                 terminator --command="$taskcommand" &
  186.                 taskpid=$!
  187.         fi
  188.         RUNNING=1
  189.         while [ $RUNNING -ge 1 ]
  190.                 do
  191.                         echo "Task still running..."
  192.                         sleep 5s
  193.                         RUNNING=`ps $taskpid | grep -c $taskpid`
  194.                         clear
  195.                 done
  196.         echo ""
  197.         echo "Task complete...comparing with set $dateoff ."
  198.         postbackuptime=`date +%s`
  199.         if [ $secoff -gt $postbackuptime ]; then
  200.                 timedifference=$(($secoff-($postbackuptime)))
  201.                 echo ""
  202.                 echo "Sleeping for $timedifference seconds"
  203.                 sleep $timedifference
  204.         fi
  205.         if [ "$actcommand" = "$SUSPEND" ] || [ "$actcommand" = "$HIBERNATE" ]; then
  206.                 echo "$pass" | sudo -S $actcommand
  207.                 exit 0
  208.                 else
  209.                         $actcommand
  210.                         exit 0
  211.         fi
  212. fi
  213.  
  214. exit
  215. #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