{"id":1389,"date":"2021-01-01T13:22:44","date_gmt":"2021-01-01T12:22:44","guid":{"rendered":"https:\/\/codedoneright.eu\/?page_id=1389"},"modified":"2021-01-01T18:12:00","modified_gmt":"2021-01-01T17:12:00","slug":"cron-task-automation","status":"publish","type":"page","link":"https:\/\/codedoneright.eu\/?page_id=1389","title":{"rendered":"Cron \u2013 task automation"},"content":{"rendered":"\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>According to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cron\" target=\"_blank\" rel=\"noreferrer noopener\">Wikipedia<\/a> &#8220;cron (&#8230;) is a time-based job scheduler in Unix-like computer operating systems.&#8221;<\/p>\n\n\n\n<p>And it does exactly that \u2013 <em>cron<\/em> allows for execution of any task at any time.  All Linux distributions come with cron preinstalled, as this is an absolutely basic tool for any system administrator.<\/p>\n\n\n\n<p>If you run a command with any regularity you should use cron instead of manually executing it. A good example might be <a href=\"https:\/\/codedoneright.eu\/?page_id=187\" target=\"_blank\" rel=\"noreferrer noopener\">ClamAV antivirus<\/a> scan script as cron can run both executables and shell scripts.<\/p>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">How does cron work?<\/h4>\n\n\n\n<p>Every user, including root, has a crontab file in which one can schedule a task. There are some system-specific crontabs as well, but I will focus on root and user crontabs as those are of more use right now.<\/p>\n\n\n\n<p>Every crontab can contain any number of lines with scheduled tasks. Take a look at the example below taken from Wikipedia<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 minute (0 - 59)\n# \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 hour (0 - 23)\n# \u2502 \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 day of the month (1 - 31)\n# \u2502 \u2502 \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 month (1 - 12)\n# \u2502 \u2502 \u2502 \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 day of the week (0 - 6) (Sunday to Saturday;\n# \u2502 \u2502 \u2502 \u2502 \u2502                                   7 is also Sunday on some systems)\n# \u2502 \u2502 \u2502 \u2502 \u2502\n# \u2502 \u2502 \u2502 \u2502 \u2502\n# * * * * * &lt;command to execute&gt;<\/code><\/pre>\n\n\n\n<p>It shows the basic syntax of a scheduled task in cron. You can specify minutes, hours, days, months and days of the week. This allows for automation of a tasks throughout the whole year.<\/p>\n\n\n\n<p>Look at the following example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 * * * * \/path\/to\/script.sh<\/code><\/pre>\n\n\n\n<p>This will run <em>script.sh<\/em> at every hour. It translates to <em>At minute zero<\/em>.<\/p>\n\n\n\n<p>You can substitute asterisk with a number to define when the task should be performed. An asterisk * means\u00a0<em>every.<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>* * * * * \/path\/to\/script.sh<\/code><\/pre>\n\n\n\n<p>This will run <em>script.sh<\/em> every minute. Script will run every 60 seconds!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 0 1 10 * \/path\/to\/script.sh<\/code><\/pre>\n\n\n\n<p>This one is for when you want to wake up Billie Joe Armstrong. It translates to <em>Minute zero of hour zero of the first of October<\/em>.<\/p>\n\n\n\n<p>If you have any trouble setting up a time frame for your crontab use <a href=\"http:\/\/crontab.guru\" target=\"_blank\" rel=\"noreferrer noopener\">this online tool<\/a> to help you.<\/p>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">How to setup a cron job?<\/h4>\n\n\n\n<p>You have to modify the crontab by running<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e<\/code><\/pre>\n\n\n\n<p>If this is your first run of this command, you will see the following output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>no crontab for root - using an empty one\n\nSelect an editor.  To change later, run 'select-editor'.\n  1. \/bin\/nano        &lt;---- easiest\n  2. \/usr\/bin\/vim.tiny\n  3. \/bin\/ed\n\nChoose 1-3 &#91;1]:<\/code><\/pre>\n\n\n\n<p>Just type 1 to use nano editor and press ENTER. <\/p>\n\n\n\n<p>You will be taken to your user&#8217;s crontab file. It contains some basic instructions. Move to the bottom of the file and add jobs there.<\/p>\n\n\n\n<p>Each line should contain only one scheduled job and should not start with #<\/p>\n\n\n\n<p>For example, if you want to run your script at <em>1pm<\/em> every <em>Thursday<\/em> put the following<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 13 * * 4 \/path\/to\/script.sh<\/code><\/pre>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">root vs user crontab<\/h4>\n\n\n\n<p>As mentioned earlier, each user, including root, has their own crontab. System keeps track of all of them. You can schedule tasks in any crontab you like, however, keep in mind that jobs scheduled in root crontab will be executed with root privileges and jobs schedules in user crontab will be restricted to what that specific user can do<\/p>\n\n\n\n<p>If the task you are scheduling requires the use of <em>sudo<\/em>, then you should use root crontab instead of using your regular user with <em>sudo<\/em>. For jobs that do not require sudo use your regular account.<\/p>\n\n\n\n<p>If you want to schedule a job in root crontab, prefix the command with sudo<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo crontab -e<\/code><\/pre>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Few things to keep in mind when using a crontab<\/h4>\n\n\n\n<ul><li>Test the execution of a script before adding it to crontab, to make sure it actually works<\/li><li>Script will be executed without any user input, do not move the script after adding it to the crontab<\/li><li>Do not schedule multiple tasks for the same time, it will unnecessarily consume resources and slow down your server<\/li><li>Schedule tasks one after another, make sure you roughly know how much time it takes for scheduled tasks \u2013 e.g. full data backup may take hours<\/li><li>Keep the file organized and put tasks in order from the most frequent at the top of your list to the least frequent at the bottom, it is for your sake<\/li><li>Keep script files in a separate folder dedicated just for them, this will help you to organise tasks<\/li><\/ul>\n\n\n\n<p>Test script execution by scheduling the task 2 minutes in the future, then close the file. If all goes well, you will have the result shortly and afterwards you can change the frequency.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>According to Wikipedia &#8220;cron (&#8230;) is a time-based job scheduler in Unix-like computer operating systems.&#8221; And it does exactly that \u2013 cron allows for execution of any task at any time. All Linux distributions come with cron preinstalled, as this&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1026,"menu_order":22,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Cron, automate tasks with ease &#8212; Code Done Right!<\/title>\n<meta name=\"description\" content=\"Learn how to automate task execution on your server with ease, schedule your script to run whenever you want it to!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codedoneright.eu\/?page_id=1389\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Cron, automate tasks with ease &#8212; Code Done Right!\" \/>\n<meta name=\"twitter:description\" content=\"Learn how to automate task execution on your server with ease, schedule your script to run whenever you want it to!\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codedoneright.eu\/?page_id=1389\",\"url\":\"https:\/\/codedoneright.eu\/?page_id=1389\",\"name\":\"Cron, automate tasks with ease &#8212; Code Done Right!\",\"isPartOf\":{\"@id\":\"https:\/\/codedoneright.eu\/#website\"},\"datePublished\":\"2021-01-01T12:22:44+00:00\",\"dateModified\":\"2021-01-01T17:12:00+00:00\",\"description\":\"Learn how to automate task execution on your server with ease, schedule your script to run whenever you want it to!\",\"breadcrumb\":{\"@id\":\"https:\/\/codedoneright.eu\/?page_id=1389#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codedoneright.eu\/?page_id=1389\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codedoneright.eu\/?page_id=1389#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codedoneright.eu\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\",\"item\":\"https:\/\/codedoneright.eu\/?page_id=1026\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Cron \u2013 task automation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codedoneright.eu\/#website\",\"url\":\"https:\/\/codedoneright.eu\/\",\"name\":\"Code Done Right!\",\"description\":\"Raspberry Pi server guides\",\"publisher\":{\"@id\":\"https:\/\/codedoneright.eu\/#\/schema\/person\/50378701e349dbd5d40888bc5b532568\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codedoneright.eu\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/codedoneright.eu\/#\/schema\/person\/50378701e349dbd5d40888bc5b532568\",\"name\":\"CodeDoneRight\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codedoneright.eu\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/codedoneright.eu\/wp-content\/uploads\/www_icon.png\",\"contentUrl\":\"https:\/\/codedoneright.eu\/wp-content\/uploads\/www_icon.png\",\"width\":120,\"height\":120,\"caption\":\"CodeDoneRight\"},\"logo\":{\"@id\":\"https:\/\/codedoneright.eu\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/codedoneright.eu\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cron, automate tasks with ease &#8212; Code Done Right!","description":"Learn how to automate task execution on your server with ease, schedule your script to run whenever you want it to!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codedoneright.eu\/?page_id=1389","twitter_card":"summary_large_image","twitter_title":"Cron, automate tasks with ease &#8212; Code Done Right!","twitter_description":"Learn how to automate task execution on your server with ease, schedule your script to run whenever you want it to!","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codedoneright.eu\/?page_id=1389","url":"https:\/\/codedoneright.eu\/?page_id=1389","name":"Cron, automate tasks with ease &#8212; Code Done Right!","isPartOf":{"@id":"https:\/\/codedoneright.eu\/#website"},"datePublished":"2021-01-01T12:22:44+00:00","dateModified":"2021-01-01T17:12:00+00:00","description":"Learn how to automate task execution on your server with ease, schedule your script to run whenever you want it to!","breadcrumb":{"@id":"https:\/\/codedoneright.eu\/?page_id=1389#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codedoneright.eu\/?page_id=1389"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codedoneright.eu\/?page_id=1389#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codedoneright.eu\/"},{"@type":"ListItem","position":2,"name":"Linux","item":"https:\/\/codedoneright.eu\/?page_id=1026"},{"@type":"ListItem","position":3,"name":"Cron \u2013 task automation"}]},{"@type":"WebSite","@id":"https:\/\/codedoneright.eu\/#website","url":"https:\/\/codedoneright.eu\/","name":"Code Done Right!","description":"Raspberry Pi server guides","publisher":{"@id":"https:\/\/codedoneright.eu\/#\/schema\/person\/50378701e349dbd5d40888bc5b532568"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codedoneright.eu\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/codedoneright.eu\/#\/schema\/person\/50378701e349dbd5d40888bc5b532568","name":"CodeDoneRight","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codedoneright.eu\/#\/schema\/person\/image\/","url":"https:\/\/codedoneright.eu\/wp-content\/uploads\/www_icon.png","contentUrl":"https:\/\/codedoneright.eu\/wp-content\/uploads\/www_icon.png","width":120,"height":120,"caption":"CodeDoneRight"},"logo":{"@id":"https:\/\/codedoneright.eu\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/codedoneright.eu"]}]}},"_links":{"self":[{"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages\/1389"}],"collection":[{"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1389"}],"version-history":[{"count":17,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages\/1389\/revisions"}],"predecessor-version":[{"id":1407,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages\/1389\/revisions\/1407"}],"up":[{"embeddable":true,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages\/1026"}],"wp:attachment":[{"href":"https:\/\/codedoneright.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}