{"id":13244,"date":"2019-04-01T09:35:17","date_gmt":"2019-04-01T09:35:17","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13244"},"modified":"2019-04-01T09:35:17","modified_gmt":"2019-04-01T09:35:17","slug":"cron-vs-anacron-how-to-schedule-jobs-using-anacron-on-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/cron-vs-anacron-how-to-schedule-jobs-using-anacron-on-linux\/","title":{"rendered":"Cron Vs Anacron: How to Schedule Jobs Using Anacron on Linux"},"content":{"rendered":"<p>In this article, we will explain\u00a0<strong>cron<\/strong>\u00a0and\u00a0<strong>anacron<\/strong>\u00a0and also shows you how to setup anacron on Linux. We will as well cover a comparison of these two utilities.<\/p>\n<p>To\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-cron-alternative-at-command-to-schedule-tasks\/\" target=\"_blank\" rel=\"noopener noreferrer\">schedule a task on given or later time<\/a>, you can use the \u2018<strong>at<\/strong>\u2019 or \u2018<strong>batch<\/strong>\u2019 commands and to set up commands to run repeatedly, you can employ the\u00a0<strong>cron<\/strong>\u00a0and\u00a0<strong>anacron<\/strong>\u00a0facilities.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/11-cron-scheduling-task-examples-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">Cron<\/a>\u00a0\u2013 is a daemon used to run scheduled tasks such as system backups, updates and many more. It is suitable for running scheduled tasks on machines that will run continuously 24X7 such as servers.<\/p>\n<p>The commands\/tasks are scripted into cron jobs which are scheduled in crontab files. The default system crontab file is\u00a0<strong>\/etc\/crontab<\/strong>, but each user can also create their own crontab file that can launch commands at times that the user defines.<\/p>\n<p>To create a personal crontab file, simply type the following:<\/p>\n<pre>$ crontab -e\r\n<\/pre>\n<h3>How to Setup Anacron in Linux<\/h3>\n<p><strong>Anacron<\/strong>\u00a0is used to run commands periodically with a frequency defined in days. It works a little different from\u00a0<strong>cron<\/strong>; assumes that a machine will not be powered on all the time.<\/p>\n<p>It is appropriate for running daily, weekly, and monthly scheduled jobs normally run by cron, on machines that will not run 24-7 such as laptops and desktops machines.<\/p>\n<p>Assuming you have a scheduled task (such as a\u00a0<strong>backup script<\/strong>) to be run using cron every midnight, possibly when your asleep, and your desktop\/laptop is off by that time. Your backup script will not be executed.<\/p>\n<p>However, if you use\u00a0<strong>anacron<\/strong>, you can be assured that the next time you power on the desktop\/laptop again, the backup script will be executed.<\/p>\n<h3>How Anacron Works in Linux<\/h3>\n<p>anacron jobs are listed in\u00a0<strong>\/etc\/anacrontab<\/strong>\u00a0and jobs can be scheduled using the format below (comments inside anacrontab file must start with\u00a0<strong>#<\/strong>).<\/p>\n<pre>period   delay   job-identifier   command\r\n<\/pre>\n<p>From the above format:<\/p>\n<ul>\n<li><strong>period<\/strong>\u00a0\u2013 this is the frequency of job execution specified in days or as @daily, @weekly, or @monthly for once per day, week, or month. You can as well use numbers: 1 \u2013 daily, 7 \u2013 weekly, 30 \u2013 monthly and N \u2013 number of days.<\/li>\n<li><strong>delay<\/strong>\u00a0\u2013 it\u2019s the number of minutes to wait before executing a job.<\/li>\n<li><strong>job-id<\/strong>\u00a0\u2013 it\u2019s the distinctive name for the job written in log files.<\/li>\n<\/ul>\n<p>To view example files, type:<\/p>\n<pre><strong>$ ls -l \/var\/spool\/anacron\/<\/strong>\r\n\r\ntotal 12\r\n-rw------- 1 root root 9 Jun  1 10:25 cron.daily\r\n-rw------- 1 root root 9 May 27 11:01 cron.monthly\r\n-rw------- 1 root root 9 May 30 10:28 cron.weekly\r\n<\/pre>\n<ul>\n<li><strong>command<\/strong>\u00a0\u2013 it\u2019s the command or shell script to be executed.<\/li>\n<\/ul>\n<h5>This is what practically happens:<\/h5>\n<ul>\n<li>Anacron will check if a job has been executed within the specified period in the period field. If not, it executes the command specified in the command field after waiting the number of minutes specified in the delay field.<\/li>\n<li>Once the job has been executed, it records the date in a timestamp file in the\u00a0<strong>\/var\/spool\/anacron<\/strong>\u00a0directory with the name specified in the job-id (timestamp file name) field.<\/li>\n<\/ul>\n<p>Let\u2019s now look at an example. This will run the\u00a0<strong>\/home\/aaronkilik\/bin\/backup.sh<\/strong>\u00a0script everyday:<\/p>\n<pre>@daily    10    example.daily   \/bin\/bash \/home\/aaronkilik\/bin\/backup.sh\r\n<\/pre>\n<p>If the machine is off when the\u00a0<strong>backup.sh<\/strong>\u00a0job is expected to run, anacron will run it 10 minutes after the machine is powered on without having to wait for another 7 days.<\/p>\n<p>There are two important variables in the anacrontab file that you should understand:<\/p>\n<ul>\n<li><strong>START_HOURS_RANGE<\/strong>\u00a0\u2013 this sets time range in which jobs will be started (i.e execute jobs during the following hours only).<\/li>\n<li><strong>RANDOM_DELAY<\/strong>\u00a0\u2013 this defines the maximum random delay added to the user defined delay of a job (by default it\u2019s 45).<\/li>\n<\/ul>\n<p>This is how your anacrontab file would possibly look like.<\/p>\n<div class=\"code-label\">Anacron \u2013 \/etc\/anacrontab File<\/div>\n<pre># \/etc\/anacrontab: configuration file for anacron\r\n\r\n# See anacron(8) and anacrontab(5) for details.\r\n\r\nSHELL=\/bin\/sh\r\nPATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/sbin:\/bin:\/usr\/sbin:\/usr\/bin\r\nHOME=\/root\r\nLOGNAME=root\r\n\r\n# These replace cron's entries\r\n1       5       cron.daily      run-parts --report \/etc\/cron.daily\r\n7       10      cron.weekly     run-parts --report \/etc\/cron.weekly\r\n@monthly        15      cron.monthly    run-parts --report \/etc\/cron.monthly\r\n\r\n@daily    10    example.daily   \/bin\/bash \/home\/aaronkilik\/bin\/backup.sh                                                                      \r\n<\/pre>\n<p>The following is a comparison of\u00a0<strong>cron<\/strong>\u00a0and\u00a0<strong>anacron<\/strong>\u00a0to help you understand when to use either of them.<\/p>\n<table>\n<tbody>\n<tr>\n<th>Cron<\/th>\n<th>Anacron<\/th>\n<\/tr>\n<tr class=\"alt\">\n<td>It\u2019s a daemon<\/td>\n<td>It\u2019s not a daemon<\/td>\n<\/tr>\n<tr>\n<td>Appropriate for server machines<\/td>\n<td>Appropriate for desktop\/laptop machines<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td>Enables you to run scheduled jobs every minute<\/td>\n<td>Only enables you to run scheduled jobs on daily basis<\/td>\n<\/tr>\n<tr>\n<td>Doesn\u2019t executed a scheduled job when the machine if off<\/td>\n<td>If the machine if off when a scheduled job is due, it will execute a scheduled job when the machine is powered on the next time<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td>Can be used by both normal users and root<\/td>\n<td>Can only be used by root unless otherwise (enabled for normal users with specific configs)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The major difference between\u00a0<strong>cron<\/strong>\u00a0and\u00a0<strong>anacron<\/strong>\u00a0is that\u00a0<strong>cron<\/strong>\u00a0works effectively on machines that will run continuously while\u00a0<strong>anacron<\/strong>\u00a0is intended for machines that will be powered off in a day or week.<\/p>\n<p>If you know any other way, do share with us using the comment form below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/cron-vs-anacron-schedule-jobs-using-anacron-on-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explain\u00a0cron\u00a0and\u00a0anacron\u00a0and also shows you how to setup anacron on Linux. We will as well cover a comparison of these two utilities. To\u00a0schedule a task on given or later time, you can use the \u2018at\u2019 or \u2018batch\u2019 commands and to set up commands to run repeatedly, you can employ the\u00a0cron\u00a0and\u00a0anacron\u00a0facilities. Cron\u00a0\u2013 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/cron-vs-anacron-how-to-schedule-jobs-using-anacron-on-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cron Vs Anacron: How to Schedule Jobs Using Anacron on Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13244","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13244"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13244\/revisions"}],"predecessor-version":[{"id":13245,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13244\/revisions\/13245"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}