How to run a script on shut down

Paul Johnson pauljohn32 at gmail.com
Wed Nov 26 20:14:10 UTC 2008


On Tue, Nov 25, 2008 at 11:17 PM, Ray Parrish <crp at cmc.net> wrote:
> Hello,
>
> I have a script I've written which checks to see if my web site is down,
> and warns me when it isn't working. It depends on another script to stop
> it's inner loop, by deleting a file called go.txt from it's working
> directory in order to shut it down by stopping the loop.
>
> I can start the web monitoring script from Preferences, Sessions by
> adding a call to it there, 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?
>

In the gnome display manager (gdm) there are PreSession/Default  and
PostSession/Default  scripts that can be customized to start that
program when you log in and shut it off when you log out. Look in
/etc/gdm for the directories.  These things run as root, but you can
adjust them to interact with your session.  There is also a BASH
script you can setup to do that, ~/.bash_logout can execute stuff when
you quit.  ~/.bashrc can launch things when you log in.  My
recollection is that the PostLogin/Default script runs as a user, and
so you can put your startup program there as well.  In the PostSession
script, I think you could just send a kill signal to your script or
you could add a new instruction to do some work when you are done.

The current approach you describe is using the gnome session
framework, and it is handy for starting things, as you've noticed.
But it is not so easy to manage shut-down events.

Can I suggest an alternative approach?  The first thing I'd consider
is running this as a cron job.  If you set your program to run under
/etc/cron somewhere, it will run every so often and it will go even if
you are not logged in. Your program does not need its own "loop", the
cron will handle that.  If you have the helper program anacron
installed, then jobs that don't run while you are turned off will be
executed after you restart.


If you don't like that idea, I have another approach that can run as a
user.  If you want this thing to run after you are logged, you can use
nohup.  nohup does not work for gui programs, but it does work for
batch scripts and other non-interactive things.   In the past, I have
sometimes noticed that things don't keep running when I log out of X,
and so I've gotten the habit of logging in on the virtual terminal or
logging into a system remotely.  To get in the virtual terminal.

Alt-Ctrl-F1

There you can log in, and then run your  program under "nohup"

nohup myProgram

nohup has the magical property that the program will run even after you type

exit

from the virtual terminal.

I believe the BASH command disown is basically the same.  I just
learned about this one. Ordinarily, if you run something in a
terminal, then killing the terminal kills the program.  But if you
have an ordinary X terminal open and you run

myProgram &

followed by

disown

Then that disconnects that program from the terminal.

The advantage of the nohup approach is that it automatically generates
a text output file to collect standard output and standard error.
I've found that to be handy.

I wonder if you couldn't just re-think your script a bit to solve the
need to do that clean up after.  I think you can avoid the need to
write that file at all.  The function at can be used to schedule
actions in the future.  You can use atq to see future actions and atrm
to remove them.  I'd suggest you revise your program a bit to use at
to schedule future checks.  If you shut off while events are waiting,
there's no damage.  You can use at in a script that refers to itself,
so that if you run the script once, it does its work and then puts
itself in the schedule for later.  So if you just write your script so
it clean up when starts, then wouldn't everything "just work".

Here's a script I use to change the backgrounds every 15 minutes. It
is called "compizChanger.sh".  Note the last line calls itself to run
again.

pjcompizbackground.pl -w 1 -W 1 -d /usr/share/backgrounds
pjcompizbackground.pl -w 2 -W 1 -d /usr/local/share/Backgrounds
at -f /home/pauljohn/bin/compizChanger.sh NOW + 15min

Well, you can tell I'm on vacation and have plenty of time to waste :)

HTH

PJ



-- 
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas




More information about the ubuntu-users mailing list