Let’s say we need to run a shell script exactly at 8:00 PM every day., shell script is a file that contains a series of commands. Rather than keeping an alarm at 8:00 a.m. every day, logging into the server, and saying hello.sh ., which is difficult, we can automate it using CronTab.
CronTab
Crontab stands for Cron Table. It is a configuration file used in Unix-like operating systems to schedule and automate repetitive tasks at specific times or intervals using the cron daemon. The crontab file contains a list of commands to be executed automatically and the schedule specifying when to run each command.
For crontab to run we need to have crond.service running., we can check the service status using the command - systemctl status cron
We have a list of commands like this,
crontab -l - which is used to list all the cronjobs
crontab -e - we can edit our cronjobs
crontab -r - we can remove cronjobs
By default all the users have crontab access like normal user and the root user., if we leave it like this each user will create a job so server resources will be utilized more., to restrict crontab access to all the users we need to create a file called as cron.allow in etc directory.,
#touch /etc/cron.allow à as a normal user we cannot create a file in the etc directory
Once we do this now Ubuntu user can configure the crontab, right now ubuntu user don’t have any crontab configured so we have something like these.,
let’s say we have many users on our server and we need to see the jobs of the other user., by default crontab will not allow us to view other user crontabs unless we have sudo access, if we have root privileges we can see it using this command
sudo crontab -u ec2-user -l
NOTE: To give the sudo privileges to a normal user we need to make an entry in the /etc/sudoers file like this - ubuntu ALL=(ALL) ALL ., as a normal user we cannot access etc directory so root user only can make an entry in the sudoers file.
We have a simple shell script like this.,
We need to run this shell script every minute ., rather than doing sh hello.sh every minute we can leverage this crontab. We will see the crontab structure now
Crontab has got 6 fields - minutes, hours, days of a month, month, week, command
This is the crontab format and we need to place the crontab expression by doing crontab -e and it will open in a vi editor.
Let’s say we are required to run the script every Monday at 7 PM then our expression should be
0 19 * Mon * /home/ubuntu/hello.sh
So this expression will run the file hello.sh every Monday at 7 PM.
When we run a script it will give an output and if we need to see the output we can redirect the output to a file using this,
0 19 * Mon * /home/ubuntu/hello.sh > /tmp/output.txt
So here whatever output the script file generates will get stored in the output.txt ., and here > will do the redirection, by default > redirects only the standard output which means only if the script generates an output it will be pushed to output.txt in case of error it will not send to the output.txt file.
To test it we will give the crontab to run every minute and we will redirect the output to output.txt file in the tmp directory.
1 * * * * /home/ubuntu/hello.sh > /tmp/output.txt → This is the expression to run the script file every minute.
We can see the list of crontabs using the crontab -l command., one more important thing is to have the execute permissions for the hello.sh , right now we don’t have execute permissions
To give the execute permissions we need to give this , chmod +x hello.sh , here we are giving execute permissions for all of them ( User, Groups, and Others )
Once we change the permissions then we can see a output.txt being present in the user home directory.
If there is any errors or if crontab is not running properly we can check the logs in /var/log/syslog in the case of ubuntu server. So the important thing here is to /etc/cron.allow file to give the users to run crontab and if there is any error then we can check the logs present in the /var/log/syslog, and systemctl status cron to see the status of the crontab.
We can also do append as well using >>, if we use the append then the new output gets added to the older output.
It is something like this, but in the above picture we manually run the shell script but we can also use >> , we can have like this.,
1 * * * * /home/ubuntu/date.sh >> /home/ubuntu/output.txt
Here it will run the script for every one minute and the output gets appended to the output.txt file., we can also use one more thing here we know > only redirects the standard output but not the standard error, so in case if there is any issue with the shellscript then if we need to redirect that error to the file we can also do it,
1 * * * * /home/ubuntu/date.sh >> /home/ubuntu/output.txt 2>&1
In the above command even if date.sh executed successfully or if there is any error in the shellscript in both of these cases it will redirect to the output.txt file. It is best practice in case if our shell script is giving any error we can debug the error by looking at output.txt file.
So we can use crontab to run a job or to perform any automation without manual intervention.