Using /etc/rc.local file to execute commands at system boot
While working on Linux systems, we may often have a requirement to perform a task on system boot but we may not like to undergo the whole process to setting up complex init scripts for the task under consideration. Under such circumstances using /etc/rc.local may prove to be useful and in fact just what we may need. In this article, we will explain briefly how the /etc/rc.local comes into play during the Linux boot process, scenarios where you may want to use it or avoid using it and finally we’ll show you an example of how to make use of it.
How does /etc/rc.local work?
A run level is a state of the system, indicating whether it is in the process of booting or rebooting or shutting down, or in single-user mode, or running normally. The traditional init program handles these actions by switching to the corresponding run level. Under Linux, the run levels are by convention: S while booting, 0 while shutting down, 6 while rebooting, 1 in single-user mode and 2 through 5 in normal operation. Runlevels 2 through 5 are known as multiuser runlevels since they allow multiple users to log in, unlike run level 1 which is intended for only the system administrator.
Once init loads, it first runs the /etc/rc.d/rc.sysinit script, which sets the environment path, starts the swap, checks the file systems, and executes all other steps required for system initialization and then it will read it’s configuration file /etc/inittab.
This file specifies what is required and how the system should be configured. Initially, the system starts in single user mode (run level S or run level 1). In single user mode, we configure everything that is required for an absolutely basic system. Following this, the system loads the default run level defined in the /etc/inittab file. When the runlevel changes, init runs rc scripts (on systems with a traditional init — there are alternatives, such as Upstart and Systemd). These rc scripts typically start and stop system services, and are provided by the distribution.
The script /etc/rc.local is for use by the system administrator. It is traditionally executed after all the normal system services are started, at the end of the process of switching to a multiuser run level. You might use it to start a custom service, for example, a server that’s installed in /usr/local. Adding commands to the bottom of this script is an easy way to perform necessary tasks like starting special services or initialize devices without writing complex initialization scripts in the /etc/rc.d/init.d/ directory and creating symbolic links. Most installations don’t need /etc/rc.local, it’s provided for the minority of cases where it’s needed.
When to avoid using /etc/rc.local
Starting application services:
The rc.local file might start up your application just fine during system boot but when the system is shutting down the init process will not stop the application gracefully since there is no stop script in place. If your application uses lock files this may mean your application doesn’t start on the next boot. It is therefore highly recommended to create init scripts when it comes to application services.
For tasks like network configuration and adding or removing firewall rules, you should not use /etc/rc.local because it is not the correct location for such entries. The system would have specific locations where configuration files for the network and firewall should reside which is generally under /etc/sysconfig directory.
When to use /etc/rc.local
If the task involves simply running a script or command which does not try to tamper with the system’s network configuration or application service configuration then that task/command should be considered for addition into /etc/rc.local.
To demonstrate how to make use of the /etc/rc.local file, we will use a Centos 6 system and put a command to create a directory and a script in the file as shown in the below sample /etc/rc.local file. Always use complete path when keeping commands in /etc/rc.local so that it will not fail if PATH variable is not consulted.
[root@arkit-centos ~]# cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local mkdir /tmp/sahil_$(date +%d-%m-%y) /home/sahil/test.bash [root@arkit-centos ~]# [root@arkit-centos ~]#
The script /home/Sahil/test.bash is a simple script that contains the following content
[root@arkit-centos ~]# cat /home/sahil/test.bash #!/bin/bash echo "This is a test script" >> /tmp/sahil
After we reboot the system, the directory specified should have been created and the script /home/Sahil/test.bash would have executed resulting in a file /tmp/Sahil having the content “This is a test script”.
Once the system came up after the restart I was able to confirm the creation of the file as well as the directory.
[root@arkit-centos ~]# cat /tmp/sahil This is a test script [root@arkit-centos ~]# ls -ld /tmp/sahil_01-02-18/ drwxr-xr-x. 2 root root 4096 Feb 1 19:16 /tmp/sahil_01-02-18/ [root@arkit-centos ~]#
In this article, we discussed exactly what is the /etc/rc.local file and how and when to make use of it with the help of a couple of examples. We hope that you found this article to be helpful and we look forward towards your feedback.
HowTo Increase XFS File System LVM in Linux
Understanding Different Kernel Types
Thanks for your wonderful Support and Encouragement