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
  start)
    echo "Starting foobar"
    echo "foobar is alive"
    ;;
  stop)
    echo "Stopping foobar"
    echo "foobar is dead"
    ;;
  *)
    echo "Usage: /etc/init.d/foobar {start|stop}"
    exit 1
    ;;
esac

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.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

5 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 :-)

  2. What part of the script actually starts the program? I don't see anything that would start it, just a ton of echo commands.

Leave a comment

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

Current ye@r *