Get To Know Linux: The /etc/init.d Directory

Jack Wallen
Apr 4, 2009
Updated • Dec 28, 2012
Linux
|
33

If you use Linux you most likely have heard of the init.d directory. But what exactly does this directory do? It ultimately does one thing but it does that one thing for your entire system, so init.d is very important. The init.d directory contains a number of start/stop scripts for various services on your system. Everything from acpid to x11-common is controlled from this directory. Of course it's not exactly that simple.

If you look at the /etc directory you will find directories that are in the form rc#.d (Where # is a number reflects a specific initialization level - from 0 to 6). Within each of these directories is a number of other scripts that control processes. These scripts will either begin with a "K" or an "S". All "K" scripts are run before "S" scripts. And depending upon where the scripts are located will determine when the scripts initiate. Between the directories the system services work together like a well-oiled machine. But there are times when you need to start or stop a process cleanly and without using the kill or killall commands. That is where the /etc/init.d directory comes in handy.

Now if you are using a distribution like Fedora you might find this directory in /etc/rc.d/init.d. Regardless of location, it serves the same purpose.

In order to control any of the scripts in init.d manually you have to have root (or sudo) access. Each script will be run as a command and the structure of the command will look like:

/etc/init.d/command OPTION

Where command is the actual command to run and OPTION can be one of the following:

  • start
  • stop
  • reload
  • restart
  • force-reload

Most often you will use either start, stop, or restart. So if you want to stop your network you can issue the command:

/etc/init.d/networking stop

Or if you make a change to your network and need to restart it, you could do so with the following command:

/etc/init.d/networking restart

Some of the more common init scripts in this directory are:

  • networking
  • samba
  • apache2
  • ftpd
  • sshd
  • dovecot
  • mysql

Of course there may be more often-used scripts in your directory - it depends upon what you have installed. The above list was taken from a Ubuntu Server 8.10 installation so a standard desktop installation would have a few less networking-type scripts.

But what about /etc/rc.local

There is a third option that I used to use quite a bit. This option is the /etc/rc.local script. This file runs after all other init level scripts have run, so it's safe to put various commands that you want to have issued upon startup. Many times I will place mounting instructions for things like nfs in this script. This is also a good place to place "troubleshooting" scripts in. For instance, once I had a machine that, for some reason, samba seemed to not want to start. Even afer checking to make sure the Samba daemon was setup to initialize at boot up. So instead of spending all of my time up front with this I simply placed the line:

/etc/init.d/samba start

in the /etc/rc.local script and Samba worked like a charm. Eventually I would come back and trouble shoot this issue.

Final Thoughts

Linux is flexible. Linux is so flexible there is almost, inevitably, numerous ways to solve a single problem. Starting a system service is one such issue. With the help of the /etc/init.d system (as well as /etc/rc.local) you can pretty much rest assured your service will start.

Advertisement

Previous Post: «
Next Post: «

Comments

  1. News247 said on May 9, 2014 at 12:56 pm
    Reply

    Well explained. I never knew what it does in Linux before.Thanks

  2. D said on October 16, 2012 at 6:12 pm
    Reply

    This is not a terribly good explanation. You talk about various aspects of the system without really explaining the theory behind it.

    1. A D said on April 17, 2013 at 2:39 pm
      Reply

      This is not terribly good feedback. You talk about various aspects of his guide without really explaining the concerns behind it.

      1. Anonymous said on November 28, 2013 at 6:06 am
        Reply

        lol!

  3. williams said on July 11, 2012 at 6:44 am
    Reply

    I used the following script to restart my DataIntegrator service:

    usr/bin/pgrep DataIntegrator || etc/init.d/DataIntegrator restart

    it stopped successfully but did not restart, instead I got the following message:

    opt/Pervasive/di-full-10.2.3-39/bin/di-service: line 387: exec: wrapper.daemonize=TRUE: not found

    I interpret this to mean the daemon didn’t recognize the service command. How can I amend the script so that it restarts the service properly?

    Also, i created a file for the script on the root dir, and would like to create a cron job to have the script run at 6pm (18) each night. The filename is root/check_services.txt. How do I save the script (after it is fixed of course) to the file, and then set the cron job for the file to execute the script?

  4. Sanjay Dodke said on May 11, 2012 at 1:27 pm
    Reply

    I am not able to install cachnamed server in linux ver5.2

  5. Jim Gish said on January 12, 2012 at 5:14 pm
    Reply

    Thanks — this was a very clear and informative posting. It answered every question I had about init.d

  6. beginner linux engineer said on December 28, 2011 at 8:43 am
    Reply

    dear all,

    currently i had install to setup connecting between sun solaris and titan ups server..
    i need to know about etc/init.d

  7. beginner linux engineer said on December 28, 2011 at 8:11 am
    Reply

    what will happen when we type “/etc/init.d”

  8. Wayne Sallee said on December 17, 2011 at 11:45 pm
    Reply

    You have:
    “/etc/initi.d/command OPTION”

    This needs to be changed to:
    “/etc/init.d/command OPTION”

    ie “initi.d” needs to changed to “init.d”

    Wayne Sallee
    Wayne@WayneSallee.com

    1. Martin Brinkmann said on December 18, 2011 at 10:10 am
      Reply

      Thanks Wayne, have corrected it.

  9. Jamal said on July 28, 2011 at 2:09 pm
    Reply

    Hi,

    How do you restart all services instead of doing it one by one?

    EG: /etc/init.d/httpd restart

    How do I restart everything in /etc/init.d ?

    1. Anonymous said on December 10, 2011 at 10:22 am
      Reply

      @jamal
      Init 2 on ubuntu
      Init 5 on red hat

  10. RomanG said on May 31, 2011 at 6:25 pm
    Reply

    I think your explanation regarding script naming and execution is incorrect.
    Script whose name is starting with an S ( start ) will be executed at that particular run level and scripts whose name is starting with a K ( kill ) will be killed at that particular run level and will not run at all.
    The number after an S or K determines an order in which these scripts are executed.

  11. Sandeep said on May 23, 2011 at 5:53 am
    Reply

    I kind of messed up with this directory. I was trying to remove a startup script from the /etc/init.d using rm and by mistake i removed the entire /etc/init.d . later i googled to find that /etc/init.d is a symlink to /etc/rc.d/init.d ; so i created to symlink /etc/init.d . what i did is that rite?

  12. rs said on February 6, 2011 at 2:11 am
    Reply

    could you add further explanations about the options.

    i.e Whats the difference between ‘reload’ and ‘restart’?

    1. Tim Oxley said on May 5, 2011 at 8:57 am
      Reply

      @rs reload will usually try reload the service’s configuration files without stopping the service (if possible). Often a restart is basically equivalent of doing a ‘service x stop’ then immediately doing a ‘service x start’, though how this works depends wholly on the script involved.

  13. Fuad said on November 10, 2010 at 4:57 pm
    Reply

    Hello i want to know how i can make crontab for my ircd ?im using bahamut,when shell is geeetong rebooting ircd is going down and it wont work untill i got to shell i up it myself,i want it to be up automatically,as my srvices when i kill them,or restart.Please help me with it.thanks./

  14. Jon said on May 5, 2010 at 5:18 pm
    Reply

    sorry… sudo touch /etc/init.d/FILE NAME

  15. Jon said on May 5, 2010 at 5:17 pm
    Reply

    to Abhilash:

    Even though you are logged in as root, try putting sudo before the touch command.

    sudo touch /etc/init.d/

  16. Abhilash said on April 15, 2010 at 5:10 am
    Reply

    May I know how we can create a new file in the init.d folder. I’ve used ‘touch’ command but it is saying ‘permission denied’ even though im the superuser (root). I’m actually installing subversion on my linux machine and I need to create and a new file in the init.d folder to configure the subversion server.

    could you please help me with this.

    Thank you

  17. mwanga said on March 19, 2010 at 2:04 pm
    Reply

    Great explanation, am new to linux and this explained the init.d directory quite easily….Altough i know the to be involved in the run level and service initialisation in the booting process.please post an article on that too..

  18. myro said on January 6, 2010 at 1:33 pm
    Reply

    thank you.
    well explained.

  19. nick said on April 5, 2009 at 5:12 pm
    Reply

    I ran into this command on Friday on my RHEL server which I thought was helpful:

    chkconfig

    ‘chkconfig’ allows the checking the startup level of services and gives the ability to set the service. Running ‘chkconfig’ will setup the scripts in the needed “rcX.d” directory.

    other command of use:

    /sbin/service “stop|start|restart|etc”

    this command is the equiv of typing “/etc/init.d/ ….”

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.