4

I played around with LSB-compatible init-scripts under Debian Wheezy (init is from sysvinit package version 2.88dsf-41+deb7u1). My script in the /etc/init.d directory is as follows:

root@T60:~# ls -l /etc/init.d/test-script
-rwxr-xr-x 1 root root 811 Aug 10 03:18 /etc/init.d/test-script
root@T60:~# cat /etc/init.d/test-script
#! /bin/sh

### BEGIN INIT INFO
# Provides:          test
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: test script
# Description:       test script
### END INIT INFO

# always executes
touch /var/test-file

case "$1" in
  start)
    echo "Starting script test"
    touch /var/test-file-start
    ;;
  stop)
    echo "Stopping script test"
    touch /var/test-file-stop
    ;;
  restart)
    echo "Restarting script test"
    touch /var/test-file-restart
    ;;
  force-reload)
    echo "Force-reloading script test"
    touch /var/test-file-force-reload
    ;;
  status)
    echo "Status of test"
    touch /var/test-file-status
    ;;
  *)
    echo "Usage: /etc/init.d/test-script {start|stop}"
    exit 1
    ;;
esac

exit 0
root@T60:~# 

As a next step I added symlink to /etc/rc3.d directory:

root@T60:~# file /etc/rc3.d/S05test-script
/etc/rc3.d/S05test-script: symbolic link to `../init.d/test-script'
root@T60:~# 

Now if I changed my runlevel from 2 to 3 I expected script to be executed, but this did not happen:

root@T60:~# who -r
         run-level 2  2014-08-01 20:47                   last=S
root@T60:~# init 3
root@T60:~# who -r
         run-level 3  2014-08-10 03:27                   last=2
root@T60:~# ls -l /var/test*
ls: cannot access /var/test*: No such file or directory
root@T60:~# 

I am aware that I could use insserv or update-rc.d to install scripts and then they will work as expected. Am I correct that both insserv and update-rc.d take some additional steps when enabling the script besides installing the symlinks? If yes, then which steps?

  • Assuming you didn't create symlink /etc/rc2.d/S??test-script, your script should have run. To debug this, switch back to runlevel 2, add set -x as the second line of /etc/init.d/rc, and switch back to runlevel 3. Post the complete trace. – Gilles Aug 10 '14 at 22:12
1

You can automatically create the /etc/rcX symlinks using the update-rc.d command. Here is some more information about update-rc.d.

In your particular example, this should do it:

update-rc.d test-script defaults

To remove it later, use:

update-rc.d -f test-script remove
0

did you make the link in rc2.d (same link as in rc3.d) as well ?

Basically, when going from run level X to run level Y,

  • all file in rcX.d starting with K (kill) will be run in alphabetical order with stop as unique arguement
  • all file in rcY.d starting witk S (start) will be run in alphabeical order with start as unique argument
  • I did not make a symlink to /etc/rc2.d. – Martin Aug 10 '14 at 16:37
  • @Martin If you'd made a symlink /etc/rc2.d/S??test-script, then the start action would have been skipped on a 2→3 transition, on the assumption that the service was already running. If you haven't then your service should have run. – Gilles Aug 10 '14 at 22:10

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.