Running a Program at Linux Startup

From MobileRobots Research and Academic Customer Support

Jump to: navigation, search

When the Debian GNU/Linux operating system is finished booting the Linux kernel, it begins running various init scripts to start background services, restore state from shutdown, etc. You can add your own calls or scripts to the init process to run your own software automatically at startup.

For example, if you have a Seekur or Seekur Jr. with some extra devices on the power distribution board (PDB), you can add an invocation of the /usr/local/Aria/examples/seekurPower program (you may have to complile this example) to change the state of switched devices connected to the PDB on startup as well.

You can also run a robot control program or server, such as arnlServer from ARNL (see note below).

It is important that any program run at system boot run as a background process (service or daemon), otherwise no following processes will be run by the init system. There are two ways that a process becomes a background process: the program itself may do this automatically (by forking), or you can use the shell to manually run it in the backround. How to do this is described in more detail in Running a Linux Program in the Background.

Note: Some of the ARIA and ARNL examples in versions 2.7/1.7 and earlier, including serverDemo, arnlServer, mogsServer and sonarnlServer create an ArKeyHandler object to read input from the keyboard; this must be removed in order to run the program in the background. Remove or comment out creation and any use of the ArKeyHandler object, then recompile the program. The key handler is not neccesary to use these programs (you can use Control-C or Linux kill command to end the program rather than the escape key).

Debian 5.x

In Debian 5.x a script intended for "local" user modifications is /etc/rc.local, which is run near the end of the init process. This script is always run at system startup (in any runlevel mode). You can simply add commands to this script.

Debian 3.1

In Debian 3.1, MobileRobots has added a script to /etc/init.d called MobileRobots for our own customizations, which can be modified for yours as well, by checking for script arguments of "start", "stop" or "restart".

/etc/init.d

Another option is to create a new script in /etc/init.d and add symlinks to that script in each of /etc/rc0.d (for shutdown), /etc/rc2.d and /etc/rc3.d (for startup in runlevel 2 or 3). Prefix the symlink names with S (for startup) or K (for shutdown) and a number that places it near the end of the order of the other scripts.

Do this if you want to only run in runlevel 2 or 3, or be able to manually start/stop/restart it from the command line, or if you are packaging software for redistribution and installation on other hosts.


An example local custom init script code for use in /etc/init.d) follows which runs myServer in the background.

case "$1" in
  start) 
    echo -n "Starting server: "
    # Run myServer in the backround, with both stdout and stderr saved in /var/log/myServer.log, and use nohup to protect against HUP signals.
    nohup /usr/local/Arnl/examples/myServer  >/var/log/myServer.log 2>&1 &
    echo "myServer"
    ;;
  stop)
    echo -n "Stopping all myServer processes: "
    killall myServer
    echo "myServer"
    ;;
  restart)
    # Re-run this script with stop and start arguments.
    $0 stop
    sleep 5
    $0 start 
    ;;
  reload|force-reload)
    echo "WARNING reload and force-reload not supported by this script
esac
Personal tools