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

Discussion in '*nix Software' started by Red Squirrel, Mar 13, 2012.

  1. Red Squirrel

    Red Squirrel Lifer

    Joined:
    May 24, 2003
    Messages:
    40,726
    Likes Received:
    1,217
    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.
     
  2. Loading...

    Similar Threads - make script linux Forum Date
    Would Ubuntu make a good secure server for VPN server? *nix Software Jun 3, 2016
    Extace - ESound Visualizer. How to make it work w/Pulse? *nix Software Feb 7, 2016
    Apple makes its Swift language open source *nix Software Dec 5, 2015
    How do I make an executable bash script? *nix Software Sep 11, 2015
    How do I make sure I have the right drivers for installing Linux on T61 Thinkpad? *nix Software Jun 29, 2015

  3. bobross419

    bobross419 Golden Member

    Joined:
    Oct 25, 2007
    Messages:
    1,981
    Likes Received:
    0
    Have you tried just adding a rm to the bash.logout?
     
  4. Red Squirrel

    Red Squirrel Lifer

    Joined:
    May 24, 2003
    Messages:
    40,726
    Likes Received:
    1,217
    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.
     
  5. PCTC2

    PCTC2 Diamond Member

    Joined:
    Feb 18, 2007
    Messages:
    3,879
    Likes Received:
    8
    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
    
     
  6. Red Squirrel

    Red Squirrel Lifer

    Joined:
    May 24, 2003
    Messages:
    40,726
    Likes Received:
    1,217
    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.
     
    #5 Red Squirrel, Mar 13, 2012
    Last edited: Mar 13, 2012
  7. Nothinman

    Nothinman Elite Member

    Joined:
    Sep 14, 2001
    Messages:
    30,672
    Likes Received:
    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.
     
  8. Gooberlx2

    Gooberlx2 Lifer

    Joined:
    May 4, 2001
    Messages:
    15,384
    Likes Received:
    5
    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.
     
    #7 Gooberlx2, Mar 14, 2012
    Last edited: Mar 14, 2012
  9. Fallen Kell

    Fallen Kell Diamond Member

    Joined:
    Oct 9, 1999
    Messages:
    4,982
    Likes Received:
    7
    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.