Adding script to run at startup or shutdown in Linux

March 10, 2009 at 3:45 PM | Posted in Linux | 14 Comments

1. Write a script and put it into /etc/init.d/ directory. Your script must have start or stop options or both, depends on when you want to run your script. start is called at reboot or system startup and stop is called at shutdown.
2.  Add the executable permission to your script. Let say your script name is test
# chmod +x test

3. Create symbolic link in /etc/rc#.d where # is runlevel number. To know more about run level check /etc/initab or check init man page (type man init).
For example if you want to run your script at system startup,  create a symbolic link in /etc/rc3.d:

    # cd /etc/rc3.d/
    # ln -s ../init.d/scriptname S##scriptname where ## is order of executing script or you can say starting number. Use some 50 or 60. You should choose this number according to the needs of your application: if you try to make the application start too early, all disks may not be mounted yet, network adapters might not be initialized or some system services might not be started yet.
    the rc script will start all scripts with a capital S in numerical order.
    The shutdown portion is similar. Go to the run level that you want to shutdown at:
    – cd /etc/rc0.d/
    – ln -s ../init.d/scriptname K##scriptname where where ## is order of executing script or you can say starting number. Use some 50 or 60.
    the rc script will shutdown all scripts with a capital K in numerical order.
    sample script

    #!/bin/sh
    # Copy at startup and remove at shutdown
    #
    start() {
    cp /etc/test /var/tmp/
    }
    stop() {
    rm -rf /var/tmp/*
    }
    case “$1″ in
    start)
    start
    ;;
    stop)
    stop
    ;;

    *)
    echo $”Usage: $0 {start|stop}”
    RETVAL=1
    esac
    exit 0
    4) Your process is done. Now you can test your script.

    Few commands for runlevel and init
    1) To switch to particular run level use init
    $ telinit 6

    2) chkconfig is a tool that helps in creating/deleting/changing the symbolic links. It requires that you put two specially-formed comment lines in your application startup/shutdown script:
    #chkconfig –list to check the current startup settings
    # chkconfig: 345 80 20
    The numbers on the chkconfig line:
    – The first number (345) is a list of runlevels you’ll want your application. So it will run at runlevel 3, and 5.
    – 80 is the “starting position” number, meaning that this application will be started rather late in the boot process.
    – 20 is the “shutdown order” number. Unless your application has special requirements, it is recommended that the sum of startup and shutdown order numbers for each application should be 100.

    When you have added the two “magic” comment lines to your startup/shutdown script, just place the script to /etc/init.d and run:
    chkconfig –add
    If you need to temporarily block the system from starting/stopping the application automatically, you can use:
    chkconfig off
    To restore the auto-start function, use:
    chkconfig on

14 Comments »

RSS feed for comments on this post. TrackBack URI

  1. waha linux guru at his best..

    tuso top hoon sadi hope hoon:)

  2. Do I need to write the script also after creating softlink, If yes I need to edit /etc/rc script? Please confirm

  3. Wonderful items from you, man. I’ve take into accout your stuff prior to and you’re
    just extremely magnificent. I really like what you’ve acquired right here, really like what you’re stating and the way in which you assert it.
    You’re making it entertaining and you still take care of to keep it wise. I can not wait to read far more from you. That is actually a tremendous site.

    • Thank you so much for your wonderful comments.

  4. Hi, yup this paragraph is truyly pleasant and I have learned lot of things from it regarding blogging.
    thanks.

    • thanks

  5. […] Because this server is running headless I wanted to make sure it came up and shut down correctly, the way I did this was to make a script that played music using the ‘beep’ command and set it designated on startup, and another on shutdown.  It’s actually fairly easy to get your scripts to launch like any other daemon in linux using this tutorial. […]

  6. Hi it’s me, I am also visiting this website on a
    regular basis, this web page is in fact good and the viewers are actually sharing
    nice thoughts.

    • Thank you very much

    • Thank you.

  7. I’m more than happy to uncover this website.
    I wanted to thank you for your time just for this wonderful read!!

    I definitely loved every little bit of it and I have
    you saved to fav to look at new information in your web site.

    • Thank you very much Mitchell for your feedback.

  8. What i do not realize is actually how you are now not actually a lot more neatly-appreciated than you might be now.
    You’re so intelligent. You already know therefore significantly relating to this matter, made
    me for my part believe it from so many varied angles.

    Its like women and men aren’t interested except it is one thing
    to accomplish with Girl gaga! Your individual stuffs outstanding.
    All the time maintain it up!

  9. Good post. I learn something new and challenging on sites I stumbleupon everyday.
    It’s always exciting to read through content from other writers and use a little something from their web
    sites.


Leave a comment

Blog at WordPress.com.
Entries and comments feeds.