user1056521
user1056521

Reputation:

commands at ubuntu shutdown

How to execute one or more commands and scripts when ubuntu shutdown? Is there any script like /etc/profile and ~/.bashrc at system starting? I know linux shutdown may have many causes, in addition to dealing with the kill signal, where can I get for this reason?

Upvotes: 1

Views: 3190

Answers (3)

Benjamin Lazarecki
Benjamin Lazarecki

Reputation: 2970

You can put your script in /etc/rc0.d (for halt) and /etc/rc6.d/ (for reboot). Make sure script has executable permission.

There is differents run level :
* 0 System Halt
* 1 Single user
* 2 Full multi-user mode (Default)
* 3-5 Same as 2
* 6 System Reboot

Run level 0 is the system halt condition. When run level 0 is reached all scripts in /etc/rc0.d are exectute.

Run level 6 is used to signal system reboot. This is the same as run level 0 except a reboot is issued at the end of the sequence instead of a power off.

If you want to execute your script on hibernate or on sleep, put your script in /etc/pm/sleep.d/

This is a example of script :

 #!/bin/sh

WLANSTATUS=`cat /sys/class/ieee80211/phy*/rfkill*/state`
test -z $WLANSTATUS && exit 1

case $1 in
    hibernate)
      # Do something before hibernate
        ;;
    suspend)
      # Do something before sleep
        ;;
    thaw)
      # Do something after hibernate
        ;;
    resume)
      # Do something after sleep
    if [ $WLANSTATUS = 0 ]; then
        echo 0 > /sys/devices/platform/asus_laptop/wlan    
    elif [ $WLANSTATUS = 1 ]; then
        echo 1 > /sys/devices/platform/asus_laptop/wlan
    fi
        ;;
    *)  echo "somebody is calling me totally wrong."
        ;;
esac

Have fun !

Upvotes: 2

Brian Cain
Brian Cain

Reputation: 14619

Is there any script like /etc/profile and ~/.bashrc at system starting?

The SysV Init scripts (/etc/init.d/*) are invoked at startup. A trivial/easy way to invoke some activity at system startup is to put it into /etc/init.d/local (/etc/rc.local for some other distros). See also: RcLocalHowto.

How to execute one or more commands and scripts when ubuntu shutdown?

It sounds as if you want to create a real init script that gets started on entering runlevels X-Z and stopped on exiting them. See also: UbuntuBootupHowto.

I know linux shutdown may have many causes, in addition to dealing with the kill signal, where can I get for this reason?

To do this noninteractively is not straightforward. You can grep through the system logs, looking for indications from shutdown.

Upvotes: 3

John Laine
John Laine

Reputation: 48

There is a ~/.bash_logout file that executes when you log out of Ubuntu 11.04 I am not sure, but assume there is a similar script in 10.04 Hope this helps.

Upvotes: 2

Related Questions