Automatic Network Shutdown / Restart

Darryl Moore darryl-90a536wCiRb3fQ9qLvQP4Q at public.gmane.org
Wed Dec 9 19:19:33 UTC 2009


FYI here is a script I wrote for similar purposes.

I often leave the kids computer on without any one using it because it
also acts as a print server. Therefore I don't want it going to sleep
after a period of inactivity. However I do want to make sure it shuts
down at night, and if someone is using it, I want to make sure they get
sufficient warning before hand.

My solution was to have /etc/crontab as follows:


# m h dom mon dow user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts
--report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts
--report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts
--report /etc/cron.monthly )
50 22   * * *   root    xwrite "This machine is going down in ten (10)
minutes. Please save your work and log off."
55 22   * * *   root    xwrite --warning "This machine is going down in
five (5) minutes. Please Log off now."
59 22   * * *   root    xwrite --error --title Shutdown "This machine is
going down in one (1) minute. This is the last warning.\n YOU MUST LOG
OFF NOW!!!"
00 23   * * *   root    shutdown -P now
#

this gives any logged in users 10min then 5min then 1min warning before
shutdown.

xwrite is a script I wrote which displays a popup message on all
xwindows sessions.

it looks like this:

#!/bin/bash
#
# xwrite
#
# send message to users on all graphic terminals
#

dialogtype="--info"

userlist=

while test -n "$1"; do
    case "$1" in
        --title)
            title="--title $2"
            shift
            ;;
        --warning)
            dialogtype="--warning"
            ;;
        --error)
            dialogtype="--error"
            ;;
		--info)
            dialogtype="--info"
            ;;
        --user)
            userlist="$userlist $2"
            shift
            ;;
        *)
        message=$1
        shift
        ;;
    esac
    shift
done

if [[ ! $userlist ]]; then
    userlist=$(users | tr " " "\n" | uniq)
fi

#itterate through all users logged into this machine
for myuser in $userlist; do
    #get list of graphic terminals each user in logged in on

    displaylist=$(last | grep "$myuser.*[[:space:]]:[[:digit:]].*still
logged in" | awk '{print $2 "\n" $3;}' | grep "^:[[:digit:]][[:digit:]]*$")
    #itterate through every terminal for every user
    for display in $displaylist; do
        #display system message
        sudo -u $myuser /usr/bin/zenity $dialogtype $title --text
"$message" --display=$display &
    done
done


--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list