How to automatically run program on Linux startup
On Linux bootup or startup, it will first run the program init, and depending on how it is configured and the runlevel Linux is running, it will run other scripts which normally resides in the /etc/rc.d/ directory. Right before a user is presented with the login prompt, the /etc/rc.d/boot.local or /etc/rc.local script is executed.
By that we should have the idea that to execute our programs even before we’re given the login prompt is to add them to the /etc/rc.d directory, or simply edit the /etc/rc.d/boot.local or /etc/rc.local script and add few lines to make it execute our program. Running programs using the /etc/rc.d directory is actually more complicated than what I’ve mentioned so far, and so we’ll just proceed with the /etc/rc.d/boot.local or /etc/rc.local script.
In any given Linux system, only one of these 2 files actually exist. /etc/rc.local is normally used by Debian and it’s derivatives, and /etc/rc.d/boot.local used by the Redhat family of Linux distribution.
The /etc/rc.d/boot.local or /etc/rc.local scripts are actually Bash scripts, and the following is the default file for Ubuntu;
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing
In my case I use it to execute no-ip program, and I added the following line to the file to have it executed;
noip2 -c /var/lib/noip2/noip2.conf
That’s a very simple example to directly execute a program. Anyone with some Bash scripting knowledge can include conditional statements and whatever Bash has to offer if they need to.