Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to run a script in runlevel 1 out of /etc/inittab. The script I want to run looks like:

#!/bin/bash
while [ 1 ]
do
  #do something
  sleep 1
  #do something
  sleep 1
done

The inittab line looks like this: d666:123456:respawn:/root/script_to_run.sh

In runlevel 2-6 everything works fine. When I change into runlevel 1 (init 1) the script gets stopped and isn't executed anymore.

Also I'm trying to handle a script to be called right before runlevel 0 is entered. I tried this line in inittab: d667:0:wait:/root/script_to_run_right_before_runlevel_0.sh

share|improve this question
    
Which init system, and/or which Linux distribution? What other runlevel 1 entries are in your /etc/inittab? –  mr.spuratic Feb 11 '13 at 13:30
    
It's Debian GNU/Linux 5.0 debarm in an embedded system. One more entry for runlevel 1 is l1:1:wait:/etc/init.d/rc 1 –  arminb Feb 11 '13 at 13:51
add comment

1 Answer 1

Debian documentation isn't explicit, init(8) cautions only that runlevels S,0,1,6 are reserved, and also:

On a Debian system, entering runlevel 1 causes all processes to be killed except for kernel threads and the script that does the killing and other processes in its session. As a consequence of this, it isn’t safe to return from runlevel 1 to a multi-user runlevel: daemons that were started in runlevel S and are needed for normal operation are no longer running. The system should be rebooted.

Run level 1 in /etc/inittab is:

l1:1:wait:/etc/init.d/rc 1

/etc/init.d/rc 1 will call /etc/rc1.d/S* including S01killprocs which kills most things it can find, and S21single, which performs "exec init -t1 S", to switch to single-user mode, so runlevel 1 is very short-lived. Single-user mode "S" in /etc/inittab is:

~~:S:wait:/sbin/sulogin

which means that init will simply wait until wait until sulogin returns before doing anything else.

In short, runlevels "1" and "S" are "hands-off" in Debian (and probably most other unixen too).

If you put your inittab entry above the system "S" entry, then init respawning and the S01killprocs script will fight it out for a while (you may not get to observe that without a running syslog), which is probably racy, and probably won't do what you want.

You may be able to some of what you need by either or both of modifying the startup scripts, and implementing an /etc/initscipt to monitor and log the various actions of init. These are a really good way to hose a working system, so I suggest experimenting in a vm first ;-).

I think your other options, neither of which seem very appealing, are to try a different init, or see if you can do what you want via a kernel thread.

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.