In this tutorial, I will be talking about how to get your Linux server (I will be using Ubuntu but it should work fine on most distros)
So sometimes you will just want your server to just boot up and do a task on startup without user interaction, this can be anything, to tell a monitoring service that the server is online, to run a simple clean up script or to run a complex bootstrap.
As you may know, a lot of services(such as Apache httpd, MySQL and Nginx) have their own startup(init) scripts that can be configured to run at any of the runtime levels using the chkconfig command in RedHat based distros or update-rc.d in Debian based distros, but to configure this will take a lot of time an work.
What we will be doing is creating a simple script and running it in /etc/rc.local this file gets executed after all normal system services such as networking and Apache or MySQL so this is one of the last things to run before it runs user-specific login tasks/scripts such as changing terminal colours or opening something like firefox on login.
With that out the way, let’s get started!
Linux server already set up and working
A script that you need running at start up
SSH and login as root to the Linux box
If you do not know the root password you can log in as user you know the password to then type the following
This allows you to log in as root with your password
Editing the rc.local file
You may need to copy your scripts to the server(out of scope of this tutorial) you should be able to use the scp command
Once you are root you can edit the /etc/rc.local file by entering the following.
You can also use vi is you like
You will see a file like this
#!/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. exit 0
The most important thing to note here is the exit 0 as this tells Linux that the script as completed successfully.
Note: If your script is a long-running or never-ending task you will need to add a “&” to the end of your script as this will tell bash to run it in the background or in another process and give the signal that the script was successful in running. If the “&” is not added you may find that your Linux box will seem to either take a long time in booting or not boot at all well, in fact, it has booted but it is stuck on your script.
#!/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. /path/to/script/counter.sh /path/to/output.txt exit 0
I have just added one line (that is highlighted in red) that goes to a script that just counts in an infinite loop and outputs the text to a file. As you can see I have added the “&“ you can also use something like this.
/path/to/script/counter.sh >> /path/to/output.txt || exit 1
To test that it has worked all you need to do is restart the server.
We have briefly gone over how Linux services startup during boot and showed you how to use a basic but effective way to run your scripts at startup using rc.local