Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Forum Archive > Absolute Beginner Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Hello, Unregistered You are browsing a READ only archive of the main support categories pre 4/21/2008. You will not be able to post or reply any threads in this section.
Ubuntu 9.10 is out!!!

When downloading Ubuntu 9.10 please consider using bittorrent to get your copy of Ubuntu.

The Ubuntu Developers Summit for Lucid Lynx will be held the week of 16-Nov-2009 till 20-Nov-2009 in Dallas, TX USA. Visit the the Ubuntu wiki for more information about UDS and how to participate remotely.

Absolute Beginner Talk
The perfect starting place to find out more about computers, Linux and Ubuntu.

 
Thread Tools Display Modes
Old August 26th, 2005   #1
ummo
First Cup of Ubuntu
 
Join Date: Jul 2005
Beans: 7
Question Running a script before shutdown or reboot

i would like to run a script that does some logging of data automatically whenever i shutdown or reboot my system.

The script is already made and works fine however i am at a loss as to how to have it automatically run right before the shutdown and reboot commands are run.

I initially thought of adding the following aliases to my /root/.bashrc file:

Code:
alias shutdown='echo running script...; /home/user/scripts/script.py; echo Done; shutdown'
alias reboot='echo Running script...; /home/user/scripts/script.py; echo Done; reboot'
however executing sudo reboot does not run my script, just reboots like normal.

Also i have no idea what happens when the system is shutdown from the Gnome Panel's System menu. And that is fairly important to me sincie i often use both the shutdown option from the menu as well as the command line method.

Am i not setting my aliases correctly?
Is there some universal system-wide shutdown / reboot script that lists tasks to do on those events?
ummo is offline   Reply With Quote
Old August 26th, 2005   #2
Juergen
Way Too Much Ubuntu
 
Join Date: Oct 2004
Location: Frankfurt am Main/Germany
Beans: 258
Re: Running a script before shutdown or reboot

I think you have to become familiar with the runlevels.
This seems to be a nice intro. (a bit old, though. So some locations/commands might have changed... )

In short you should put your script into /etc/init.d (as root) and then set up the right links in /etc/rcX.d where you want them executed when the runlevel changes.

The shutdown-runlevel is 0, but maybe you also want to do your logging for a change from runlevel 2 (Debian/Ubuntu default for normal operation) to 1 (single user)?
Thats up to you. Just create the links.
Juergen is offline   Reply With Quote
Old August 27th, 2005   #3
cwaldbieser
May the Ubuntu Be With You!
 
cwaldbieser's Avatar
 
Join Date: Oct 2004
Location: Pennsylvania
Beans: 1,697
Re: Running a script before shutdown or reboot

Quote:
Originally Posted by ummo
Am i not setting my aliases correctly?
Is there some universal system-wide shutdown / reboot script that lists tasks to do on those events?
If you create a function or an alias in bash, and you want to make sure the command you issue is what you expect it should be, the type command will help you:
Code:
$ hello () { echo Hello'!'; }
$ alias hi="echo Hi."
$ hello
Hello!
$ hi
Hi.
$ type hello
hello is a function
hello () 
{ 
    echo Hello'!'
}
$ type hi
hi is aliased to `echo Hi.'
cwaldbieser is offline   Reply With Quote
Old August 29th, 2005   #4
ummo
First Cup of Ubuntu
 
Join Date: Jul 2005
Beans: 7
Talking Re: Running a script before shutdown or reboot

Thanks for the help guys. I think I've got it working now.

For completeness sake I'll post what i did, for anyone else who wants to do something similiar:
From the link Juergen provided I learned to put my script: scriptname.blah in the /etc/init.d directory and create a link to the script named K02scriptname.blah in both the /etc/rc0.d/ and /etc/rc6.d/ directories.

Runlevel 0 is halt.
Runlevel 1 is single-user.
Runlevels 2-5 are multi-user.
Runlevel 6 is reboot.
There are /etc/rc#.d/ folders for each # of the Runlevels.

Normally during typical ubuntu use the computer is at Runlevel 2, at least thats the default setting in my /etc/inittab file.
Runlevel 0 shuts the computer down and runlevel 6 reboots. /etc/rc0.d/ and /etc/rc6.d/ contain links to the scripts to be executed when switching to those runlevels. The names of the links in these folders begin with either a K or S followed by some 2-digit number and then the name of the script in /etc/init.d/ they are linked to. The K stands for kill and indicates that init will send the stop argument to the script linked to, S is for start and init sends the start argument to the script if the link begins with S. The K links are run first in the order of the 2-digit numbers that follw the K followed by the S scripts in the same way. Following the digits is the exact name of the script in the /etc/init.d/ directory.

So since i wanted my script run at shutdown and reboot I named the links in the /etc/rc0.d/ and rc6.d/ directories as a kill script with a low number even though my script doesnt kill any processes and doesnt use the 'stop' argument given to it. I wanted my script to run before all of the services my script utilizes are stopped for shutdown. So now it runs right after gnome is killed and before all the other vital processes are stopped everytime my system shuts down.

I hope this makes sense and definitely check Juergen's link for the real story.
ummo is offline   Reply With Quote
Old May 8th, 2006   #5
d00d
First Cup of Ubuntu
 
Join Date: Apr 2006
Beans: 4
Re: Running a script before shutdown or reboot

My question is related to this only...

This is an example when one wants to run the script after gnome has exited, what do we do if we want to run the script before Gnome exits but after we have either killed the window manager of shut it down using the Shutdown/Logoff dialog.
__________________
Visit my site at http://rohit.aroways.com/web/
d00d is offline   Reply With Quote
Old May 10th, 2006   #6
coquinho
First Cup of Ubuntu
 
Join Date: May 2006
Beans: 2
Re: Running a script before shutdown or reboot

I think you can influence the order in which programs are shutdown with the runlevel system. Check this part of juergens description...
coquinho is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:52 PM.


vBulletin ©2000 - 2009, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry