• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

Red Squirrel

No Lifer
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.
 
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.
 
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
 
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:
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.
 
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:
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.
 
Back
Top