Linux Crontab Explained
Cron is a software utility for Unix-like operating systems. Cron allows users to run scheduled tasks for every specific time, specific dates, etc. Anacron is a computer program that can run schedule tasks which is done by cron, but without assuming that the system is running continuously. It can run commands periodically, with a frequency specified in days, and it can control regular tasks. It does not assume that the system is running 24 hours so it can be used with machines that are not running 24 hours a day. Cron allows to run tasks every minute, but if the system goes down, it does not guarantee the execution of cron job while anacron will remember the job if the system is down and it starts the execution of the jobs when the system is up. The configuration file of anacron is located at /etc/anacrontab. Anacron reads and controls the jobs from this configuration file. Each entry in this configuration file contains a period in days, a delay in minutes, a unique job identifier, and a shell command. Anacron reads each job and check whether the job has been executed in last n days (n=period specified for that job). If the job is not executed, then anacron runs the shell commands specified in the file after waiting for the delay parameter specified. If the command exists after execution anacron record the date in a special time-stamp file for the next execution of that command and if there are no more jobs to be run, Anacron exits. The timestamp files are stored in a directory named as anacron which is located at “/var/spool/anacron”.
Fields of anacron jobs
period delay job-id command
1) period: This is a numeric value which specifies the frequency of job execution in days, weeks and months.
2) delay: This indicates the number of minutes to wait before running a job.
3) job-id: Unique name for the job in timestamp file and it is available under /var/spool/anacron directory.
# ls -1 /var/spool/anacron/
4) command: The shell script or command to be run.
Example: – If the system goes down then the below command will execute backup.sh script 30 minutes after the system comes back up without having to wait for another 7 days.
# cat /etc/anacrontab
7 30 test.daily /bin/sh /home/user/backup.sh
Options of Anacron
1) -f: force execution of the jobs, ignoring the timestamps.
2) -u: Update the timestamps of the jobs to the current date.
3) -s: Serialize execution of jobs. Anacron starts the execution of a new job only after completing the previous one.
4) -n: Run jobs now.
5) -d: In this mode, anacron will output messages to standard error.
6) -q: Suppress messages to standard error.
Cron VS Anacron
1) Cron jobs can be scheduled by any normal user where as anacron can be used only by super user but it can be usable by the normal user.
2) Cron requires the system to be running 24 hours and anacron does not requires the system to run 24 hours a day.
3) Cron does not execute the tasks if the system goes down. With an anacron it starts the execution of the jobs when the system is up.
4) Cron jobs are ideal for servers and anacrons are ideal for desktops and laptops.
5) Cron schedules jobs to run at specific time(s) of day(s) and anacron is used to run jobs periodically.
Disadvantages of Anacron
1) Only the system administrator can configure the tasks.
2) It can only run tasks once a day.
3) Anacron never removes timestamp files. Remove unused files manually.
4) Anacron uses up to two file descriptors for each active job.
Command to install cron and anacron
# yum install cronie cronie-anacron
To start the service
# service crond start
To stop the service
# service crond stop
If you need any further assistance please contact our support department.