How can I make a script in /etc/init.d start at boot?

  • I think I read something a while back about this, but I can't remember how it's done. Essentially, I have a service in /etc/init.d which I'd like to start automatically at boot time. I remember it has something to do with symlinking the script into the /etc/rc.d directory, but I can't remember at the present. What is the command for this?

    I believe I'm on a Fedora/CentOS derivative.

    That depends on your OS. What unix variant are you running, and if Linux, what distribution? Which amongst `/etc/init`, `/etc/init.d`, `/etc/rc.d`, `/etc/rc.local` and a few more does it have?

    I think I'm on CentOS or something like it. Would the answer differ for Debian? I'm interested in a solution for Debian too.

  • canen

    canen Correct answer

    9 years ago

    If you are on a Red Hat based system, as you mentioned, you can do the following:

    1. Create a script and place in /etc/init.d (e.g /etc/init.d/myscript). The script should have the following format:
    # chkconfig: 2345 20 80
    # description: Description comes here....
    # Source function library.
    . /etc/init.d/functions
    start() {
        # code to start app comes here 
        # example: daemon program_name &
    stop() {
        # code to stop app comes here 
        # example: killproc program_name
    case "$1" in 
           # code to check status of app comes here 
           # example: status program_name
           echo "Usage: $0 {start|stop|status|restart}"
    exit 0 

    The format is pretty standard and you can view existing scripts in /etc/init.d. You can then use the script like so /etc/init.d/myscript start or chkconfig myscript start. The ckconfig man page explains the header of the script:

     > This says that the script should be started in levels 2,  3,  4, and
     > 5, that its start priority should be 20, and that its stop priority
     > should be 80.

    The example start, stop and status code uses helper functions defined in /etc/init.d/functions

    1. Enable the script

      $ chkconfig --add myscript 
      $ chkconfig --level 2345 myscript on 
    2. Check the script is indeed enabled - you should see "on" for the levels you selected.

      $ chkconfig --list | grep myscript

    Chkconfig is what u want. +1

    Why have init start something at runlevels 2, 3, 4 and 5? Don't you have to take care to avoid starting a server at runlevel 2? And runlevel 4 isn't defined on RHEL anyway, so why try to start it at that runlevel? Not criticism, just wanting to know. I'm a native Slackware user.

    4 isn't defined on RHEL, but it does exist. You can steal it for your own things.

    The header is taken from the chkconfig man page. I personally stick with 235 or just 35 in most cases.

    Is there a log I can look at if something fails to start during startup?

    for a CUI based interface you can use `ntsysv` to configure the runlevels

    Unfortunately this didn't work on Raspberry Pi running Raspbian Jessie. I still have to login if I want my Ngrok script to execute...

    Is there a particular reason why the script should `exit` with a code at the end?

    Instead of `chkconfig myscript start` we can use `service myscript start`, right?

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM