How to automatically start a program on boot in Debian 7 (wheezy)

Last updated on November 17, 2020 by Dan Nanni

When you are running Debian 7 (wheezy) or earlier, and want to configure your Debian system to start a certain program automatically on boot, you can leverage Debian's init system. As the first process to start upon kernel boot, the init system controls various startup services at different runlevels.

This tutorial describes how to configure a program to auto-start on Debian 7 or earlier (e.g., Debian wheezy, squeeze, etc).

In Debian 7 or earlier, 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

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean