Learn the step by step process and the technique of scheduling tasks in Linux by using the cron, crontab and at utilities. You will also learn how to see and terminate scheduled tasks.
Just as there is the task scheduler on Windows operating system, there is also the “crond”, and the “atd” daemon in Linux that runs scheduled tasks in the background.
Scheduling tasks is very important for system users, more importantly for system administrators, especially to be able to automatically perform some repeated and regular activities such as backups, maintenance, etc.
In Linux, the two basic services that are responsible for scheduling tasks are the “crond”, and the “atd” services.
cron is the program that is used to schedule a job in Linux, just as we mentioned above, the daemon responsible for cron is the “crond”, and it is started by default as soon as the system is powered on
For an administrator to be able to configure and run a scheduled task, the cron daemon (crond) must be active and running.
To verify the status of the crond daemon, use the command,
[root@HQDEV1 cron.d]# systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2020-08-30 01:57:52 WAT; 1 day 14h ago
cron is very flexible, and best for tasks that need to be done repeatedly or on a regular basis (recurring jobs).
The cron configuration file can be found in many paths, one of which is the “/etc/crontab”.
[root@HQDEV1 ~]# 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
Other cron configuration files are found in “/etc/cron.d”, “/etc/cron.daily”, “/etc/cron.deny” , “/etc/cron.hourly”, “/etc/cron.monthly”, “/etc/crontab”, “/etc/weekly”
[root@HQDEV1 ~]# cd /etc [root@HQDEV1 etc]# ls -ld cron* drwxr-xr-x. 2 root root 39 Jul 11 19:25 cron.d drwxr-xr-x. 2 root root 36 Jul 11 19:26 cron.daily -rw-r--r--. 1 root root 0 Jun 12 2019 cron.deny drwxr-xr-x. 2 root root 22 Jul 11 19:22 cron.hourly drwxr-xr-x. 2 root root 6 Aug 12 2018 cron.monthly -rw-r--r--. 1 root root 451 Aug 12 2018 crontab drwxr-xr-x. 2 root root 6 Aug 12 2018 cron.weekly
As the names of these directories imply, they execute shell scripts/jobs daily, weekly, hourly, and monthly by rpm packages, and these packages just know how to use these files to execute their jobs.
To schedule a task using cron as a user or an administrator, it is recommended you define your crontab files in the “/etc/cron.d” directory or you use the “crontab -e” command to launch the crontab editor.
The reason is because the “/etc/crontab” file is always automatically modified by the system or packages when an update is done and the cron jobs you define in here can be overwritten. Hence, it is not recommended a user or an administrator modify this file to schedule a task.
Using the “/etc/cron.d” directory and the command, “crontab -e” to schedule a job is not difficult. The files in the “/etc/cron.d” will follow the same format with the one in /etc/crontab.
Similarly, the files that will be created using crontab -e will follow the same format in /etc/crontab except that you will not add the username in the file which we will see how to do this using examples as we go on.
An example of a file in the “/etc/cron.d” directory is the 0hourly file as seen below
[root@DRDEV1 cron.hourly]# cat /etc/cron.d/0hourly # Run the hourly jobs SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root 01 * * * * root run-parts /etc/cron.hourly
An administrator can also schedule a job by creating files in other cron configuration file location such as /etc/cron.daily, /etc/cron.monthly, /etc/cron.weekly, etc. However, this doesn’t follow the same format as using the /etc/cron.d or the crontab -e command.
The format in this location requires a user to create a script. An example of a cron job in this location is the /etc/cron.daily/logrotate file.
[root@DRDEV1 cron.daily]# cat /etc/cron.daily/logrotate #!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit $EXITVALUE
The advantage of using the /etc/cron.daily, /etc/cron.weekly, and /etc/cron.hourly to schedule a job is because the system uses some sort of mathematical technique called anacron (/etc/anacrontab) to backup the files in these directories.
So, if a job did not run due to the unavailability of the system in any form, the job will run when the system is available because the system looks at the anacron file and uses the configuration in the file to make the jobs run.
crontab is the acronym for “cron table”. It is the scripts or command that will run the cron job with the use of the cron daemon. The crontab command with the “-e” option will launch the crontab editor where you can define your scripts for a cron job
To use the “crontab -e” command to run a cron job, it is advisable to switch user to the user that wants to run the job or login as the user to give room for the right permissions.
For example, if you want to run a cron job with the user, Victor, switch user or login as Victor before using the “crontab -e” command to launch the crontab editor. If there is no existing crontab for the user, a new one will be created of course.
From the “/etc/crontab” file, you will see the examples of cron job definition
[root@HQDEV1 ~]# cat /etc/crontab
You can see the examples of the job definition, these examples can be compared to as
* * * * * scripts
What does this mean?
From left to right,
The first * represents minutes (from 0-59 minutes)
The second * represents hours (form 0-23 hours)
The third * represents days of month (from 1-31 days)
The fourth * represents months (form 1-12 months) or can also be jan, feb, mar, etc (i.e, the first three letters of the month)
The fifth * represents days of the week (from 0-6 days). Sunday is 0 or 7. It can also be sun, mon, tue, etc ( i.e, the first three letters of week)
The * wildcard means “every”, so for example, the script above simply means
(execute “every minute, every hour, every day of the month, every month, every day of the week”. The summary of the script is “execute every minute”)
Let’s log some information in the system log, “/var/log/messages” file on our system by using the cron job above
1. launch the crontab editor by running the command
[root@HQDEV1 ~]# crontab -e
* * * * * logger test
2. verify the /var/log/messages file to see if the job runs
[root@HQDEV1 ~]# cat /var/log/messages
Sep 2 17:26:02 HQDEV1 systemd: Started Session 16 of user root. Sep 2 17:26:02 HQDEV1 root: test Sep 2 17:27:01 HQDEV1 systemd: Started Session 17 of user root. Sep 2 17:27:01 HQDEV1 root: test Sep 2 17:28:01 HQDEV1 systemd: Started Session 18 of user root. Sep 2 17:28:01 HQDEV1 root: test
You can now see that the job runs every minutes
Let’s take another example.
If we are to schedule a job to execute every Thursday by 2am, the script will be written as,
0 2 * * 4 scripts
This simply means, execute the job every 0 minutes, of 2 hours (2AM), of every day of the month, of every month on Thursdays.
Let’s take another example,
If we are to schedule a job to execute from Monday till Friday at every 3 AM, the script will be written as
0 3 * * 1-5 script
This simply means, execute this job every o minutes of 2 hours (3AM), of every day of the month, of every month from monday till friday
Using cron is very flexible and can also be used by some certain words such as
When using these certain words, the other columns should be left empty
For example, to run a scripts daily, use the scripts
cron can also allow one to redirect the output of the job to a file.
To redirect the output of a cron job to a file, it can be written as below
@daily scripts >> /tmp/file1
To remove all the cron job of a user, use the command
crontab -r -u <username>
For example, to remove the all the cron job of the user Victor, use the command,
# crontab -r -u Victor
To verify the scheduled cron jobs of a user, use the command,
# crontab -l -u <username>
For example, to verify all the cron jobs of the user, Victor, use the command,
# crontab -l -u Victor
Just as cron, “at” is a program used to schedule a task in Linux. “at” is excellent for tasks that need to be done just once and not repeatedly (one time job).
For a user or administrator to be able to run scheduled tasks with at, the atd service must be active and running
To verify if the atd service is up and running, use the command
[root@HQDEV1 ~]# systemctl status atd ● atd.service - Job spooling tools Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2020-08-30 01:57:52 WAT; 2 days ago Main PID: 1723 (atd)
If the at package is not installed on your system, install it by using the command
# yum install at
To schedule a task using at, use the at utility followed by the argument to launch the at shell.
For example, to schedule a one time task to run at 18:50, do the following
1. Verify the current time on your system
[root@HQDEV1 ~]# date Wed Sep 2 18:47:27 WAT 2020
2. Use the at utility followed by the argument, in this case, we want to log the data test2 in the syslog, “var/log/messsages”
[root@HQDEV1 ~]# at 18:50 warning: commands will be executed using /bin/sh at> logger test2
3. use cntrl+d” to terminate the at shell
at> <EOT> job 2 at Wed Sep 2 18:50:00 2020 [root@HQDEV1 ~]#
4. Now, let’s verify if the job run
[root@HQDEV1 ~]# cat /var/log/messages
Sep 2 18:50:00 HQDEV1 systemd-logind: New session 40 of user root. Sep 2 18:50:00 HQDEV1 root: test2 Sep 2 18:50:00 HQDEV1 systemd-logind: Session 40 logged out. Waiting for processes to exit. Sep 2 18:50:00 HQDEV1 systemd-logind: Removed session 40.
You can see that the job was executed
To verify the number of at jobs that are to run, use the command,
[root@HQDEV1 ~]# atq
To remove an at job, use the command,
atrm <job no>
Other special arguments that can be used with at are;
at now + 3 minutes
at 8:00AM tomorrow
at 1:PM + 9 days
at 4:00 AM March 20
Your feedback is welcomed. If you love others, you will share with others