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
  • 2,967
  • 12
  • 38
  • 60
  • 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 '15 at 07: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 '15 at 07:07
  • let me write it for you! – Tim May 20 '15 at 07: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 '15 at 07:18
  • Why do you ask for a password in your script. That's not necessary. – A.B. May 20 '15 at 12:16
  • Shutdown command works with password only – Eka May 20 '15 at 12:17
  • 1
    @Eka http://askubuntu.com/questions/168879/shutdown-from-terminal-without-entering-password will mean you can avoid this. – Tim May 20 '15 at 14:48
  • @Tim thanks tim but i am afraid to edit any system files because recently i edited a system file and whole system got crashed and i have to re install it again. Any way i will look into your suggestion. – Eka May 20 '15 at 15:10

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
  • 32,861
  • 27
  • 118
  • 178
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,231