2

This is the script I made to lock and shutdown my system after 30 minutes.

#!/bin/bash
# Read Password
read -s -p "Password: " password
echo
# shutdown and screensaver Command
echo "$password" | sudo -kS shutdown -h +30 &
gnome-screensaver-command -a &
read -p "Press any key to continue... " -n1 -s 

The problem with this script is that sometimes it will not shutdown even after 30 minutes.

That's why I added a read command at the end - in order to monitor the status of shutdown sequence as well as a progress window. But this script only shows the status of the starting minute

eg:

Broadcast message from root@eka-PC
    (unknown) at 10:31 ...

The system is going down for halt in 30 minutes!

it doesn't show the progress of the shutdown.

EDITED:

I also found this bug in the above script, If a wrong password is entered the system will lock without any warning. Is it possible to give a conditional statement in shutdown command. I have tried this

SHUTDOWN=$(echo "$password" | sudo -kS shutdown -h +30 &);
if [[ $SHUTDOWN -eq 0]]
then
gnome-screensaver-command -a &
read -p "Press any key to continue... " -n1 -s 
fi

It activates only the shutdown but not the screensaver


WORKING SCRIPT

No solution found for validating shutdown (echo "$password" | sudo -kS shutdown -h +30 &) so i tried a workaround to this problem by confirming the password twice. This is the working script

#!/bin/bash

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`

echo "Activate screensaver and shutdowns the system in 30 minutes";
# Read Password
shut()
{
read -s -p "Password: " password1
read -s -p "Confirm Password: " password
echo
# Run Command
if [ "$password1" == "$password" ]
then
echo "$password" | sudo -kS shutdown -h +30 &
gnome-screensaver-command -a &
read -p "`echo $'\n> \n>'` ${green} To cancel shutdown, press C ${reset}`echo $'\n> \n>'`" prompt

    if [[ $prompt =~ [cC](es)* ]]
    then
    echo "$password" | sudo -kS shutdown -c
    read -p "`echo $'\n> \n>'`${red} Shutdown Cancelled ${reset}" -n1 -s
    fi
else
echo -e "\n${red} Wrong Password ${reset}, Re enter the password"
shut 
fi
}
shut

Added additional text formatting and shutdown cancellation code also to the above script.

If you want to activate a music player (eg Rhytmbox) as well, then after gnome-screensaver-command -a & add this code rhythmbox-client --play &

Any suggestion is aprreciated

Eka
  • 3,007

2 Answers2

2

This script controls the shutdown timer using sleep and a loop.

#!/bin/bash
# Read Password
read -s -p "Password: " password
echo
# Send messages
for i in {0..5}
do
    echo "Shutdown in" $((30- i * 5)) "minutes"
    sleep 300
done
echo "Shutdown now" | wall
# shutdown and screensaver Command
echo "$password" | sudo -kS shutdown -h +30 &
gnome-screensaver-command -a &
read -p "Press any key to continue... " -n1 -s
Tim
  • 33,500
1

Instead, you could use at command. Install it with

sudo apt-get install at

And run

sudo echo "shutdown -h now"|at  now + 30 min
solsTiCe
  • 9,515