Init Scripts

This page explains how to create init scripts.

/etc/init.d/example

Code
#!/bin/sh /etc/rc.common
# Example script
# Copyright (C) 2007 OpenWrt.org
 
START=10
STOP=15
 
start() {        
        echo start
        # commands to launch application
}                 
 
stop() {          
        echo stop
        # commands to kill application 
}

What this means:

  • START=10 - this means the file will be symlinked as /etc/rc.d/S10example - in other words, it will start after the init scripts with START=9 and below, but before START=11 and above.
  • STOP=15 - this means the file will be symlinked as /etc/rc.d/K15example - this means it will be stopped after the init scripts with STOP=14 and below, but before STOP=16 and above. This is optional.
  • start() - these commands will be run when it is called with 'start' or 'boot' as its parameter.
  • stop() - these commands will be run when it is called with 'stop' as its parameter.

If you add a section like the one below:

Code
boot() {
        echo boot
        # commands to run on boot
}

… then these commands will be run on boot, instead of those in the start() section. This is handy for things that need to be done on boot, but not every time the program it calls restarts.

You can add your own custom commands by using the EXTRA_COMMANDS variable, and provide help for those commands with the EXTRA_HELP variable, then adding sections for each of your custom commands:

Code
EXTRA_COMMANDS="custom"
EXTRA_HELP="        custom  Help for the custom command"
 
custom() {
        echo "custom command"
        # do your custom stuff
}

If you run the script with this code added, with no parameters, this is what you'll see:

Shell Output
root@OpenWrt:/# /etc/init.d/example
Syntax: /etc/init.d/example [command]

Available commands:
        start   Start the service
        stop    Stop the service
        restart Restart the service
        reload  Reload configuration files (or restart if that fails)
        enable  Enable service autostart
        disable Disable service autostart
        custom  Help for the custom command

If you have multiple custom commands to add, you can use here documents to add help text for each of them:

Code
EXTRA_COMMANDS="custom1 custom2 custom3"
EXTRA_HELP=<<EOF
        custom1 Help for the custom1 command
        custom2 Help for the custom2 command
        custom3 Help for the custom3 command
EOF
 
custom1 () {
        echo "custom1"
        # do the stuff for custom1
}
custom2 () {
        echo "custom2"
        # do the stuff for custom2
}
custom3 () {
        echo "custom3"
        # do the stuff for custom3
}

Enable and disable

:!: Dont forget to chmod +x /etc/init.d/example!

In order to automatically start the init script on boot, it must be installed into /etc/rc.d/ .

Invoke the "enable" command to run the initscript on boot:

Shell Output
root@OpenWrt:/# /etc/init.d/example enable

To disable the script, use the "disable" command:

Shell Output
root@OpenWrt:/# /etc/init.d/example disable

The current state can be queried with the "enabled" command:

Shell Output
root@OpenWrt:/# /etc/init.d/example enabled && echo on
on

Back to top

doc/techref/initscripts.txt · Last modified: 2010/09/02 16:55 by zajec