How to automatically start a program on boot in Debian

When you want to configure your Debian system so that it will start a certain program automatically on boot, you can leverage Debian's init system which controls various startup services at different runlevels.

In Debian, a directory called /etc/init.d contains a list of scripts that are executed by init process during startup and shutdown. Thus, in order to automatically run a particular program or script at startup, you can create a corresponding init.d script.

The following is an init.d script template for a hypothetical service called foobar. A typical init.d script gets executed with arguments such as "start", "stop", "restart", "pause", etc. In order for an init.d script to be started or stopped by init during startup and shutdown, the script needs to handle at least "start" and "stop" arguments.

$ sudo vi /etc/init.d/foobar
#! /bin/sh
# /etc/init.d/foobar

# The following part always gets executed.
echo "This part always gets executed"

# The following part carries out specific functions depending on arguments.
case "$1" in
    echo "Starting foobar"
    echo "foobar is alive"
    echo "Stopping foobar"
    echo "foobar is dead"
    echo "Usage: /etc/init.d/foobar {start|stop}"
    exit 1

exit 0

Finally, make the init.d script executable, and add the init.d script to a default runlevel, so that the script can be called at boot time (and also during shutdown).

$ sudo chmod 755 /etc/init.d/foobar
$ sudo update-rc.d foobar defaults

Later if you decide to remove the init.d script from start-up service list, you can simply run the following.

$ sudo update-rc.d -f foobar remove

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

2 thoughts on “How to automatically start a program on boot in Debian

  1. Just to simply start something after reboot, I recommend an easier way:
    Add a line: @reboot /path/to/script in /etc/crontab - this will do the same job :-)

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *