What is Job Scheduling ?
When you want to run specific tasks, programs or commands at some particular time we do job scheduling so that we no need to sit at our desk to press a key or run a command by ourselves at the right time.
Few most popular examples of such tasks are:
- Taking database or log files backups at mid night or over weekends
- Executing system maintenance commands to run during non-working hours
- clean up of cached data
- Sending out notifications such as password expiration etc.
Tools available for Job Scheduling
Linux operating system natively comes up with two daemons for scheduling jobs:
atd
and crond
. There is a third tool called anacron
is also available which is closely related to cron
with some differences.
Let us understand all these tools one by one in more details.
Job Scheduling with cron
The cron
daemon runs a lot of tasks (most of them runs in background) at specific scheduled times. It is still the best Task Scheduler
tool available when you want to schedule repetitive tasks.
Daemon Name -> crond
crond
is the daemon which keeps tracking of all the tasks scheduled to run at specific times. It read the configuration files we call as crontabs
aka cron tables
in order to execute commands or shell scripts specified if the time matches the time indicated.
Configurations files and directories location ->
Following are the files and directories where we keep our cron
job scheduling files or programs.
cron daemon service name -> cron.service
Unlike other services the cron
service doesn't need to be reloaded or restarted to load new changes to it's configuration. The cron
daemon keep on checking it's configurations in the /etc/crontab
file, and inside the /etc/cron.*/
directories. It also checks the /var/spool/cron/
directory every minute to see whether anything which needs to be started.
Crontab file entry syntax
Every crontab file entry has six fields. The first five fields defines the time and date of execution, and the 6'th field mentions the command which will get executed.
To understand the first five fields in more detail please go through the following table.
Following table will help you in understanding the usage of operators while scheduling a cron
job.
Adding/Modifying crontab files
The is a command called crontab
to maintain crontab files for individual users.
Here I will demonstrate crontab file addition/modifications for a regular Linux local user vagrant
.
I have a very simple bash script which echo's a string with time stamp to a text file named cron_example.txt
in vagrant user's home directory /home/vagrant/
.
vagrant@lco-linux-master:~$ cat crontab-example.sh
#!/bin/bash
echo "cron example sample taken at "`date +"%d-%m-%Y-%H%M%S"`"" >> /home/vagrant/cron_example.txt
List current list of crontab entries for a user ->
vagrant@lco-linux-master:~$ crontab -l
no crontab for vagrant
Edit the crontab file
Now I will add a cron job for vagrant user which will run the crontab-example.sh
every minute.
vagrant@lco-linux-master:~$ crontab -e
We have added following entry in the crontab file which will run the crontab-example.sh
every minute:
* * * * * bash /home/vagrant/crontab-example.sh
Since we now have a job scheduled for vagrant user which will run every minute. Have a look below it will create a file having the data defined as per our script.
Remove crontab entries for a user -> Run the following command to remove all the crontab entries for a user :
vagrant@lco-linux-master:~$ crontab -r
vagrant@lco-linux-master:~$ crontab -l
no crontab for vagrant
This is disastrous. You should never remove
cron
entries like this. The another way is to open crontab file in edit mode and comment out the particular entry. It will disable that particular job from scheduled job list.If a confirmation before deleting is required, use
-i
option with-r
root@lco-linux-master:~# crontab -r -i -u vagrant crontab: really delete vagrant's crontab? (y/n) y
If you are logged in as
root
and wants to view or edit other user's crontab files run the following commands.To view the other user's crontab -
root@lco-linux-master:~# crontab -l -u vagrant * * * * * bash /home/vagrant/crontab-example.sh #*/5 * * * * bash /home/vagrant/crontab-example.sh
To edit other user's crontab -
root@lco-linux-master:~# crontab -e -u vagrant
To delete other user's crontab -
root@lco-linux-master:~# crontab -r -u vagrant root@lco-linux-master:~# crontab -l -u vagrant no crontab for vagrant
Important cron examples
Here I will list some important examples of Crontab.
- Schedule a cron to execute twice a day at 6 AM and 6 PM
0 6,18 * * * /path_to_script/script.sh
- Schedule a job to run at 2.15 P.M. on the first of every month
15 14 1 * * * /path_to_script/script.sh
- Schedule a job to run every hour from 3 P.M. To 10 P.M
* 3-10 * * * /path_to_script/script.sh
- Schedule a cron to execute on selected months
* * * feb,aug,oct * /path_to_script/script.sh
- Schedule a cron to execute every 15 minutes
*/15 * * * * /path_to_script/script.sh
- Schedule multiple jobs in a single cron job
0 11 * * * /path_to_script/script.sh; /path_to_script/backup.sh
- Schedule a cron to execute after every 10 Seconds
* * * * * /path_to_script/script.sh
* * * * * sleep 10; /path_to_script/script.sh
Since we can not control seconds in the Crontab, this request was achieved by following two step process of scheduling first task that runs every minute and then pause the execution for 10 seconds.
- Schedule a job which runs every third hour
* 0-12/3 * * * /path_to_script/script.sh
- Delete empty files and directory
45 0 * * * find /tmp -type f -empty -delete
Macros in crontab
We can also add special time specification as nicknames in the crontab file. For example, they are prefixed with the @
.
Below table will explain all the macros with examples.
Disable Email Notification and Ignoring stderr and stdout
To prevent the sending of errors and output, any one of the following entries can be added at the end of the line for each cron
job to redirect output to /dev/null
.
> /dev/null 2>&1
> /dev/null
> /dev/null 2>&1 || true
To send cron output to a log file->
> /var/log/my.app.log 2>&1
For example:
0 1 5 10 * /path_to_script/script.sh >/dev/null 2>&1
Another option is to set
MAILTO=""
variable at the start of your crontab file or shell script. This as well disables email alert.
Job Scheduling with anacron
Anacron tool can be used to execute commands or scripts periodically, with a frequency specified in days.
Here is the sample Anacron table file, also known as, anacrontab
.
Anacron doesn't expect or assume a machine to be running continuously and that is it's biggest advantage over cron
.
For example lets say we have a cron
job scheduled to run a task every midnight such as a backup script. If your laptop or desktop is off during that time cron
will skip the task and your backup script will not be executed.
Whereas if you use anacron
whenever next time you desktop/laptop comes up again, the backup script will be executed. And after that it will record the date
in a timestamp file in the /var/spool/anacron
directory with the name specified in the job-id
(timestamp file name) field.
Schedule anacron job
The syntax of an anacrontab file is:
period delay job-identifier command
period
– Job execution frequency specified in days or as@daily
,@weekly
, or@monthly
for once per day, week, or month. You can also use numbers:1 – daily
,7 – weekly
,30 – monthly
andN – number of days
.delay
– it’s the number of minutes to wait before executing a job.job-id
– it’s the distinctive nick-name for the job written in log files.
Let us understand this by an example.
We have a system maintenance script called sys-maintain.sh
with a unique identifier of sys-repair
. It should run every 7 days with a delay of 15 minutes between when anacron runs and when the script runs.
Just add the line below on the /etc/anacrontab
file.
7 15 sys-repair /path/to/sys-maintain.sh
No assume the machine is off when the sys-repair
task is expected to run, anacron
will run it 15 minutes after the machine is powered on. One no need to wait for next run which will come after 7 days.
Job Scheduling with at
We use at
service where the program need to be executed only once. Whereas cron
or anacron
are used to schedule jobs which gets executed on a regular basis.
at
command
at
is the command which you need to use in order to schedule one or more programs for a single execution at some later time.
The at
command only needs one parameter the time
of scheduling.
The syntax is ->
at [-m] time [date]
-m
-> Sends you email after the job is completed.
time
-> Specifies the hour that you want to schedule the job. Add am or pm if you do not specify the hours according to the 24-hour clock. Acceptable keywords are midnight, noon, and now. Minutes are optional.
date
-> Specifies the first three or more letters of a month, a day of the week, or the keywords today or tomorrow.
Related client commands:
at
-> Runs commands at specified time
atq
-> Lists pending commands
atrm
-> Cancels pending jobs
batch
-> Runs commands when system load permits
Schedule jobs with at
command
Schedule a task at a specified time ->
Let us schedule a job which will run at 9 AM.
root@lco-linux-master:~# at 9am
warning: commands will be executed using /bin/sh
at> echo "Hello LCO readers, Good Morning!"
at> <EOT>
job 4 at Fri May 14 09:00:00 2021
root@lco-linux-master:~# atq
4 Fri May 14 09:00:00 2021 a root
Press
CTRL+D
to quit the at shell.
Schedule a future task ->
To schedule a future task run the following commands.
root@lco-linux-master:~# at 09:35 09/08/21
warning: commands will be executed using /bin/sh
at> /path_to_script/script.sh >/dev/null 2>&1
at> <EOT>
job 5 at Wed Sep 8 09:35:00 2021
root@lco-linux-master:~# atq
5 Wed Sep 8 09:35:00 2021 a root
4 Fri May 14 09:00:00 2021 a root
Remove a task ->
root@lco-linux-master:~# at -l
5 Wed Sep 8 09:35:00 2021 a root
4 Fri May 14 09:00:00 2021 a root
root@lco-linux-master:~# atrm 4
root@lco-linux-master:~# at -l
5 Wed Sep 8 09:35:00 2021 a root
Controlling user access for Job Scheduling
By default, all the Linux system users can schedule jobs. If you want to limit this access to specific users that's possible by using some special or permissive files.
Limit Access for cron
job scheduling
Crontab manages access based on the files /etc/cron.allow
and /etc/cron.deny
. If the cron.allow
file exists on the system, a user must be listed in it to be allowed to schedule jobs using cron
. If the cron.deny
file exists, a user mustn't be listed in it to be allowed to schedule cron
jobs.
Limit Access for at
job scheduling
If the at.allow
file exists on the system and usernames are listed there, only those users, and the root user are allowed to use at. If the /etc/at.deny
file exists and usernames are mentioned, those users will be denied at
scheduling access and all others will be allowed.
That's all for this detailed guide covering all the important aspects of Job Scheduling on Linux systems.
Hope you like the article. Stay Tuned for more.
Thank you. Happy learning!