{"id":13710,"date":"2015-03-25T05:06:18","date_gmt":"2015-03-24T18:06:18","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=13710"},"modified":"2015-03-25T05:06:18","modified_gmt":"2015-03-24T18:06:18","slug":"linux-crontab-considerations-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-crontab-considerations-tutorial\/","title":{"rendered":"Linux crontab Considerations Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_strong_morereadable_best.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Linux crontab Considerations Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_strong_morereadable_best.jpg\" title=\"Linux crontab Considerations Tutorial\" id='ilcct' onmouseover=\"  this.src=this.src.replace('_strong_morereadable_best.j','_alternative_interactive.xj').replace('_alternative_interactive.j','_weakest.xj').replace('_weakest.j','_strong_lessreadable.xj').replace('_strong_lessreadable.j','_strong_morereadable_best.xj').replace('.x','.');  \"   \/><\/a><p class=\"wp-caption-text\">Linux crontab Considerations Tutorial<\/p><\/div>\n<p>The <a target=_blank title='crontab information' href='http:\/\/www.computerhope.com\/unix\/ucrontab.htm'>crontab<\/a> functionality in Linux or Unix is just great.   It is <a target=_blank title='Batch processing information from Wikipedia' href='http:\/\/en.wikipedia.org\/wiki\/Batch_processing'>batch processing<\/a>, doing things while you are not there.<\/p>\n<p>This same crontab strength is also, at times, a source of its weakness &#8230; what if something goes wrong (and &#8230; doh &#8230; you won&#8217;t be there)?   What if one process relies for its input on the successful completion of another process, and that process can occasionally fail, for some odd reason, or runs longer than expected?<\/p>\n<p>Well, even if this can happen, and you haven&#8217;t catered for every eventuality in the scripting (we&#8217;re using <a target=_blank title='Korn Shell information from Wikipedia ... thanks' href='http:\/\/en.wikipedia.org\/wiki\/Korn_shell'>Korn Shell<\/a> scripting today) there are some better &#8220;approaches&#8221; to this, (in contrast to some other approaches), with regard to your &#8220;crontab&#8221; usage design considerations.<\/p>\n<p>Before we get to that, though &#8230; why would it be that &#8220;you haven&#8217;t catered for every eventuality&#8221;?  Well, the usual situation is that the code to cater for &#8220;every eventuality&#8221; would make your code hundreds of times bigger than you first thought, and think practicalities should come into play here, and assess programs on a risk analysis basis &#8230; ie. is it the end of the world if this process very occasionally fails, or displays data that is old but &#8220;true&#8221;?<\/p>\n<p>Okay, today we are simulating on our http:\/\/localhost:888\/ MAMP Linux web server, something like the rjmprogramming.com.au domain does with respect to its &#8220;All Posts&#8221; &#8220;Code Download Table&#8221; (we talked about with <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=9686' title='PHP Blog Summary Follow Up Tutorial'>PHP Blog Summary Follow Up Tutorial<\/a>), and show:<\/p>\n<ol>\n<li>an interactive run &#8230; via command line <i>.\/basegetmelist.ksh<\/i> &#8230; using our <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/basegetmelist.ksh_GETME\" title=\"basegetmelist.ksh\">basegetmelist.ksh<\/a> &#8230; containing &#8230;<br \/>\n<code><br \/>\n#!\/bin\/ksh<br \/>\nfind \/Applications\/MAMP\/htdocs\/ -name '*GETME' &gt; \/tmp\/basegetmelist.wis 2&gt; \/dev\/null<br \/>\ncurl http:\/\/localhost:8888\/getmelist_localhost.php<br \/>\nexit<br \/>\n<\/code><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_alternative_interactive.jpg\" title=\"an interactive run\" onclick=\"window.open('http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_alternative_interactive.jpg','_blank'); \">\n<\/li>\n<li>a not ideal crontab scenario (lacking &#8220;bulletproofness&#8221;) &#8230; (ie. what if the &#8220;find&#8221; command should fail, or run past one minute) &#8230;<br \/>\n<code><br \/>\n45 12 * * * find \/Applications\/MAMP\/htdocs\/ -name '*GETME' &gt; \/tmp\/basegetmelist.wis 2&gt; \/dev\/null<br \/>\n46 12 * * * curl http:\/\/localhost:8888\/getmelist_localhost.php<br \/>\n<\/code><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_weakest.jpg\" title=\"a not ideal crontab scenario (lacking 'bulletproofness')\"onclick=\"window.open('http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_weakest.jpg','_blank'); \">\n<\/li>\n<li>a good crontab approach (but not very readable in &#8220;crontab -l&#8221;) &#8230;<br \/>\n<code><br \/>\n38 12 * * * \/Applications\/MAMP\/htdocs\/basegetmelist.ksh<br \/>\n<\/code><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_strong_lessreadable.jpg\" title=\"a good crontab approach (but not very readable in 'crontab -l')\"onclick=\"window.open('http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_strong_lessreadable.jpg','_blank'); \">\n<\/li>\n<li>a good crontab approach (and very readable in &#8220;crontab -l&#8221;) &#8230;<br \/>\n<code><br \/>\n34 12 * * * ksh -c \"find \/Applications\/MAMP\/htdocs\/ -name '*GETME' &gt; \/tmp\/basegetmelist.wis 2&gt; \/dev\/null ; curl http:\/\/localhost:8888\/getmelist_localhost.php > \/tmp\/ok.ok 2> \/tmp\/notok.notok\"<br \/>\n<\/code><br \/>\n<img decoding=\"async\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_strong_morereadable_best.jpg\" title=\"a good crontab approach (and very readable in 'crontab -l')\"onclick=\"window.open('http:\/\/www.rjmprogramming.com.au\/Linux\/cron\/methods\/crontab_strong_morereadable.jpg','_blank'); \">\n<\/li>\n<\/ol>\n<p>There is another approach that would be to make the &#8220;find&#8221; functionality happen in the &#8220;curl&#8221; PHP, and that is okay, but there is an argument to say that PHP <a target=_blank title='PHP exec method' href='http:\/\/php.net\/manual\/en\/function.exec.php'>exec<\/a> usage can be overplayed, and of course we lose that obvious &#8220;crontab -l&#8221; readability of that last idea above.   Such approaches to crontab jobs can help you sleep at night, in that if something fails, at least something a bit dumb is less likely to be the end result displayed.   We don&#8217;t want too much <a target=_blank title='Dumb and Dumber trailer' href='http:\/\/www.youtube.com\/watch?v=lGXHVlEklgQ'>&#8220;Dumb and Dumber&#8221;<\/a> thanks.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d13710' onclick='var dv=document.getElementById(\"d13710\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=linux\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d13710' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The crontab functionality in Linux or Unix is just great. It is batch processing, doing things while you are not there. This same crontab strength is also, at times, a source of its weakness &#8230; what if something goes wrong &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-crontab-considerations-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/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":[12,29,33,37],"tags":[126,135,137,234,272,274,284,405,677,707,744,841,885,932,997,1106,1166,1319,1339,1411],"class_list":["post-13710","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-software","category-tutorials","tag-background","tag-batch","tag-batch-processing","tag-command-line","tag-cron","tag-crontab","tag-curl","tag-exec","tag-korn-shell","tag-linux","tag-mamp","tag-nohup","tag-operating-system-2","tag-php","tag-programming","tag-scripting","tag-software-2","tag-tutorial","tag-unix","tag-web-server"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/13710"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=13710"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/13710\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=13710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=13710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=13710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}