Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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