Can I call /sbin/init from init script

Let us say I have a custom init like this

#!/bin/bash
sleep infinity

Which of these will load init

  1. #!/bin/bash
    /sbin/init
    sleep infinity
    
  2. #!/bin/bash
    exec /sbin/init
    sleep infinity
    

I know that exec is supposed to start a new shell but is it necessary?

Asked By: Bret Joseph

||
  1. It is actually very normal to invoke init from a script. Common bootloader scripts will properly mount the root disk and then run init.

  2. To invoke init as init, and not as telinit, it must be run as PID 1. Thus you need the exec. Thus, only your (2) script is potentially useful to run init.

  3. After an exec, the script is no longer running. Thus, the sleep infinity will not run. Anything run before the exec can be quite useful.

  4. If you just want something else running, you might want to just list it appropriately in /etc/inittab, or a script invoked from there (typically placed in /etc/init.d and symlinked into /etc/rc#.d).

Answered By: David G.
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.