{"id":50106,"date":"2020-08-28T03:01:04","date_gmt":"2020-08-27T17:01:04","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=50106"},"modified":"2020-08-27T13:56:07","modified_gmt":"2020-08-27T03:56:07","slug":"chat-no-sockets-cron-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/chat-no-sockets-cron-tutorial\/","title":{"rendered":"Chat No Sockets Cron Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_listener.php\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Chat No Sockets Cron Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/chat_cron.gif\" title=\"Chat No Sockets Cron Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Chat No Sockets Cron Tutorial<\/p><\/div>\n<p>Yesterday&#8217;s <a title='Chat No Sockets Primer Tutorial' href='#cnspt'>Chat No Sockets Primer Tutorial<\/a> had a &#8220;command line&#8221; PHP usage component, we wonder whether you noticed?   We intend to keep this arrangement for our &#8220;no sockets&#8221; Chat web application.  It will not function as that &#8220;Chat&#8221; without the command line part of the &#8220;equation&#8221; being activated.  You might think of it as the &#8220;traffic cop&#8221; of the web application.<\/p>\n<p>As far as that &#8220;command line&#8221; PHP usage goes &#8230;<\/p>\n<ul>\n<li>Isn&#8217;t it great to have the one code source for all this?<\/li>\n<li>How are we going to manage this command line usage, out of &#8230;\n<ol>\n<li>interactive in an interactive command line session<\/li>\n<li>set off a background process run of it via the &#8220;&#038;&#8221; suffix<\/li>\n<li>cron<font size=1>tab<\/font> it (on our Linux web server)<\/li>\n<\/ol>\n<p>?<\/li>\n<\/ul>\n<p>Suggestion 1 is kludgy, over the top and awkward to arrange for any long period of time, though useful if non-continuous &#8220;process coverage&#8221; is the go.<\/p>\n<p>Suggestion 2 and 3 are great for &#8220;continuous process coverage&#8221; (we privately think of as &#8220;jigsaw coverage&#8221;), but in our books <font size=2>(and the pamphlettes have scarpered it to <a target=_blank title='?' href='https:\/\/www.google.com\/maps\/search\/pamphlette+island\/@-33.9112506,151.1139931,12z\/data=!3m1!4b1'>Pamphlette Island<\/a><\/font><font size=1> &#8230; which they must intend on &#8220;founding&#8221;?!<\/font><font size=2>)<\/font> cron<font size=1>tab<\/font> is a better option to take for at least two reasons &#8230;<\/p>\n<ul>\n<li>cron<font size=1>tab<\/font> resurrects itself on a system reboot<\/li>\n<li>cron<font size=1>tab<\/font> is self documenting (an important advantage regarding command line processing usage)<\/li>\n<\/ul>\n<p> &#8230; but if we are to use cron<font size=1>tab<\/font> &#8220;jigsaw coverage&#8221;, to avoid &#8220;jigsaw overlap&#8221; we&#8217;re going to have to change &#8230; regarding &#8230;<\/p>\n<blockquote style='width:90%;' cite='https:\/\/stackoverflow.com\/questions\/18919151\/crontab-day-of-the-week-syntax'><p>\n&nbsp;&nbsp;\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 minute (0 &#8211; 59)<br \/>\n&nbsp;&nbsp; \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 hour (0 &#8211; 23)<br \/>\n&nbsp;&nbsp; \u2502 \u2502 \u250c\u2500\u2500\u2500\u2500\u2500\u2500 day of month (1 &#8211; 31)<br \/>\n&nbsp;&nbsp; \u2502 \u2502 \u2502 \u250c\u2500\u2500\u2500\u2500 month (1 &#8211; 12)<br \/>\n&nbsp;&nbsp; \u2502 \u2502 \u2502 \u2502 \u250c\u2500\u2500 day of week (0 &#8211; 6 => Sunday &#8211; Saturday, or<br \/>\n&nbsp;&nbsp; \u2502 \u2502 \u2502 \u2502 \u2502                1 &#8211; 7 => Monday &#8211; Sunday)<br \/>\n&nbsp;&nbsp; \u2193 \u2193 \u2193 \u2193 \u2193<br \/>\n&nbsp;&nbsp; * * * * * command to be executed\n <\/p><\/blockquote>\n<p> &#8230; thanks to <a target=_blank title='https:\/\/stackoverflow.com\/questions\/18919151\/crontab-day-of-the-week-syntax' href='https:\/\/stackoverflow.com\/questions\/18919151\/crontab-day-of-the-week-syntax'>https:\/\/stackoverflow.com\/questions\/18919151\/crontab-day-of-the-week-syntax<\/a> for above cron<font size=1>tab<\/font> syntax &#8230; as per ( our assumption being that the user will use one of the &#8220;every ? units&#8221; syntax such as <br \/><i>*\/6 * * * * php this_php_happens_every_six_minutes.php<\/i><br \/> ) &#8230;<\/p>\n<table style='width:100%;'>\n<tr>\n<th>To <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/PHP\/php_listener.php-GETME\" title=\"php_listener.php\">&lt;&#8212;<\/a><\/th>\n<th><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/chat_cron.gif\">&lt;&#8212;<\/a> From<\/th>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<code><br \/>\nif ($argv) {  \/\/ command line ...<br \/>\n    $cfindings=\"\";<br \/>\n    $goes=0;<br \/>\n    $howmanygoes=\"-1\";<br \/>\n    $par=getenv(\"TERM\"); \/\/ thanks to <a target=_blank title='https:\/\/stackoverflow.com\/questions\/3214935\/can-a-bash-script-tell-if-its-being-run-via-cron' href='https:\/\/stackoverflow.com\/questions\/3214935\/can-a-bash-script-tell-if-its-being-run-via-cron'>https:\/\/stackoverflow.com\/questions\/3214935\/can-a-bash-script-tell-if-its-being-run-via-cron<\/a><br \/>\n    if (\"$par\" == \"$TERM\") {  \/\/ via cron<br \/>\n      $cfindings1=exec(\"crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 1\");<br \/>\n      $cfindings2=exec(\"crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 2\");<br \/>\n      $cfindings3=exec(\"crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 3\");<br \/>\n      $cfindings4=exec(\"crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 4\");<br \/>\n      $cfindings5=exec(\"crontab -l | grep 'php_listener' | grep -v 'grep' | cut -d ' ' -f 5\");<br \/>\n      if (strpos($cfindings1, \"*\/\") !== false) {<br \/>\n        $howmanygoes=12 * explode(\"*\/\", $cfindings1)[1];<br \/>\n      } else if (strpos($cfindings2, \"*\/\") !== false) {<br \/>\n        $howmanygoes=720 * explode(\"*\/\", $cfindings2)[1];<br \/>\n      } else if (strpos($cfindings3, \"*\/\") !== false) {<br \/>\n        $howmanygoes=17280 * explode(\"*\/\", $cfindings3)[1];<br \/>\n      }<br \/>\n    }<br \/>\n    while (1) {<br \/>\n    foreach (glob(\"chat_*.rls\") as $ourfilename) {<br \/>\n      rename($ourfilename, explode(\".rls\", $ourfilename)[0] . \".line\");<br \/>\n    }<br \/>\n    if ($howmanygoes >= 0) { $goes++; if ($goes &gt;= $howmanygoes) {  exit; } }<br \/>\n    sleep(5);<br \/>\n    }<br \/>\n    exit;<br \/>\n}<br \/>\n<\/code>\n<\/td>\n<td style='vertical-align:top;'>\n<code><br \/>\nif ($argv) {  \/\/ command line ...<br \/>\n    while (1) {<br \/>\n    foreach (glob(\"chat_*.rls\") as $ourfilename) {<br \/>\n      rename($ourfilename, explode(\".rls\", $ourfilename)[0] . \".line\");<br \/>\n    }<br \/>\n    sleep(5);<br \/>\n    }<br \/>\n    exit;<br \/>\n}<br \/>\n<\/code>\n<\/td>\n<\/tr>\n<\/table>\n<p>Again, feel free to try <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/PHP\/php_listener.php-GETME\" title=\"php_listener.php\">the changed<\/a> <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_listener.php-GETME\" title=\"php_listener.php\">php_listener.php<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_listener.php\" title=\"Click picture\">chat web application<\/a> &#8220;second draft&#8221;.<\/p>\n<p><!--p>You can also see this play out at WordPress 4.1.1's <a target=_blank  href='\/\/www.rjmprogramming.com.au\/ITblog\/chat-no-sockets-crontab-tutorial\/'>Chat No Sockets Crontab Tutorial<\/a>.<\/p-->\n<hr>\n<p id='cnspt'>Previous relevant <a target=_blank title='Chat No Sockets Primer Tutorial' href='\/\/www.rjmprogramming.com.au\/ITblog\/chat-no-sockets-primer-tutorial\/'>Chat No Sockets Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_listener.php\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Chat No Sockets Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/chat_no_sockets.jpg\" title=\"Chat No Sockets Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Chat No Sockets Primer Tutorial<\/p><\/div>\n<p>We&#8217;re starting on a new PHP web application project.  We&#8217;ve got a &#8220;first draft&#8221; of a chat web application that does not use sockets, but rather &#8230;<\/p>\n<ol>\n<li>invites somebody else (via &#8220;Invite&#8221; button) via email or SMS<\/li>\n<li>that &#8220;somebody else&#8221; opens the web application via email or SMS links<\/li>\n<li>from then on keep the chat going via &#8220;Send&#8221; buttons in the two chat incarnations<\/li>\n<\/ol>\n<p>It needs more work, that is for sure, but perhaps you want to see <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_listener.php_GETME\" title=\"php_listener.php\">php_listener.php<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/php_listener.php\" title=\"Click picture\">chat web application<\/a> &#8220;first draft&#8221;.<\/p>\n<p>As with good learning between two parties, each listens, takes it in, and replies, as required.<\/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='#d50101' onclick='var dv=document.getElementById(\"d50101\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/chat\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d50101' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\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='#d50106' onclick='var dv=document.getElementById(\"d50106\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/cron\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d50106' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday&#8217;s Chat No Sockets Primer Tutorial had a &#8220;command line&#8221; PHP usage component, we wonder whether you noticed? We intend to keep this arrangement for our &#8220;no sockets&#8221; Chat web application. It will not function as that &#8220;Chat&#8221; without the &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/chat-no-sockets-cron-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,14,29,37],"tags":[126,200,234,272,274,707,2114,932,991,997,1319,1402],"class_list":["post-50106","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-operating-system","category-tutorials","tag-background","tag-chat","tag-command-line","tag-cron","tag-crontab","tag-linux","tag-listener","tag-php","tag-process","tag-programming","tag-tutorial","tag-web-application"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/50106"}],"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=50106"}],"version-history":[{"count":15,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/50106\/revisions"}],"predecessor-version":[{"id":50121,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/50106\/revisions\/50121"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=50106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=50106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=50106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}