Adding script to run at startup or shutdown in Linux

March 10, 2009 at 3:45 PM | Posted in Linux | 1 Comment

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

1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. waha linux guru at his best..

    tuso top hoon sadi hope hoon:)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.