Powerthing


Powerthing is a program to schedule a timed shutdown/reboot. It functions pretty much the same as Gshutdown and Kshutdown. The difference is that Powerthing works, where others don't. Powerthing does not depend on a display manager, desktop environment, window manager, or anything. Powerthing does not even need a running X server to do the task it's meant to do. It just works.


History:

Originally written in 2007, to save a friend's soul from burning in the eternal flames of hell... sort of... A certain friend was complaining about having to use a certain proprietary operating system because of a certain peer to peer file sharing program that would sometimes take many hours to complete its task, thus forcing him to stay up until it finished so he could shut down the computer. The complaint was that, in that certain proprietary operating system, there is, or was, a certain function that would allow the user to schedule a timed shutdown. This friend complained further on that the only two similar programs he had found in linux either did not work, or simply did not work... And they didn't... So I took it upon myself to save his soul by writing a simple script that would complete the required task.

Back in 2007, powerthing was only one file, and had to run from terminal. Between January and August 2008, more files were added, mor functionality, and a graphical interface, using zenity.

As of 2008, no further work has been done on it. It worked, so why bother? Later versions of sudo refuse to work with echo, but the gksu/kdesu parts still work, as well as the gui-independent terminal version.

Some Linux distributions (read: Ubuntu), want to be cool and funky and no longer use gksu.  So that leaves the thing with a non working sudo | echo, and a non working gksu... bummer... :)

It would seem a re-write is in order.

Source Code:

powerthing

These are all the files that make powerthing work. The code is quite chaotic, and rather redundant at times, but it works.
#!/bin/bash

GNOME='gdm'
KDE='kdm'
OTHER='/usr/bin/startx'

if ps ax | grep -v grep | grep $GNOME > /dev/null
 then
  if [ -e "/usr/bin/zenity" ]
  then
  /usr/local/bin/powerkat &
  else
  /usr/local/bin/purtykat &
  fi
else
 if ps ax | grep -v grep | grep $KDE > /dev/null
  then
   if [ -e "/usr/bin/zenity" ]
   then
   /usr/local/bin/powerkat &
   else
   /usr/local/bin/purtykat &
   fi
  else
   if ps ax | grep -v grep | grep $OTHER > /dev/null
    then
     if [ -e "/usr/bin/zenity" ]
     then
     /usr/local/bin/zenkat &
     else
     /usr/local/bin/purtykat &
     fi
    else
     /usr/local/bin/termkat
   fi
 fi
fi

exit 0


powerkat

#!/bin/bash

### Powerthing v 1.0
###
### SEND ME MONEY!
### -_- Yogatron -_-


### Define variables

me=whoami
scren='screensaver'
PROCESS='shutdown'
gscren='gnome-screensaver'
xscren='xscreensaver'
gnome='gdm'
kde='kdm'
gdesk='nautilus'
kdesk='kdesktop'

### Find out whether shutdown is already running and, if it is, switch to "abort" mode.

if ps ax | grep -v grep | grep $PROCESS > /dev/null
then

### Warn user that shutdown will be aborted.
if [ -e "/usr/bin/mplayer" ]
then
mplayer /usr/share/sounds/powerthingalert1.wav &
fi

 if ! $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/powerthing.svg --title "Powerthing" --text "Aborting shutdown process
\n Continue?") ; then
  zenity --info --title="Powerthing" --window-icon=/usr/share/pixmaps/powerthing.svg --text="Screen will be locked again in 15 seconds."
  if [ -e "/usr/bin/mplayer" ]
  then
  mplayer /usr/share/sounds/powerthingalert2.wav &
  fi
  sleep 15
  if ps ax | grep -v grep | grep $gscren > /dev/null
   then
   gnome-screensaver-command --lock &
   else
   if ps ax | grep -v grep | grep $kde > /dev/null
   then
   if ps ax | grep -v grep | grep $kdesk > /dev/null
   then
   if [ -e "/usr/bin/xdg-screensaver" ]
   then
   xdg-screensaver lock &
   fi
   fi
   else
   if ps ax | grep -v grep |grep $gnome > /dev/null
   then
   if ps ax | grep -v grep | grep $kdesk > /dev/null
   then
   if [ -e "/usr/bin/xdg-screensaver" ]
   then
   xdg-screensaver lock &
   fi
   fi
   if ps ax | grep -v grep | grep $xscren > /dev/null
   then
   xscreensaver-command -lock &
   else
   if [ -e "/usr/bin/xscreensaver"]
   then
   xscreensaver -no-splash &
   xscreensaver-command -lock &
   fi
   fi
   fi
   fi
  fi

  exit 0
 fi


### get root password, with either gksu, kdesu, or plain old zenity, and switch to xterm, if none is available.

if [ -e "/usr/bin/gksudo" ]
 then
 gksudo -k -m "Powerthing requires r00t power" /bin/echo "got r00t?"
else
 if [ -e "/usr/bin/kdesu" ]
  then
   kdesu /bin/echo "got r00t"
 else
   if [ -e "/usr/bin/zenity" ]
    then
    PASS=$(zenity --entry --title="Powerthing" --text "Enter administrator password to continue." --hide-text)
   else
    xterm -e /usr/local/bin/termkat &
    exit 0
   fi
   if $PASS
    then
    exit 0
   else
    echo $PASS | sudo /bin/echo "we got r00t"
   fi
 fi
fi
### Abort shutdown

sudo shutdown -c

### Notify

if [ -e "/usr/bin/mplayer" ]
then
mplayer /usr/share/sounds/powerthingalert2.wav &
fi
zenity --info --window-icon=/usr/share/pixmaps/powerthing.svg --title='Powerthing' --text='Shutdown aborted successfully.'
exit 0

else

### If shutdown not running:
### Ask for confirmation
if [ -e "/usr/bin/mplayer" ]
then
mplayer /usr/share/sounds/powerthing-sounds/alert1.wav &
fi
if ! $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/powerthing.svg --title "Powerthing" --text "This script will shutdown/reboot your computer.
\n Continue?") ; then
 exit 0
fi

### Get password

if [ -e "/usr/bin/gksudo" ]
 then
  gksudo -k -m "Powerthing requires r00t power" /bin/echo "got r00t?"
 else
  if [ -e "/usr/bin/kdesu" ]
   then
    kdesu /bin/echo "I got r00t power"
    else
     if [ -e "/usr/bin/zenity" ]
      then
       PASS=$(zenity --entry --title="Powerthing" --text "Powerthing requires r00t power." --hide-text)
      else
       xterm -e /usr/local/bin/termpowerthing &
       exit 0
     fi
      if $PASS
       then
       exit 0
      else
       echo $PASS | sudo /bin/echo "got r00t power"
      fi
  fi
fi

### Get shutdown parameters.

PARAMETER=$(zenity --list --window-icon=/usr/share/pixmaps/powerthing.svg --title "Powerthing" --width 300 --height 50  --text "Choose desired action"\
  --radiolist \
  --column "Choice" --column "Value" --column "Meaning" \
  ONE r "Reboot" \
  TWO h "Shutdown" \
)
if $PARAMETER
 then
 exit 0
fi

### Determine time lapse (up to 6 hours) If you want more time, change "--max-value=480" to something higher.

WAIT=$(zenity --scale --window-icon=/usr/share/pixmaps/powerthing.svg --title="Powerthing" --text "Choose delay time. (minutes)" --min-value=1 --max-value=480 --value=1 --step 1)
if $WAIT
 then
 exit 0
fi

### Ultimate sanity check... in case you want to cancel
if [ -e "/usr/bin/mplayer" ]
then
mplayer /usr/share/sounds/powerthingalert2.wav &
fi
if ! $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/powerthing.svg --title "Powerthing" --text "Are you sure you want to do this?
You will still be able to abort the shutdown 
process by running this script again.") ; then
 exit 0
fi

### change user and activate screensaver and lock

su $me


if ps ax | grep -v grep | grep $gscren
   then
   gnome-screensaver-command --lock &
   else
   if ps ax | grep -v grep | grep $kde > /dev/null
   then
   if ps ax | grep -v grep | grep $kdesk > /dev/null
   then
   if [ -e "/usr/bin/xdg-screensaver" ]
   then
   xdg-screensaver lock &
   fi
   fi
   else
   if ps ax | grep -v grep |grep $gnome > /dev/null
   then
   if ps ax | grep -v grep | grep $kdesk > /dev/null
   then
   if [ -e "/usr/bin/xdg-screensaver" ]
   then
   xdg-screensaver lock &
   fi
   fi
   if ps ax | grep -v grep | grep $xscren > /dev/null
   then
   xscreensaver-command -lock &
   else
   if [ -e "/usr/bin/xscreensaver"]
   then
   xscreensaver -no-splash &
   xscreensaver-command -lock &
   fi
   fi
   fi
   fi
  fi


sudo shutdown -$PARAMETER $WAIT

fi


purtykat

#!/bin/bash

PROCESS='shutdown'
gpan='gnome-panel'
kpan='kicker'
xpan='xfce4-panel'
ppan='pypanel'
fpan='fbpanel'
lpan='lxpanel'
plpa='perlpanel'
fbox='/usr/bin/fluxbox'
icem='/usr/bin/icewm'
sttr='stalonetray'

if ps ax | grep -v grep | grep $PROCESS > /dev/null
 then
 xterm -e /usr/local/bin/termkat &
else

if ps ax | grep -v grep | grep $gpan > /dev/null
 then
 alltray xterm -e /usr/local/bin/termkat -stask &
else
 if ps ax | grep -v grep | grep $kpan > /dev/null
  then
  alltray xterm -e /usr/local/bin/termkat -stask &
 else
  if ps ax | grep -v grep | grep $xpan > /dev/null
   then
   alltray xterm -e /usr/local/bin/termkat -stask &
  else
   if ps ax | grep -v grep | grep $ppan > /dev/null
    then
    alltray xterm -e /usr/local/bin/termkat -stask &
   else
    if ps ax | grep -v grep | grep $fpan > /dev/null
     then
     alltray xterm -e /usr/local/bin/termkat -stask &
    else
     if ps ax | grep -v grep | grep $lpan > /dev/null
      then
      alltray xterm -e /usr/local/bin/termkat -stask &
     else
      if ps ax | grep -v grep | grep $plpa > /dev/null
       then
       alltray xterm -e /usr/local/bin/termkat -stask &
      else
       if ps ax | grep -v grep | grep $fbox > /dev/null
        then
        alltray xterm -e /usr/local/bin/termkat -stask &
       else
        if ps ax | grep -v grep | grep $icem > /dev/null
         then
         alltray xterm -e /usr/local/bin/termkat -stask &
        else
         if ps ax | grep -v grep | grep $sttr > /dev/null
          then
          alltray xterm -e /usr/local/bin/termkat -stask &
         else
          xterm -e /usr/local/bin/termkat &
         fi
        fi
       fi
      fi
     fi
    fi
   fi
  fi
 fi
fi

exit 0

fi


termkat

#!/bin/bash

### Terminal-based powerthing

thing=/usr/bin/startx

PROCESS='shutdown'
scren='screensaver'

if ps ax | grep -v grep | grep $PROCESS > /dev/null
then
 clear
 echo "Shutdown will now be aborted."
 echo "Press Ctrl + C to cancel."
 killall cmatrix
 sudo shutdown -c
 exit 0
else

clear
echo "Press Ctrl + C to Cancel"
sudo -v
read -p "What do you want to do? Restart=\"r\", shutdown=\"h\":"
parameter="$REPLY"
case $parameter in
 "p") parameter="P"
 action="shutdown";;
 "R") parameter="r"
 action="restart";;
 "C") parameter="c";;
 "K") parameter="k";;
 "h") action="halt or power off";;
 "H") action="halt";;
 "P") action="shutdown";;
 "r") action="restart";;
 *) echo "Invalid argument.  Exiting...";;
esac
echo ""
echo "How many minutes till $action?"
echo "Type 'now' for immediate execution."
read -p "Minutes:"
if ps ax | grep -v grep | grep $thing > /dev/null
 then
  if ps ax | grep -v grep | grep $scren > /dev/null
   then
   xdg-screensaver lock &
  else
   if [ -e "/usr/bin/xscreensaver" ]
    then
    xscreensaver -no-splash &
    xscreensaver-command -lock &
   fi

  fi

sudo shutdown -$parameter $REPLY

else
if [ -e "/usr/bin/cmatrix" ]
then
sleep 1
cmatrix &
fi
sudo shutdown -$parameter $REPLY
fi
fi


zenkat

#!/bin/bash

PROCESS='shutdown'
scren='screensaver'
gscren='gnome-screensaver'
xscren='xscreensaver'
gnome='gdm'
kde='kdm'
gdesk='nautilus'
kdesk='kdesktop'


if ps ax | grep -v grep | grep $PROCESS > /dev/null
then
if [ -e "/usr/bin/mplayer" ]
then
mplayer /usr/share/sounds/powerthingalert1.wav &
fi
 if ! $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/powerthing.svg --title "Powerthing" --text "Aborting shutdown process
\n Continue?") ; then
  if [ -e "/usr/bin/mplayer" ]
  then
  mplayer /usr/share/sounds/powerthingalert2.wav &
  fi
  zenity --info --title="Powerthing" --window-icon=/usr/share/pixmaps/powerthing.svg --text="Screen will be locked again in 15 seconds."
  sleep 15

if ps ax | grep -v grep | grep $gscren
   then
   gnome-screensaver-command --lock &
   else
   if ps ax | grep -v grep | grep $kde > /dev/null
   then
   if ps ax | grep -v grep | grep $kdesk > /dev/null
   then
   if [ -e "/usr/bin/xdg-screensaver" ]
   then
   xdg-screensaver lock &
   fi
   fi
   else
   if ps ax | grep -v grep |grep $gnome > /dev/null
   then
   if ps ax | grep -v grep | grep $kdesk > /dev/null
   then
   if [ -e "/usr/bin/xdg-screensaver" ]
   then
   xdg-screensaver lock &
   fi
   fi
   if ps ax | grep -v grep | grep $xscren > /dev/null
   then
   xscreensaver-command -lock &
   else
   if [ -e "/usr/bin/xscreensaver"]
   then
   xscreensaver -no-splash &
   xscreensaver-command -lock &
   fi
   fi
   fi
   fi
  fi

  exit 0
 else

PASS=$(zenity --entry --window-icon=/usr/share/pixmaps/powerthing.svg --title="Powerthing" --text "Enter administrator password to continue." --hide-text)

if $PASS
 then
 exit 0
fi

echo $PASS | sudo shutdown -c

fi
if [ -e "/usr/bin/mplayer" ]
then
mplayer /usr/share/sounds/powerthingalert2.wav &
fi
zenity --info --window-icon=/usr/share/pixmaps/powerthing.svg --title='Powerthing' --text='Shutdown aborted successfully.'

exit 0

else
if [ -e "/usr/bin/mplayer" ]
then
mplayer /usr/share/sounds/powerthingalert1.wav &
fi
if ! $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/powerthing.svg --title "Powerthing" --text "This script will shutdown/reboot your computer.
\n Continue?") ; then
 exit 0
fi

PASS=$(zenity --entry --window-icon=/usr/share/pixmaps/powerthing.svg --title="Powerthing" --text "Enter administrator password to continue." --hide-text)

if $PASS
 then
 exit 0
fi

PARAMETER=$(zenity --list --window-icon=/usr/share/pixmaps/powerthing.svg --title "Powerthing" --width 300 --height 50  --text "Choose desired action"\
  --radiolist \
  --column "Choice" --column "Value" --column "Meaning" \
  ONE r "Reboot" \
  TWO h "Shutdown" \
)
if $PARAMETER
 then
 exit 0
fi

WAIT=$(zenity --scale --window-icon=/usr/share/pixmaps/powerthing.svg --title="Powerthing" --text "Choose delay time. (minutes)" --min-value=1 --max-value=480 --value=1 --step 1)
if $WAIT
 then
 exit 0
fi
if [ -e "/usr/bin/mplayer" ]
then
mplayer /usr/share/sounds/powerthingalert2.wav &
fi
if ! $(zenity --question --no-wrap --window-icon=/usr/share/pixmaps/powerthing.svg --title "Powerthing" --text "Are you sure you want to do this?
You will still be able to abort the shutdown 
process by running this script again.") ; then
 exit 0
fi

if ps ax | grep -v grep | grep $gscren > /dev/null
   then
   gnome-screensaver-command --lock &
   else
   if ps ax | grep -v grep | grep $kde > /dev/null
   then
   if ps ax | grep -v grep | grep $kdesk > /dev/null
   then
   if [ -e "/usr/bin/xdg-screensaver" ]
   then
   xdg-screensaver lock &
   fi
   fi
   else
   if ps ax | grep -v grep |grep $gnome > /dev/null
   then
   if ps ax | grep -v grep | grep $kdesk > /dev/null
   then
   if [ -e "/usr/bin/xdg-screensaver" ]
   then
   xdg-screensaver lock &
   fi
   fi
   if ps ax | grep -v grep | grep $xscren > /dev/null
   then
   xscreensaver-command -lock &
   else
   if [ -e "/usr/bin/xscreensaver"]
   then
   xscreensaver -no-splash &
   xscreensaver-command -lock &
   fi
   fi
   fi
   fi
  fi




echo $PASS | sudo shutdown -$PARAMETER $WAIT

fi