An important service provided by any system is the ability to run a process on a predetermined schedule without human intertention. The “automation” of tasks can reduce the workload of the system administrator significantly. Unfortunately Linux currently offers not one service but potentially three—cron, anacron, and systemd timer units. In about a page compare and contrast all three systems. Illustrate your discussion by using the system files /etc/anacron and /etc/crontab and by constructing equivalent systemd service and timer files.
Cron, anacron and system timer units all run the scheduled tasks in linux and unix systems.
[email protected]$ crontab -l
*/10 * * * * /home/userone/check-disk-space
I logged in to my linux server as userone and gave the crontab –l command. That gave the above output. From this output we can come to know the crontab format. It is like
|MIN||Minute field||0 to 59|
|HOUR||Hour field||0 to 23|
|DOM||Day of Month||31-Jan|
|DOW||Day Of Week||0-6|
|CMD||Command||Any command to be executed|
To edit my crontab entries I ran crontab –e command. Here crontab = command to be executed. e=edit option
We can schedule a job for more than one instance. We can schedule a job to run at a particular time. We can use @daily , @monthly , @yearly , @reboot keywords.
|@yearly||0 0 1 1 *|
|@daily||0 0 * * *|
|@hourly||0 * * * *|
|@reboot||Run at startup.|
By default crontab sends the job output to the user who sets the cron job. We can redirect the output to some other user using MAIL variable in the crontab.
In cron the minimum time unit is minute. We cannot run cron jobs in seconds. We can add the cron jobs by running the commands in a separate file (example cronfile.txt) and adding the file entries to crontab by running the command crontab cronfile.txt. Crontab is ideal for servers.
It is the cron for laptops and desktops
When I ran cat /etc/anacrontab I got the following output.
7 15 test.daily /bin/sh /home/userone/backup.sh
Field 1 is Repeating period: This is a numeric value that specifies the number of days.
1 – daily
7 – weekly
30 – monthly
N – This can be any numeric value. N indicates number of days
Field 2 is Delay: This indicates the delay in minutes. i.e Waiting time. X number of minutes anacron should wait before executing the job after the the machine starts.
Field 3 is Job identifier: It is the name for the job’s timestamp file. It should be unique for each job. This will be available as a file under the /var/spool/anacron directory. This file will contain a single line that indicates the last time when this job was executed.
# ls -1 /var/spool/anacron/ test.daily
# cat /var/spool/anacron/test.daily
Field 4 is command: Command or shell script that needs to be executed
Now 7 15 test.daily /bin/sh /home/userone/backup.sh job runs once in 7 days , with a 15 min delay after the reboot. In this test.daily is the job identifier.
Comparison between cron and anacron
Cron is manly suitable for servers. i.e for the systems which will run continuously. Because during the time of execution if the system is down then the cron task will not get executed at all.
Anacron is suitable for desktops and laptops which will not run continuously. In this during the time of run if the system is down then the task will run when the systems starts next time.
Cron job can be used when we need to execute one jobs at particular time only. Anacron can be used for running the tasks irrespective of the time.
Cron job can be scheduled by any normal user. Usually anacron can be scheduled by only super users.
In cron the minimum granularity is minute. In anacron it is days.
Cron jobs can run any number of times like once in a minute. But anacron jobs can run only once in a day.