anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the job then decides whether to run it or not and if its timestamp is >=n(n is defined number of days) then runs it after a specified time delay.
It mainly constitutes of two important Files:
- /etc/anacrontab : It contains specifications of job.
- See line SHELL=/bin/sh. It uses sh not bash you cannot run bash scripts.
- Table at the last represents different scheduling. The first colum is the number of days(n) i.e. period and second is a time delay in minutes.
- /var/spool/anacron : This directory is used by Anacron for storing timestamp files. It represents timestamp for different catetgory of jobs i.e. daily, weekly, monthly, etc.
anacron [-s] [-f] [-n] [-d] [-q] [-t anacrontab] [-S spooldir] [job] anacron [-S spooldir] -u [-t anacrontab] [job] ... anacron [-V|-h] anacron -T [-t anacrontab]
- f : Used to force execution of the jobs, ignoring the timestamps.
- u : Only update the timestamps of the jobs, to the current date, but don’t run anything.
- s : Serialize execution of jobs. Anacron will not start a new job before the previous one finished.
- n : Run jobs now.Ignore any delay.
- d : Don’t fork to the background. In this mode, Anacron will output informational messages to standard error, as well as to syslog. The output of jobs is mailed as usual.
- q : Suppress messages to standard error. Only applicable with -d.
- V (Use specified anacrontab) : Print version information and exit.
- h (Use specified anacrontab) : Print short usage message, and exit.
- You can add any script to etc/cron.daily or etc/cron.weekly or cron.monthly directory.but remember script should be sh not bash.
- Don’t forget to give executable permissions to your files sudo chmod +x filename.
- Scripts in directories automatically get executed depending upon which directory they are in.
- Changing the timestamp of jobs. Observe the change in timestamp values.
- Performing force execution. Notice the time delay.
- Serialized Execution.
- Print version information.
- Print sort Usage message.
Note: Here we have used -d option with all commands to display on the screen what is actually happening.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.