Understanding Linux Cronjobs?
Cronjob is a scheduled task that is defined in crontab. They are able to trigger automatically at a specified time. They are beneficial for housekeeping tasks in Linux such as restarting services, cleaning logs or unwanted data, having database backups, automatic certificate updates, and many more.
Who Plays Behind the Cronjobs?(crond.service)
The crond.service aka cron daemon plays behind the scene and keep monitoring the /etc/crontab file
, and /etc/cron.*/
directories and /var/spool/cron/
crond.service by default coming as enabled but if it is disabled you can enable it by,
sudo systemctl enable crond.service
sudo systemctl start crond.service
Understanding Cronjob Structure
A cronjob can be written as a rule with 6 parts. Each part is separated by a blank space. The first five slots are allocated to set the time for the cronjob.
For example, let’s say you need to execute a script called “test.sh” at 10.30 am everyday,
30 10 * * * cd /home/randula/scripts/ && ./test.sh
Wild Cards and Special Characters
There are several wild cards and special characters used in cronjobs. The following table is a simplified summary of symbols used in cronjobs in Linux.
Crontab Example
Let’s have a look into sample cronjobs that we need to use in most cases when you managing a production server.
Daily Cronjobs Use Cases
shutdown the instance at 12.00 a.m00 00 * * * sudo /sbin/shutdown -h now
Shutdown the instance if it on between 12 am- 7a.m00 0–7 * * * sudo /sbin/shutdown -h now
Execute a script daily and save output00 5 * * * cd /home/randula/scripts/ && ./test.sh >> ~/tmp/test.output
Clean logs older than 5 days10 3 * * * find /home/randula/log -name “*.log*” -type f -mtime +5 -exec rm -f{} \;
Execute a script hourly0 * * * * cd /home/randula/scripts/ && ./test.sh
Weekly Cronjobs Use cases
Execute a script on every Sunday00 11 * * 0 cd /home/randula/scripts/ && ./test.sh
Execute a script on every weekday00 11 * * 1–5 cd /home/randula/scripts/ && ./test.sh
Exercise
Try to write time patterns for the following scenarios
- Every 30 minutes
2.Every hour
3.Every 2 hours
4.Every day at 2:45 PM
5.Every Thursday at 2:45 PM
6.13th of Every month
7.2:40 AM the third Saturday of every month
8.2:40 AM every Monday, Tuesday, Wednesday, Thursday and Friday
9.At 2:10 p.m. and at 2:44 p.m. every Wednesday in March.
Answers
1.Every 30 minutes*/30 * * * *
2.Every hour0 */1 * * *
3.Every 2 hours0 */2 * * *
4.Every day at 2:45 PM45 14 * * *
5.Every Thursday at 2:45 PM45 14 * * 4
45 14 * * THU
6. 13 th of Every month* * 13 * *
7. 2:40 AM the third Saturday of every monthNot possible
8. 2:40 AM every Monday, Tuesday, Wednesday, Thursday and Friday40 2 * * 1–6
9. At 2:10 p.m. and at 2:44 p.m. every Wednesday in March10,44 14 * 3 3
I found this tool, Crontab guru very useful to test and create cronjobs very easily.
How to Create or Edit a New Cronjob?
To start writing a new cronjob you can simply write
crontab -e
You can press ‘i’ to start editing the cronjob.
Once you have done adding the cronjob, you can save and exit the cronjob as same as you do in your editor.
ESC
:qw
Press Enter
If the crontab service is not activated, please start the service using,
sudo systemctl enable crond.service
sudo systemctl start crond.service
Red Hat Error : You are not allowed to use this program error RHEL
On Linux there are two files that control the access to crontab:
/etc/cron.allow
/etc/cron.deny
The above error occurs when your user is not allowed to for cronjobs. Hence you will need to add the login account to the /etc/cron.allow
file.
Solution
sudo -i -u root
echo YOUR_USER > /etc/cron.allow
example, if your user is randula,
sudo -i -u root
echo randula > /etc/cron.allow
this will fix the error.
Listing a cronjob
cronjob -l
AWS Cronjobs
Amazon AWS also provides similar functionality for cronjobs, call “schedule”. They canbe set up for a single run or run in sequences as same as cronjobs.
MigrationScheduleExpression:
Description: cron job or rate
Default: 'cron(0/3 13-1 ? * * *)'
Type: 'String'
The scheduler consists of 6 parts to describe the duration of the event, whereas we had 5 parts at Linux cronjobs.
There are several other differences between Linux and AWS cronjobs.
Not like Linux cronjobs, you can’t put * for both “day of the month” and “day of the week” fields. It is prohibited here. You may use “?” for one of them instead.
The time is always calculated in UTC hence you need to convert the time correctly when creating the schedule
The weekdays are starting from 0, where 0 is Monday. Linux cronjobs consider 0 as Sunday.