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.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of 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.

8 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.

    • What is so confusing about 'The following is an init.d script template for a hypothetical service called foobar. '
      This is just a template for a HYPOTHETICAL service.
      You can put in whatever you want for the case decisions. ...

      But anyways, thanks for the article! ;)

  3. this is just an example script. you put your own. don't worry about the example contents. it will work with yours no matter what's in it.

Leave a comment

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