{"id":4134,"date":"2019-09-07T18:55:06","date_gmt":"2019-09-07T15:55:06","guid":{"rendered":"https:\/\/kifarunix.com\/?p=4134"},"modified":"2024-03-12T21:57:55","modified_gmt":"2024-03-12T18:57:55","slug":"scheduling-tasks-using-anacron-in-linux-unix","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/scheduling-tasks-using-anacron-in-linux-unix\/","title":{"rendered":"Scheduling tasks using anacron in Linux\/Unix"},"content":{"rendered":"\n<p>Well, this guide will focus on scheduling tasks using anacron in Linux\/Unix systems.<\/p>\n\n\n\n<p>So what is anacron? <code>Anacron<\/code>&nbsp;is a service that can be used to execute scheduled tasks or jobs or commands periodically in Linux\/Unix systems with a frequency specified in days.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schedule tasks using Anacron in Linux\/Unix<\/h2>\n\n\n\n<p>In our previous guide, we discussed how to schedule system tasks or jobs in Linux\/Unix using the <code>cron<\/code> command. Check the link below;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-schedule-cron-jobs-tasks-in-linux-unix\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">How to Schedule Cron Jobs\/Tasks in Linux\/Unix<\/a><\/p>\n\n\n\n<p>So, how does <code>anacron<\/code>&nbsp;and&nbsp;<code>cron<\/code>&nbsp;services differ?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>While&nbsp;<code>cron<\/code>&nbsp;is suitable for scheduling tasks on systems that are rarely shut down,&nbsp;<code>anacron<\/code>&nbsp;is suitable for scheduling asynchronous tasks on systems that are shut down regularly.<\/li>\n\n\n\n<li>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<li>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<li>Anacron jobs are executed at most&nbsp;<strong>once a day<\/strong> while cron jobs can be executed more than once in a day.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Files used by Anacron<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Anacron reads scheduled jobs from&nbsp;<code>\/etc\/anacrontab<\/code>&nbsp;configuration file.<\/li>\n\n\n\n<li>It also uses the&nbsp;<code>\/var\/spool\/anacron<\/code>&nbsp;directory for storing timestamp files on when a specific job was last executed.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Anacron Job Entry Format<\/h3>\n\n\n\n<p>Each anacron job entry is specified in the format;<\/p>\n\n\n\n<p><code>period delay job-identifier shell-command<\/code><\/p>\n\n\n\n<p>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>period<\/code>&nbsp;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\n<li><code>delay<\/code>&nbsp;specifies a time in minutes between the when the anacron starts and when the job is executed.<\/li>\n\n\n\n<li><code>job identifier<\/code>&nbsp;specifies a unique job ID. This is used in log messages to identify the jobs.<\/li>\n\n\n\n<li><code>shell command<\/code>&nbsp;specifies the command or the name of the script to be executed.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">How does <code>anacron<\/code> work?<\/h3>\n\n\n\n<p>Anacron operates by checking whether a scheduled job has been executed in the last days specified in the&nbsp;<code>period<\/code>&nbsp;parameter. If the job has not been executed, anacron waits for the number of minutes specified in the&nbsp;<code>delay<\/code>&nbsp;field to elapse after which it runs the specified shell command or script.<\/p>\n\n\n\n<p>Every 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<p>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<p>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<p>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<p>Note that anacron jobs can also be called via the cron jobs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schedule tasks using <code>anacron<\/code><\/h3>\n\n\n\n<p>Before we can learn how to schedule an anacron job, let us have a look at a sample Anacron table, also known as,&nbsp;<code>anacrontab<\/code>&nbsp;file from a CentOS 7 system.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>less \/etc\/anacrontab<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># \/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\n<p>Based on the format above, for example, to schedule an anacron job to execute a;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>backup shell script called&nbsp;<code>db-backups.sh<\/code><\/li>\n\n\n\n<li>with a unique identifier of&nbsp;<code>my-backups<\/code><\/li>\n\n\n\n<li>every&nbsp;<code>7 days<\/code><\/li>\n\n\n\n<li>with a&nbsp;<code>delay of 10 minutes<\/code>&nbsp;between when anacron runs and when the script runs,<\/li>\n<\/ul>\n\n\n\n<p>Simply add the line below on the&nbsp;<code>\/etc\/anacrontab<\/code>&nbsp;file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>7 10 my-backups \/path\/to\/db-backups.sh<\/code><\/pre>\n\n\n\n<p>To use custom anacrontab for example, you can create is as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"1 1 backup \/home\/me\/myscripts\/backup.sh\" &gt; mycustomanacrontab<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Examples on Scheduling tasks using anacron in Linux\/Unix<\/h3>\n\n\n\n<p>To wrap up with, let us see example usage of various&nbsp;<code>anacron<\/code>&nbsp;command line options.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Anacron Command Syntax<\/h4>\n\n\n\n<p>The anacron command line syntax is like;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anacron &#91;-s] &#91;-f] &#91;-n] &#91;-d] &#91;-q] &#91;-t anacrontab] &#91;-S spooldir] &#91;job] ...\nanacron &#91;-S spooldir] -u &#91;-t anacrontab] &#91;job] ...\nanacron &#91;-V|-h]\nanacron -T &#91;-t anacrontab]\n<\/code><\/pre>\n\n\n\n<p>To explain the usage of some of these options, let us see a few examples;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Test the validity of anacron configuration<\/h4>\n\n\n\n<p>Once you have schedules your anacron job, you can run the command below to verify its validity.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>anacron -T<\/code><\/pre>\n\n\n\n<p>If there is no error in your configuration, this command will give not output.<br>Assume you install an anacron task on your anacrontab called&nbsp;<code>myanacrontest<\/code>&nbsp;on your home directory with the line below missing the&nbsp;<code>job identifier<\/code>;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>1 0  \/home\/me\/myscripts\/backup.sh<\/code><\/pre>\n\n\n\n<p>Run the validity test by running;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>anacron -T -t myanacrontest<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>anacron: Invalid syntax in myanacron on line 1 - skipping this line<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Force execution of anacron job<\/h4>\n\n\n\n<p>You can force the execution of anacron job by using option&nbsp;<code>-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\n<pre class=\"wp-block-preformatted\"><code>anacron -d -f -t myanacron<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Run anacron jobs one after the other<\/h4>\n\n\n\n<p>To execute anacron jobs one after the first one completes, you can use the&nbsp;<code>-n<\/code>&nbsp;or&nbsp;<code>-s<\/code>&nbsp;options where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-n<\/code>&nbsp;means execute the jobs immediately<\/li>\n\n\n\n<li><code>-s<\/code>&nbsp;means serialize execution of jobs. Anacron will not start a new job before the previous one finished.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>anacron -d -n -t myanacron<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>anacron -d -s -t myanacron<\/code><\/pre>\n\n\n\n<p>Both give the same output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Anacron started on 2019-09-05\nNormal exit (0 jobs run)<\/code><\/pre>\n\n\n\n<p>That 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&nbsp;<code>man anacron<\/code>.<\/p>\n\n\n\n<p>In our next guide, we will learn how to schedule tasks using the <code>at<\/code> command. See the link below;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/scheduling-tasks-using-at-command-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Scheduling tasks using at command in Linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, this guide will focus on scheduling tasks using anacron in Linux\/Unix systems. So what is anacron? Anacron&nbsp;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}]}}