{"id":63220,"date":"2022-03-28T09:01:17","date_gmt":"2022-03-28T09:01:17","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=63220"},"modified":"2022-03-28T09:01:17","modified_gmt":"2022-03-28T09:01:17","slug":"scheduling-tasks-with-the-linux-cron-command","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=63220","title":{"rendered":"Scheduling tasks with the Linux cron command"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">Scheduling tasks with the Linux cron command<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/don-watkins\" class=\"username\">Don Watkins<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Mon, 03\/28\/2022 &#8211; 03:00<\/span><\/p>\n<div data-drupal-selector=\"rate-node-69786\" class=\"rate-widget-thumbs-up\">\n<div class=\"rate-thumbs-up-btn-up rate-thumbs-up-btn-up vote-pending\">Up<\/div>\n<div class=\"rate-score\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to like.<\/div>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>Early in my Linux journey, I came to appreciate the numerous command-line utilities of the operating system and the way they streamlined regular tasks. For example, backing up applications on my Windows server frequently required expensive add-on software packages. By contrast, the <code>tar<\/code> command makes backing up Linux relatively easy, and it&#8217;s powerful and reliable too.<\/p>\n<p>When backing up our school district email system, however, I faced a different challenge. Backups couldn&#8217;t occur during the workday or early evening because people were using the system. The backup had to occur after midnight, and it needed to be reliable. I was used to Windows Task Manager, but what was I going to use on Linux? That&#8217;s when I learned about cron.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More Linux resources<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/linux-commands-cheat-sheet\/?intcmp=70160000000h1jYAAQ\">Linux commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/advanced-linux-commands\/?intcmp=70160000000h1jYAAQ\">Advanced Linux commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/rh024-red-hat-linux-technical-overview?intcmp=70160000000h1jYAAQ\">Free online course: RHEL technical overview<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-networking?intcmp=70160000000h1jYAAQ\">Linux networking cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-selinux?intcmp=70160000000h1jYAAQ\">SELinux cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/linux-common-commands-cheat-sheet?intcmp=70160000000h1jYAAQ\">Linux common commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/resources\/what-are-linux-containers?intcmp=70160000000h1jYAAQ\">What are Linux containers?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux?intcmp=70160000000h1jYAAQ\">Our latest Linux articles<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>Scheduling tasks on Linux with cron<\/h2>\n<p>Cron is a daemon used to execute scheduled commands automatically. Learning how to use cron required some reading and experimenting, but soon I was using cron to shut down our email server, back up the data in a compressed tar file, then restart the email service at 3AM.<\/p>\n<p>The commands for a cron job are stored in the crontab file on a Linux system, which is usually found in \/etc\/crontab. Display the contents of your crontab file with <code>$ crontab -l<\/code>.<\/p>\n<p>Edit the crontab file with <code>$ crontab -e<\/code>.<\/p>\n<p>Some systems default to the <a href=\"https:\/\/opensource.com\/article\/20\/12\/vi-text-editor\">Vi editor<\/a> for cron editing. You can override this setting using <a href=\"https:\/\/opensource.com\/article\/19\/8\/what-are-environment-variables\">environment variables<\/a>:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span><span class=\"re2\">EDITOR<\/span>=<span class=\"kw2\">nano<\/span> crontab <span class=\"re5\">-e<\/span><\/code><\/span><\/pre>\n<p>This allows you to use the <a href=\"https:\/\/opensource.com\/article\/20\/12\/gnu-nano\">nano editor<\/a> to edit your personal crontab (if you don&#8217;t have one yet, one is created automatically for you).<\/p>\n<p>All crontab commands have parameters denoted by an asterisk until you insert an integer value. The first represents minutes, then hours, day of the month, month of the year, and finally, day of the week.<\/p>\n<p>Comments are preceded by a hash. Cron ignores comments, so they&#8217;re a great way to leave yourself notes about what a command does and why it&#8217;s important.<\/p>\n<h2>A sample cron job<\/h2>\n<p>Suppose you want to scan your home directory for viruses and malware with <a href=\"https:\/\/opensource.com\/business\/15\/9\/keeping-your-linux-system-safe\">clamscan<\/a> every week on Monday at 10AM. You also want to back up your home directory every week on Tuesday at 9AM. Using cron and crontab files ensures that your system maintenance occurs every week whether you remember to run those utilities or not.<\/p>\n<p>Edit your crontab file to include the following, using your own username instead of &#8220;don&#8221; (my user name):<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\"><span class=\"co0\"># Scan my home directory for viruses<\/span><br><span class=\"nu0\">0<\/span> <span class=\"nu0\">10<\/span> <span class=\"sy0\">*<\/span> <span class=\"sy0\">*<\/span> <span class=\"nu0\">1<\/span> clamscan <span class=\"re5\">-ir<\/span> <span class=\"sy0\">\/<\/span>home<span class=\"sy0\">\/<\/span>don<br><span class=\"co0\"># Backup my home directory<\/span><br><span class=\"nu0\">0<\/span> <span class=\"nu0\">9<\/span> <span class=\"sy0\">*<\/span> <span class=\"sy0\">*<\/span> <span class=\"nu0\">2<\/span> <span class=\"kw2\">tar<\/span> <span class=\"re5\">-zcf<\/span> <span class=\"sy0\">\/<\/span>var<span class=\"sy0\">\/<\/span>backups<span class=\"sy0\">\/<\/span>home.tgz <span class=\"sy0\">\/<\/span>home<span class=\"sy0\">\/<\/span>don<\/div><\/div><\/pre>\n<p>If you&#8217;re using the nano editor, save your work with <b>Ctrl+O<\/b> to write the file out and <b>Ctrl+X<\/b> to exit the editor. After editing the file, use <code>crontab -l<\/code> to list the contents of your cron file to ensure that it has been properly saved.<\/p>\n<p>You can create crontab jobs for any job required on your system. This takes full advantage of the cron daemon.<\/p>\n<h2>Scheduling from the Linux command line<\/h2>\n<p>It&#8217;s no secret that the hardest part of cron is coming up with the right values for those leading asterisks. There are websites, like <a href=\"https:\/\/crontab.guru\/\" target=\"blank\" rel=\"noopener\">crontab.guru<\/a>, that dynamically translate cron time into human-readable translations, and Opensource.com has a <a href=\"https:\/\/opensource.com\/downloads\/linux-cron-cheat-sheet\">cron cheat sheet<\/a> you can download to help you keep it straight.<\/p>\n<p>Additionally, most modern cron systems feature shortcuts to common values, including:<\/p>\n<ul>\n<li><code>@hourly <\/code>: Run once an hour (0 * * * *)<\/li>\n<li><code>@daily<\/code> : Run once a day (0 0 * * *)<\/li>\n<li><code>@weekly<\/code> : Run once a week (0 0 * * 0)<\/li>\n<li><code>@monthly<\/code> : Run once a month (0 0 1 * *)<\/li>\n<li><code>@reboot<\/code> : Run once after reboot<\/li>\n<\/ul>\n<p>There are also alternatives to cron, including <a href=\"https:\/\/opensource.com\/article\/21\/2\/linux-automation\">anacron<\/a> for jobs you want to run regularly but not according to a specific schedule, and the <code>at<\/code> <a href=\"https:\/\/opensource.com\/article\/21\/8\/linux-at-command\">command<\/a> for one-off jobs.<\/p>\n<p>Cron is a useful task scheduling system, and it&#8217;s as easy to use as editing text. Give it a try!<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Try this way to conquer challenging scheduling problems right from the Linux command line.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/03\/checklist_todo_clock_time_team.png\" width=\"520\" height=\"292\" alt=\"Team checklist\" title=\"Team checklist\" loading=\"lazy\"><\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux\" hreflang=\"en\">Linux<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/03\/cc-by-sa--14.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scheduling tasks with the Linux cron command Don Watkins Mon, 03\/28\/2022 &#8211; 03:00 Up Register or Login to like. Early in my Linux journey, I came to appreciate the numerous command-line utilities of the operating system and the way they streamlined regular tasks. For example, backing up applications on my Windows server frequently required expensive [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":63221,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-63220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/63220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=63220"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/63220\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/63221"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=63220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=63220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=63220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}