Lab 22: Schedule Tasks With at and Cron

Objective Focus
Deploy, configure, and maintain systems
Schedule Tasks With at and Cron

Official RHCSA EX200 Exam Objectives

“Scheduling jobs” means executing a command in the future. This similar to Windows Task Scheduler in the Microsoft world. Examples of jobs we would schedule are reboots, updates, and even custom scripts.

The commands we use for scheduling jobs are at and crontab.

  • As always, reviewing the man page is the first step in learning new commands.
    $ man at 
    $ man crontab 
Using at
Specifying the time of execution

Using the at command schedules the execution of a command once in the future.

Time Explanation
at HH:MM We can use PM or AM but using the 24-hour clock is my preference
at HH:MM MM/DD/YY We can specify the date
at HH:MM tomorrow We can specify the next day

More examples of time specifications can be found in the man and info pages.

Our at command example use case
$ at now + 1 minutes
at> echo "This is from at" > /home/learner/at_test.txt
at> <EOT>
  • Press enter then the at> prompt will start. Enter your commands then enter lastly press Ctrl+D to exit the at> prompt or at shell.

Now, we can check use the at -l command to list the pending jobs.

$ at -l 
  • We can use the job ID with at -c *job_ID* to get more details.
$ atrm 
  • now remove using the job ID and check with at -l that it is gone.
What you need to know about crontab

The cron utility is another way we can schedule command to be executed in the future. It is a very popular form of automating system administration tasks.

The crontab is different from at because there is background process called crond that runs periodically to check for cron jobs in /etc/crontab. Crond examines entries every minute

The key to creating cron jobs is the using the proper syntax.

$ man crontab 
  • There are examples in the man page of the syntax rules. Do not try and memorize, just know where to find the information.
  • Each line in a crontab typically represents a single cron job and follows a specific format:
[root@server1 ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
  • Asterisks (*) are used as wildcards, meaning “every” or “all” for that field. For example, * * * * * would mean “every minute of every hour of every day of every month of every day of the week.”
Exercise for Crontab
$ crontab -e
  • After this command is entered, you will be sent to a text editor.
* * * * * echo "This is from cron" >> /home/learner/cron_test.txt
$ crontab -l
  • We the -l option, we see the contents of the crontab file
$ crontab -r
  • Now with -r option we can remove the crontab file

Another way yo remote the crontab file, is using the -e (edit option) because the -r option will remove everything job.

$ crontab -e
  • We can also just remove the line of interest with -e
Important Notes

The root user is always permitted to schedule jobs.

On a fresh install of RHEL, any user can schedule jobs. We can control who can schedule jobs via allow and deny files in the /etc directory.

These files are named at.allow or at.deny for at. cron.allow or cron.deny for cron. We only need one instance of either the allow or deny for control.

If it does not exist, you have to create it manually!

$ vim /etc/cron.deny
  • We’ll add the learner user to the deny file and see if we can use the crontab command.
Check if Crond service is running
systemctl status crond

That is all for cron and at service!!