Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
RHCSA - Deploy, Configure, & Maintain Systems: Schedule Tasks Using at & cron
Both cron
and at
are used for scheduling tasks to be executed at specific times or intervals. They are essential tools for automating various administrative and repetitive tasks.
cron
cron
is a time-based job scheduler allowing users to schedule tasks (commands or scripts) to run at specific intervals or times. These tasks can be set to run daily, weekly, monthly, or even at custom intervals. cron
uses a configuration file called the crontab
to define scheduled tasks and every user can have their own crontab
.
To access the crontab file you use the crontab
command with either the -e
option to edit the crontab or -l
option to list entries in the crontab to screen. by default it will open the crontab of the current user but root
or sudo
can access other user crontabs by specifying the -u <user>
option.
To remove a crontab file you use the crontab -r
command.
The -e
for edit and -r
for remove options are next to each other on the keyboard. Maybe one of your cron jobs could be making a backup of the cron itself (Trying to recreate it from memory is not fun).
The table below shows the fields, the allowed values, and their meanings for a crontab entry:
Field | Allowed Values | Meaning |
---|---|---|
Minute | 0-59 | Minute of the hour when the task should run (0-59) |
Hour | 0-23 | Hour of the day when the task should run (0-23) |
Day of Month | 1-31 | Day of the month when the task should run (1-31) |
Month | 1-12 or JAN-DEC | Month of the year when the task should run (1-12 or JAN-DEC) |
Day of Week | 0-6 or SUN-SAT | Day of the week when the task should run (0-6 or SUN-SAT) |
Command | Any valid command | The command that should be executed at the specified time |
An example user cron entry may look like:
# Run a backup script every day at 2:30 AM
30 2 * * * /path/to/backup_script.sh
# Send a daily email reminder at 8:15 AM
15 8 * * * echo "Don't forget your daily tasks!" | mail -s "Daily Reminder" user@example.com
# Generate a monthly report on the 1st of each month
0 0 1 * * /path/to/weekly_report.sh
# Clean up temporary files every Monday, Wednesday & Friday at 10 PM
0 22 * * 1,3,5 /path/to/cleanup_script.sh
# Update a Git repository Monday to Friday, in working hours, every 15 minutes
*/15 9-17 * * 1-5 cd /path/to/repo && git pull
# Send Christmas reminder to do shopping at 8AM on 24th Dec
0 8 24 12 * echo "Don't forget it is Christmas tomorrow!" | mail -s "Daily Reminder" user@example.com
The following exercises will get you working with cron:
Create a cron entry and observe it run:
Open your crontab file for editing:
crontab -e
Add an entry that will append the current date & time to a file every minute:
# Print date and time to file /tmp/cron-executed.log
* * * * * echo "$(date)" >> /tmp/cron-executed.log
Tail the log file /tmp/cron-executed.log
to see the date & time print to file every minute:
tail -f /tmp/cron-executed.log
Use Ctrl+C to stop the tail
command.
List your crontab file now that it has an entry present:
crontab -l
The crontab should display to screen:
# Print date and time to file /tmp/cron-executed.log
* * * * * echo "$(date)" >> /tmp/cron-executed.log
at
The at
command allows users to schedule tasks to run once at a specific time in the future. It is particularly useful for one-time tasks that need to be executed at a specific moment.
The at
command takes a time arguments, for example at 17:00
, and then drops you into the at
prompt where you specify the command you wish to run at that time. To submit the job, you then press Ctrl+D. It can also take relative time arguments such as at now + 1 minute
.
There are 2 other useful commands related to at
which are; atq
to show the list of scheduled jobs and atrm
to remove a scheduled job.
Schedule a job with an exact and relative time:
To schedule a job to run at a specific time, run:
at 17:00
Then in the at
prompt, type:
echo 'echo command scheduled for 17:00' > /tmp/at-1700.job
Then press Ctrl+D to submit the job.
To schedule a job with a relative time, run:
at now + 10 minutes
Then in the at
prompt, type:
echo 'echo command that was scheduled for 10 minutes time' > /tmp/at-5mins.job
Then press Ctrl+D to submit the job.
Display and remove a scheduled job:
To list scheduled jobs:
atq
This will display the time of when the 2 scheduled jobs are due to run:
1 Thu Aug 10 17:00:00 2023 a user1
2 Thu Aug 10 08:59:00 2023 a user1
Remove job 1 as it is no longer required:
atrm 1
When listing the at
queue again, it will only display job 2 (unless it took you over 10 minutes to complete the exercise, in which case no jobs will be displayed):
atq
2 Thu Aug 10 08:59:00 2023 a user1
Check at
job run successfully:
After 10 minutes the scheduled job should have run. Check file /tmp/at-5mins.job
to see if it has the expected contents:
cat /tmp/at-5mins.job
You should receive output:
echo command that was scheduled for 10 minutes time
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.