4

I've been working with linux for a while but in a rather simple manner.

I understand that scripts in init.d are executed when the os starts but how exactly does it works?

How does the os know which paramater to pass to a script?

To start apache I would do sudo /etc/init.d/apache2 start. If I run sudo /etc/init.d/apache2 it doesn't work without the start. How does the os pass start to the script?

  • I know it's the job of init (see man init). Maybe you'll get some insight by looking at it's source code. – solarc Jun 28 '12 at 2:15
6

Depending on your Linux distro and release, ultimately sysvinit scripts (those scripts in /etc/init.d/) are generally run from the symlinks which exist in /etc/rc[0-6S].d, by /etc/init.d/rc.

Under Ubuntu, you're either using old-style sysvinit, or more recently, upstart. Under the upstart management directory (/etc/events.d/) you'll find a legacy mode that falls back to the /etc/init.d/rc invocation. Otherwise, /etc/init.d/rc is invoked for each runlevel via /etc/inittab.

If you examine the logic of /etc/init.d/rc, you'll find it defines actions depending on runlevel (0 & S are unconditionally stopped) or script prefix (S[0-9][0-9]* scripts start, K[0-9][0-9]* (kill) scripts stop). The numbering of scripts within a runlevel directory (e.g.: /etc/rc1.d/) determines the order in which scripts are stopped or started. Kill scripts are run first, then start scripts.

For more on this, research sysvinit and upstart.

| improve this answer | |
3

I suggest you read some tutorial on how runlevels and init scripts work - http://www.debian-administration.org/article/212/An_introduction_to_run-levels seems quite understandable.

In short, the scripts are not called directly, but through symlinks in the /etc/rc.d directories, where n = runlevel. The symlink's names are formatted like this:

[K | S] + nn + [string] 

where nn is a number marking the order in which the scripts are run (lower numbers first) and K or S determines whether to run the script with the "stop" or "start" parameter.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.