{"id":60,"date":"2008-08-04T04:30:40","date_gmt":"2008-08-04T09:30:40","guid":{"rendered":"http:\/\/yourlinuxguy.com\/?p=60"},"modified":"2008-07-29T09:09:14","modified_gmt":"2008-07-29T14:09:14","slug":"how-can-i-set-up-a-cron-job-scheduled-task-quickly","status":"publish","type":"post","link":"https:\/\/yourLinuxGuy.com\/?p=60","title":{"rendered":"How can I set up a cron job (scheduled task) quickly?"},"content":{"rendered":"<p>So you have a task that you want to run regularly on Linux\/Unix. It could be a single commend, or a script that you prepared in advance that contains all the command you need run. What you need to do is to set up a &#8220;Cron job&#8221;. Cron is a system that, when the <code>cron<\/code> program is running, reads a &#8220;tab&#8221; file for the instructions it needs, so that it can know *when* to execute *what*.<\/p>\n<p><strong>How cron works &#8211; Old School<\/strong><\/p>\n<p>The standard &#8220;Old School&#8221; design has <code>cron<\/code> reading a tab file that is named after any given valid user (we&#8217;ll just focus on root for now), in a directory like:<\/p>\n<p><code>\/var\/spool\/cron\/tabs<\/code> or <code>\/var\/spool\/cron\/crontab<\/code><\/p>\n<p>&#8230;depending on your distro\/version. The tab file contains single-line instructions in the form of:<\/p>\n<p><code>minute hour day-of-month month day-of-week command<\/code><\/p>\n<p>&#8230;whereas the smallest increment of time <code>cron<\/code> can utilize is one minute, and &#8220;command&#8221; is either your single command or a script, preferably with the full path. Asterisks can be used in place of any given date\/time stamp. For much more detail on the <code>crontab<\/code> layout, see &#8220;<code>man 5 crontab<\/code>&#8220;&#8230; and don&#8217;t forget the <code>5<\/code>!<\/p>\n<p>Here is an example. Say you had written a script that you wanted to run every morning at 6:10am. You would have the following in your <code>crontab<\/code>:<\/p>\n<p><code>10 6 * * * \/home\/jpavlov\/bin\/myScript.bash<\/code><\/p>\n<p><strong>How cron works &#8211; Modern<\/strong><\/p>\n<p><em>Most of this post will focus on the &#8220;Old School&#8221; <code>cron<\/code> method, except for this section.<\/em> The modern method of <code>cron<\/code> is very similar, and in fact on many distros, you have both methods available to you. In this version though, the <code>cron<\/code> tab file is located at has a slightly place (<code>\/etc\/crontab<\/code>) that does not directly matter to you, because you do not enter items in this tab to get them executed. Because this tab is nothing but a &#8220;caller&#8221; to other scripts that execute items in a series of other directories, all under <code>\/etc\/<\/code>: <code>cron.hourly<\/code>, <code>cron.daily<\/code>, <code>cron.weekly<\/code>, and <code>cron.monthly<\/code>. Any executable files in these directories will be executed at the interval after which they are named.<\/p>\n<p>So, for instance, if you wanted a script to run daily, you can just copy the script into the <code>\/etc\/cron.daily<\/code> directory (or create a link to it from there with <code>ln -s<\/code>) and it will be run as <code>root<\/code> at the next daily interval. If you only want a command executed, you can either add it to <code>\/etc\/crontab<\/code> (not recommended, and it has a slightly different syntax, which I&#8217;m not going into), or just create a two-line script and do what I already described.<\/p>\n<p>Ah, but &#8220;what *time* to they start&#8221;, you ask? This is dictated (or not) by entries in the <code>\/etc\/sysconfig\/cron<\/code> file, and I&#8217;ll allow you to read that and spare you more detail here. Just know that if you haven&#8217;t modified that file, the jobs run shortly after you boot, and the <code>cron<\/code> clock is maintained off that time forward.<\/p>\n<p><strong>How to view or edit the <code>crontab<\/code><\/strong><\/p>\n<p>To view (list) the &#8220;Old School&#8221; <code>crontab<\/code>, just do this:<\/p>\n<p><code>crontab -l -u user<\/code><\/p>\n<p>&#8230;and you can leave off the &#8220;<code>-u user<\/code>&#8221; if you want to view your own crontab.<\/p>\n<p>DO NOT edit the <code>crontab<\/code> files directly (unless you *really* know what you&#8217;re doing). To edit the <code>crontab<\/code> file, do this:<\/p>\n<p><code>crontab -e -u user<\/code><\/p>\n<p>This will launch an editor for you to use to modify the <code>crontab<\/code> file. By design, <code>cronta<\/code>b on Linux\/Unix machines will use the editor specified by the system variable &#8220;<code>EDITOR<\/code>&#8221; to open the file. You can find out which one you have set (likely <code>vi<\/code>\/<code>vim<\/code>) by typing <code>echo $EDITOR<\/code>. If none is specified, it is <code>vi<\/code>\/<code>vim<\/code>.<\/p>\n<p>If you don&#8217;t like <code>vi<\/code>, and prefer <code>gedit<\/code> or something like it, you can modify that variable like this:<\/p>\n<p><code>EDITOR=gedit;export EDITOR<\/code><\/p>\n<p>&#8230;which can be made system-wide permanent by adding it to <code>\/etc\/profile<\/code> or similar.<\/p>\n<p><strong>Email behavior<\/strong><\/p>\n<p>I just want to mention how <code>cron<\/code> integrates email notifications&#8230; When a job in the <code>crontab<\/code> is executed, if there is any output, by default it is sent to <code>root<\/code> (as dictated by your system, and any aliases, bla bla bla). A couple important points about that:<\/p>\n<ol>\n<li>The email destination may be changed *per <code>crontab<\/code>*, not per job. Just change (or add) a line like this to the top of the tab: <code>MAILTO=jpavlov@yourLinuxGuy.com<\/code><\/li>\n<li>Also, your command or script might dump a lot of output (like debugging) that you may not want sent in a result email. So, the output of a job can be suppressed by adding a redirector at the end of the job line. I&#8217;ll demonstrate by updating the <code>crontab<\/code> line from above with the redirector (all on one line):<\/li>\n<\/ol>\n<p><code>10 6 * * * \/home\/jpavlov\/bin\/myScript.bash &gt; \/dev\/null 2&gt;&amp;1<\/code><\/p>\n<p>And that&#8217;s our lesson for today folks. Phew, that was a lot, but it really isn&#8217;t that complicated once you&#8217;re used to it&#8230; like anything else, I suppose.<\/p>\n<p>If I missed anything, make sure and let me know!<\/p>\n<p>\ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So you have a task that you want to run regularly on Linux\/Unix. It could be a single commend, or a script that you prepared in advance that contains all the command you need run. What you need to do&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/yourLinuxGuy.com\/?p=60\">Read more<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[46,11,47],"tags":[],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-cron","category-intermediate","category-linuxgeneral"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnjn1-Y","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=60"}],"version-history":[{"count":4,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":64,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions\/64"}],"wp:attachment":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}