Linux Tricks and Techniques

For Linux Experts

Use the website for easy leaning of Linux

Sunday, 12 November 2017

How to execute a script at boot using cron in Linux

This article is about to execute a script created by a system admin during reboot of a Linux server. We all know about the cronjob utility of Linux. Cron male this possible for a system admin to execute a script while the system boot. The script may contain any commands outputs or other monitoring codes , all it depends upon the requirement of your setup . Here I am sharing one of the scripts I created and will show you the output of the same once the server is up. This script contains the output of a simple and basic command of Linux i.e “df-h “ and will get automatically save at the desired path of the directory.

In the above image the script will automatically create a file file.txt which will contain the output of df-h command at boot. The permission for the script is 777 you can also set it 755 for root user. Let me show you the script content of
So the above image is the script written in . 

This script will take the output of df -h and will save it in a file file.txt in /root directory. In the previous image you can see that there is no file.txt file exists but once I will reboot the server it will create file.txt in /root directory because this path is given in the script.
Reboot the server and once the server is up check /root directory a file named file.txt would have been created which contains the df –h output of your server.
You can see that a file is been created by name file.txt . Let me show you the output of this command.

File.txt will give you output of df -h during boot. The next important task to execute this script at boot is to make this entry in cronjob in your Linux server. Contab is the utility which allow you to execute the scripts at the desired time. For this script we need to add @reboot in the crontab entry to make this possible for this script execute at boot. The crontab entry is mentioned below.
Also please check the status of cron service and it should be running and keep the cron on at boot as mentioned below.


No comments:
Write comments