# /etc/rc.d/ versus /usr/local/etc/rc.d/



## rambetter (Aug 25, 2009)

I have a custom script in /etc/rc.d/ that waits for the network to become operational (or 100 seconds, whichever comes first) before continuing with init.  It has "REQUIRE: NETWORKING" and "BEFORE: named".

I feel that my script belongs in /usr/local/etc/rc.d/ instead of in /etc/rc.d/.  Am I correct?  Will my startup scripts still get run in the same order if I move it to /usr/local/etc/rc.d/ ?


----------



## tangram (Aug 25, 2009)

rambetter said:
			
		

> I have a custom script in /etc/rc.d/ that waits for the network to become operational (or 100 seconds, whichever comes first) before continuing with init.  It has "REQUIRE: NETWORKING" and "BEFORE: named".
> 
> I feel that my script belongs in /usr/local/etc/rc.d/ instead of in /etc/rc.d/.  Am I correct?  Will my startup scripts still get run in the same order if I move it to /usr/local/etc/rc.d/ ?



/etc/rc.d keeps the resource configuration scripts for FreeBSD. These scripts are designed to handle the launch of infrastructural services and daemons and should not be "polluted" with customized scripts.

Place startup scripts that you write under /usr/local/etc/rc.d.

init(8) runs the contents of /usr/local/etc/rc.d after it finishes running all of the scripts in /etc/rc.d.


----------



## SirDice (Aug 25, 2009)

You can check the order of the startup scripts using rcorder(8).


----------



## phoenix (Aug 25, 2009)

tangram said:
			
		

> /etc/rc.d keeps the resource configuration scripts for FreeBSD. These scripts are designed to handle the launch of infrastructural services and daemons and should not be "polluted" with customized scripts.
> 
> Place startup scripts that you write under /usr/local/etc/rc.d.
> 
> init(8) runs the contents of /usr/local/etc/rc.d after it finishes running all of the scripts in /etc/rc.d.



No, it doesn't.  The order that scripts are run depends entirely on the contents of the scripts (the REQUIRE, BEFORE, and AFTER lines), *not* their location on disk.  Please read the rcorder(8) man page for all the details.

You can see the order that the scripts are run via:
`$ rcorder /etc/rc.d/* /usr/local/etc/rc.d/*`

Back in the old day (FreeBSD 4.x and earlier), the contents of /etc/rc.d/ were run first, and then the contents of /usr/local/etc/rc.d/ were run.  And they were run in alphabetical order.  But that changed with FreeBSD 5.0 when RCng was brought over from NetBSD.


----------



## tangram (Aug 25, 2009)

According to rc(8):

```
6.   Invoke rcorder(8) to order the files in /etc/rc.d/ that do not have
	  a ``nostart'' KEYWORD (refer to rcorder(8)'s -s flag).

     7.   Call each script in turn using run_rc_script() (from rc.subr(8)),
	  which sets $1 to ``start'', and sources the script in a subshell.
	  If the script has a .sh suffix then it is sourced directly into the
	  current shell.  Stop processing when the script that is the value of
	  the $early_late_divider has been run.

     8.   Re-run rcorder(8), this time including the scripts in the
	  $local_startup directories.  Ignore everything up to the
	  $early_late_divider, then start executing the scripts as described
	  above.
```

And,

```
# grep local_startup /etc/defaults/rc.conf
local_startup="/usr/local/etc/rc.d" # startup script dirs.
```

Leads me to conclude that /etc/rc.d contents are executed before /usr/local/etc/rc.d.


----------



## DutchDaemon (Aug 25, 2009)

Running rcorder does show otherwise, _unless_ it really gets executed thusly:


```
rcorder /etc/rc.d/*
execute /etc/rc.d/* in correct order
rcorder /usr/local/etc/rc.d/*
execute /usr/local/etc/rc.d/* in correct order
```

Running [cmd=]rcorder /usr/local/etc/rc.d/*[/cmd] on its own does lead to a lot of errors, though.


----------



## SirDice (Aug 25, 2009)

tangram said:
			
		

> Leads me to conclude that /etc/rc.d contents are executed before /usr/local/etc/rc.d.


Read the last line of step 7 

So some parts of /etc/rc.d get done first but once $early_late_divider is detected, it'll be a mix of /etc/rc.d/ and /usr/local/etc/rc.d (depending on their rcorder).


----------



## tangram (Aug 25, 2009)

SirDice said:
			
		

> Read the last line of step 7
> 
> So some parts of /etc/rc.d get done first but once $early_late_divider is detected, it'll be a mix of /etc/rc.d/ and /usr/local/etc/rc.d (depending on their rcorder).



Well stopped


----------



## rambetter (Aug 25, 2009)

Thank you, this makes sense now.

So just to make sure I know what's going on,

In /etc/defaults/rc.conf, early_late_divider="FILESYSTEMS".
I do an "rcorder /etc/rc.d/*" which shows that FILESYSTEMS precedes NETWORKING.

My script in /usr/locaal/etc/rc.d/ has "# REQUIRE: NETWORKING".

When I run "rcorder /etc/rc.d/* /usr/local/etc/rc.d/*" I get my script listed immediately after NETWORKING.

So I am concluding that during bootup, my script /usr/local/etc/rc.d/waitfornetwork will get run immediately following NETWORKING.


----------



## phoenix (Aug 25, 2009)

Correct.


----------

