Auto-Initialization Scripts in Linux

In many situations, it is desirable to start certain programs or softwares automatically while a computer boots up. In this article we will see few ways we can achieve this objective using some settings in Linux systems.

The Bashrc method

This is one of the easiest methods to load a program or script while a Linux system boots up. All we need to do is simply put the bash command to load the program in the “.bashrc” file. The bashrc file exists in the root location and it is hidden. Inorder to open the file and edit we can do it in the command line itself using the following command.

$sudo nano ~/.bashrc

The above command will open up the bashrc file using the nano editor and with admin rights. Now we only needs to add command which needs to be run the script/program to this file. For example, if you have a Python script to greet you with a welcome message everytime you boots up the system, it can be added to the bashrc as follows.

python greetings.py

Once the changes made to the bashrc is saved, the script/command added will run everytime the device boots up. This method has few downsides also. The bashrc also runs on few other occasions. Which are the following, when the terminal window is opened and when someone remotely logs to the system using SSH. Also the user has to be logged in is another requirement. Because of these reasons it may not be the exact choice you would want to impliment.

Crontab Method

Another method is to add the command to be executed to crontab system. This can be done using the following commands.

$sudo crontab -e
$@reboot sudo python greeting.py

Similar to the previous method, here also we will be adding the script to be run to the crontab settings. Compared to previous method this has no problems such as unwanted initialization of the script while someone remotely login or terminal window is opened.

ScriptService

It is also possible to start programms using the script service. To do that we need to create a script-service file, change its file permissions and enable the script service. All of these actions can be done in the command line itself. The following commands show how to do that.

$sudo nano /etc/systemd/system/ScriptService.service
$sudo chmod 644 /etc/systemd/system/ScriptSerive.service
$systemctl enable ScriptService.service

Please note that we will have to create separate script service files for each of the programs we want load on startup.

rc.local

Another method is to add the python script to an rc.local file. First we will create an rc.local file. Then add the script to run into the file. Final step is to change the file permissions. The following commands show how to do that.

$sudo nano /etc/rc.local

# Now add the script to run inside the file
$python greetings.py

#Now set suitable file permissions
$sudo chmod +x /etc/rc.local

All of the above documented cases are valid methods for loading a program/software/script while a Linux system boots up. Some of them requires the user to log in to enable the process. The automatic user login can be enabled for achieving the results. Depending on the application, a user can select one of these methods considering the pros and cons of each of the methods.

1 thought on “Auto-Initialization Scripts in Linux”

Leave a Comment

Your email address will not be published. Required fields are marked *