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

8
  • Why not a script that broadcasts every 5 mins (with sleep 300 to control this) and then just does shutdown -h now? So you control the delay, rather than shutdown controlling it.
    – Tim
    May 20, 2015 at 7:05
  • I never thought about that but i believe for that method we have to use loops and have to write more codes!?. The above method is simple
    – Eka
    May 20, 2015 at 7:07
  • let me write it for you!
    – Tim
    May 20, 2015 at 7:09
  • I have to go to school now, so that code is just quick and dirty. When I get back I'll make it a little better, with a loop to make it shorter. Leave a comment here with @Tim to remind me to!
    – Tim
    May 20, 2015 at 7:18
  • 1
    @Eka askubuntu.com/questions/168879/… will mean you can avoid this.
    – Tim
    May 20, 2015 at 14:48

2 Answers 2

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
2
  • Common @Tim I think you can give better answers. ;)
    – A.B.
    May 20, 2015 at 12:26
  • @A.B. look at the time :P 07:17 (08:17 locat time), I leave at 20 past :P
    – Tim
    May 20, 2015 at 14:34
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.