How to Schedule Cron Jobs\/Tasks in Linux\/Unix<\/a><\/p>\n\n\n\nSo, how does anacron<\/code> and cron<\/code> services differ?<\/p>\n\n\n\n\n- While
cron<\/code> is suitable for scheduling tasks on systems that are rarely shut down, anacron<\/code> is suitable for scheduling asynchronous tasks on systems that are shut down regularly.<\/li>\n\n\n\n- Cron jobs are executed at the exact time they are scheduled and if the system is shutdown when the cron job is supposed to be being executed, that cron is skipped until the next schedule.<\/li>\n\n\n\n
- Anacron jobs will be postponed until when the system is running if the system is shutdown by the time they were scheduled to be executed.<\/li>\n\n\n\n
- Anacron jobs are executed at most once a day<\/strong> while cron jobs can be executed more than once in a day.<\/li>\n<\/ul>\n\n\n\n
Files used by Anacron<\/h3>\n\n\n\n\n- Anacron reads scheduled jobs from
\/etc\/anacrontab<\/code> configuration file.<\/li>\n\n\n\n- It also uses the
\/var\/spool\/anacron<\/code> directory for storing timestamp files on when a specific job was last executed.<\/li>\n<\/ul>\n\n\n\nAnacron Job Entry Format<\/h3>\n\n\n\n
Each anacron job entry is specified in the format;<\/p>\n\n\n\n
period delay job-identifier shell-command<\/code><\/p>\n\n\n\nWhere:<\/p>\n\n\n\n
\nperiod<\/code> specifies how frequently, in days the job should be executed. For a example, a value of 10 means the job should be run every 10 days.<\/li>\n\n\n\ndelay<\/code> specifies a time in minutes between the when the anacron starts and when the job is executed.<\/li>\n\n\n\njob identifier<\/code> specifies a unique job ID. This is used in log messages to identify the jobs.<\/li>\n\n\n\nshell command<\/code> specifies the command or the name of the script to be executed.<\/li>\n<\/ul>\n\n\n\nHow does anacron<\/code> work?<\/h3>\n\n\n\nAnacron operates by checking whether a scheduled job has been executed in the last days specified in the period<\/code> parameter. If the job has not been executed, anacron waits for the number of minutes specified in the delay<\/code> field to elapse after which it runs the specified shell command or script.<\/p>\n\n\n\nEvery anacron job that is being executed or that is awaiting execution is locked so that if there are any other copies of Anacron in the system, such tasks cannot be executed at the same time.<\/p>\n\n\n\n
Once the execution of the specified task or job completes, Anacron timestamps this and exits when there is no more scheduled jobs to be executed.<\/p>\n\n\n\n
Output from Anacron tasks are mailed to the root user or to the user specified by the MAILTO environment variable if it is defined.<\/p>\n\n\n\n
By default, Anacron jobs cannot be executed if the system is not plugged in to power source. This ensures that the battery is not drained when executing the scheduled anacron jobs.<\/p>\n\n\n\n
Note that anacron jobs can also be called via the cron jobs.<\/p>\n\n\n\n
Schedule tasks using anacron<\/code><\/h3>\n\n\n\nBefore we can learn how to schedule an anacron job, let us have a look at a sample Anacron table, also known as, anacrontab<\/code> file from a CentOS 7 system.<\/p>\n\n\n\nless \/etc\/anacrontab<\/code><\/pre>\n\n\n\n# \/etc\/anacrontab: configuration file for anacron\n\n# See anacron(8) and anacrontab(5) for details.\n\nSHELL=\/bin\/sh\nPATH=\/sbin:\/bin:\/usr\/sbin:\/usr\/bin\nMAILTO=root\n# the maximal random delay added to the base delay of the jobs\nRANDOM_DELAY=45\n# the jobs will be started during the following hours only\nSTART_HOURS_RANGE=3-22\n\n#period in days delay in minutes job-identifier command\n1 5 cron.daily nice run-parts \/etc\/cron.daily\n7 25 cron.weekly nice run-parts \/etc\/cron.weekly\n@monthly 45 cron.monthly nice run-parts \/etc\/cron.monthly\n<\/code><\/pre>\n\n\n\nBased on the format above, for example, to schedule an anacron job to execute a;<\/p>\n\n\n\n
\n- backup shell script called
db-backups.sh<\/code><\/li>\n\n\n\n- with a unique identifier of
my-backups<\/code><\/li>\n\n\n\n- every
7 days<\/code><\/li>\n\n\n\n- with a
delay of 10 minutes<\/code> between when anacron runs and when the script runs,<\/li>\n<\/ul>\n\n\n\nSimply add the line below on the \/etc\/anacrontab<\/code> file.<\/p>\n\n\n\n7 10 my-backups \/path\/to\/db-backups.sh<\/code><\/pre>\n\n\n\nTo use custom anacrontab for example, you can create is as follows;<\/p>\n\n\n\n
echo \"1 1 backup \/home\/me\/myscripts\/backup.sh\" > mycustomanacrontab<\/code><\/pre>\n\n\n\nExamples on Scheduling tasks using anacron in Linux\/Unix<\/h3>\n\n\n\n
To wrap up with, let us see example usage of various anacron<\/code> command line options.<\/p>\n\n\n\nAnacron Command Syntax<\/h4>\n\n\n\n
The anacron command line syntax is like;<\/p>\n\n\n\n
anacron [-s] [-f] [-n] [-d] [-q] [-t anacrontab] [-S spooldir] [job] ...\nanacron [-S spooldir] -u [-t anacrontab] [job] ...\nanacron [-V|-h]\nanacron -T [-t anacrontab]\n<\/code><\/pre>\n\n\n\nTo explain the usage of some of these options, let us see a few examples;<\/p>\n\n\n\n
Test the validity of anacron configuration<\/h4>\n\n\n\n
Once you have schedules your anacron job, you can run the command below to verify its validity.<\/p>\n\n\n\n
anacron -T<\/code><\/pre>\n\n\n\nIf there is no error in your configuration, this command will give not output.
Assume you install an anacron task on your anacrontab called myanacrontest<\/code> on your home directory with the line below missing the job identifier<\/code>;<\/p>\n\n\n\n1 0 \/home\/me\/myscripts\/backup.sh<\/code><\/pre>\n\n\n\nRun the validity test by running;<\/p>\n\n\n\n
anacron -T -t myanacrontest<\/code><\/pre>\n\n\n\nOutput<\/p>\n\n\n\n
anacron: Invalid syntax in myanacron on line 1 - skipping this line<\/code><\/pre>\n\n\n\nForce execution of anacron job<\/h4>\n\n\n\n
You can force the execution of anacron job by using option -f<\/code>. For example, to force the execution of anacron job installed on a custom directory and sent the informational messages to standard error while being mailed to the default user;<\/p>\n\n\n\nanacron -d -f -t myanacron<\/code><\/pre>\n\n\n\nRun anacron jobs one after the other<\/h4>\n\n\n\n
To execute anacron jobs one after the first one completes, you can use the -n<\/code> or -s<\/code> options where:<\/p>\n\n\n\n\n-n<\/code> means execute the jobs immediately<\/li>\n\n\n\n-s<\/code> means serialize execution of jobs. Anacron will not start a new job before the previous one finished.<\/li>\n<\/ul>\n\n\n\nanacron -d -n -t myanacron<\/code><\/pre>\n\n\n\nanacron -d -s -t myanacron<\/code><\/pre>\n\n\n\nBoth give the same output;<\/p>\n\n\n\n
Anacron started on 2019-09-05\nNormal exit (0 jobs run)<\/code><\/pre>\n\n\n\nThat is just it on our guide on scheduling tasks using anacron in Linux\/Unix. Feel free to read more about anacron command line options on man anacron<\/code>.<\/p>\n\n\n\nIn our next guide, we will learn how to schedule tasks using the at<\/code> command. See the link below;<\/p>\n\n\n\nScheduling tasks using at command in Linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Well, this guide will focus on scheduling tasks using anacron in Linux\/Unix systems. So what is anacron? Anacron is a service that can be used to<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[1110,1112],"tags":[1114,1115],"class_list":["post-4134","post","type-post","status-publish","format-standard","hentry","category-automate-system-tasks","category-anacron","tag-anacron","tag-anacron-jobs","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4134"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=4134"}],"version-history":[{"count":4,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4134\/revisions"}],"predecessor-version":[{"id":21205,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4134\/revisions\/21205"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=4134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=4134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=4134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}