How to run a script on shut down

Erik Christiansen dvalin at internode.on.net
Wed Nov 26 08:00:16 UTC 2008


On Tue, Nov 25, 2008 at 09:17:10PM -0800, Ray Parrish wrote:
> ... but I don't know where to put a corresponding 
> call to the stop monitoring script so it will run when I log off, or 
> shut down. I'd really like to find out, as this script does the final 
> log file clean up for the application so I have to run it manually 
> before I log off or it won't clean up the log files.
> 
> Anyone know how to do this?

The traditional way is with a "Kill script" in /etc/rc.2. Why not rc.3?
Well, though upstart doesn't care much about runlevels AFAIR, it's happy
to put on a show of being in level 2, at least when we're in X:

$ runlevel
N 2

Even with the new "upstart" event-based init system, the traditional
/etc/rc?.d files are supported on 7.10. Assuming your ubuntu version,
like mine, has /etc/rc2.d/S99rc.local , you could copy it to
/etc/rc2.d/K10rc.local (given that such a file doesn't pre-exist)

Now, simply insert your command in your nice new /etc/rc2.d/K10rc.local:

    stop)
         myscript and any options it takes         <-- Here
        ;;

That's it, if habit serves as memory.

Why K10? That's to run it early, before e.g. your networking has been
shut down.

      ------------------ Optional extra reading ------------------ 

Just checking that the newfangled upstart really does what we want, in
its "telinit compatibility wrapper", we see in line 284 of
/etc/init.d/rc :

         startup stop $SCRIPTS

will be iterated over your script on exiting runlevel 2, by line 262 :

         for i in /etc/rc$runlevel.d/K$level*

(And see /etc/event.d/rc2, which does an "exec /etc/init.d/rc 2")

So we _can_ still do it the traditional way.

Erik

-- 
If at first you don't succeed, try again.
Then Quit. No use being a damn fool about it.              - W.C. Fields





More information about the ubuntu-users mailing list