3

I'm trying to set up a script for my school's computer that switches them off automatically at the end of the day (since most teachers forgot them on).

I thought to send a zenify warning box to inform the user that the PC is going to switch off and, if the user do not confirm in 1 minute, the PC shuts down.

I know the existence of the timeout command, but in this case I want to call sudo shutdown -h now if the zenity command do not return in 60 seconds.

I was trying to do something like

timeout 60 --onTimeout="sudo shutdown -h now" zenity --warning --text="This PC is going to shut down in a minute if you DO NOT press the OK button."

The command would be executed in a cron script.

So, the question is: How can I execute a command when the zenity program timeout?

0

1 Answer 1

4

You'll need to check the exit status and make a decision based on that. The manual for GNU timeout says that

If the command times out, and --preserve-status is not set, then exit with status 124.

So, in a shell script, you'd do something like this:

timeout 60 zenity ...
if [ $? = 124 ] ; then
    echo it timed out, do something...
else
    echo didn't time out
fi

Though, it appears that zenity supports a timeout in itself, so you don't need an external timeout:

zenity --timeout 60 --warning --text="Shutdown in 60"
if [ $? = 5 ] ; then
    sudo shutdown -h now
fi

Even if using Zenity's own timeout, it doesn't seem to show the time remaining. However, it does have a progress bar mode that could be used to convey a sense of impending doom. Something like this would display a timer counting down from 60 with a progress bar filling up at the same time:

#!/bin/bash
timeout=60
for (( i=0 ; i <= $timeout ; i++ )) do 
    echo "# Shutdown in $[ $timeout - $i ]..."
    echo $[ 100 * $i / $timeout ]
    sleep 1
done | zenity  --progress --title="Shutting down automatically..."  \
    --window-icon=warning --width=400 --auto-close

if [ $? = 0 ] ; then
    echo timeout passed
else 
    echo cancel was pressed or zenity failed somehow
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .