how do I make a script run at linux restart/shutdown?

Red Squirrel

No Lifer
May 24, 2003
71,116
13,998
126
www.anyf.ca
I have an app that needs to be shutdown in a graceful way which is done by deleting a file that it checks for at certain intervals. Is there a way to script it so this file gets deleted when the system is either shut down or rebooted?

Eventually I want to learn how to do a start/stop script but I just want something quick and easy for now. I looked at them and it's way over my head for the time being. I just want something easy till I get around to diving deeper into those.
 

Red Squirrel

No Lifer
May 24, 2003
71,116
13,998
126
www.anyf.ca
Isin't that just for when the logged in user logs out though? I need it to happen at system reboot. Ex: UPS or other process initializes a reboot.
 

PCTC2

Diamond Member
Feb 18, 2007
3,892
33
91
Well, for start/stop scripts (like init scripts at run levels) just need a start/stop/restart function block. Something like
Code:
case "$1" in
   start)
       <your code here>
        ;;
   stop)
      <your code here>
        ;;
   restart|force-reload)
       <your code here>
        ;;
   reload)
        <your code here>
        ;;
        <your code here>
        ;;
    *)
         echo "Usage: $0 {start|stop|restart|status}"
         exit 1
         ;;
esac
exit 0
Then put it in /etc/init.d (or similar) and run chkconfig (red hat) or update-rc.d (debian) with the proper arguments to get it to start and stop on certain run levels.

Another option is to run once at start, which you can do in crontab. Instead of the time codes in crontab, you can use a tag like so:
Code:
@reboot   /path_to_script/executable_script.sh
 

Red Squirrel

No Lifer
May 24, 2003
71,116
13,998
126
www.anyf.ca
Oh wow is the start/stop script that simple? All the ones I looked at were really complicated. I will try that first, but good to know about the crontab option, I may try that as well.
 
Last edited:

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
They're only as complicated as the system/service requires. RedHat used to come with a skeleton script that was similar to that. But the basic gist is that you just need to handle the start/stop options if you only want the script for bootup/shutdown.
 

Gooberlx2

Lifer
May 4, 2001
15,381
6
91
Which system? In Opensuse/SLES, for example, there's /etc/init.d/halt.local where you can enter scripts/commands to run right before shutdown/reboot.

But yeah, writing your own init scripts is probably the best way to do it.
 
Last edited:

Fallen Kell

Diamond Member
Oct 9, 1999
6,243
556
126
Be aware, shutdown scripts are not guaranteed to run. There are numerous situations where they are skipped, or not given long enough time to execute (as well as the cases of the system losing power, kernel panic, or hardware failure). Better to have a wrapper script around your application's start routine which checks to see if it is already properly running, and if it is not, have it clean up things and start from the clean state. Using things like creating a file like "/var/run/<application_name>.pid" and putting the PID of your application in that file, and then you simply check if that file exists, then check to see if that PID exists, and that the PID is indeed your application is the better way to handle your situation.