{"id":173,"date":"2014-12-17T17:20:15","date_gmt":"2014-12-17T17:20:15","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=173"},"modified":"2014-12-17T17:55:20","modified_gmt":"2014-12-17T17:55:20","slug":"cron-overlapping","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/miscellaneous\/cron-overlapping\/","title":{"rendered":"Prevent CRON from overlapping"},"content":{"rendered":"<p>To solve situations where CRON starts another copy before the current one finishes. <\/p>\n<p>At the top of the CRON job use:<\/p>\n<pre class='wp-code-highlight prettyprint'>\r\n\/\/check if already running\r\n$buff = file_get_contents(\"cron_stat.txt\");\r\nif($buff == 1)\r\n{\r\n    die();\r\n}\r\n\/\/set a running flag;\r\nfile_put_contents(\"cron_stat.txt\",\"1\");\r\n......\r\n......\r\n......\r\n<\/pre>\n<p>When the CRON will run for the first time &#8220;cron_stat.txt&#8221; will be blank so a 1 will be written in &#8220;cron_stat.txt&#8221; indicating that the CRON is running and the script will proceed with its work.<br \/>\nWhen the CRON will start again (second time onwards) it will check &#8220;cron_stat.txt&#8221; and if it finds there is a 1 in it the script it will exit. And if it finds a 0 then it will proceed.<\/p>\n<p>At the end of the file use:<\/p>\n<pre class='wp-code-highlight prettyprint'>\r\nfile_put_contents(\"cron_stat.txt\",\"0\");\r\n<\/pre>\n<p>This will write a 0 in the &#8220;cron_stat.txt&#8221; file indicating the CRON is not running. <\/p>\n<p>So in short at the start of the script set 1 in a flag indicating the CRON is running and at the end of the script set the flag to 0 indicating CRON has finished. <\/p>\n<p>There are ways to implement this through Linux commands, but didn&#8217;t work for me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To solve situations where CRON starts another copy before the current one finishes. At the top of the CRON job use: \/\/check if already running $buff = file_get_contents(&#8220;cron_stat.txt&#8221;); if($buff == 1) { die(); } \/\/set a running flag; file_put_contents(&#8220;cron_stat.txt&#8221;,&#8221;1&#8243;); &#8230;&#8230; &#8230;&#8230; &#8230;&#8230; When the CRON will run for the first time &#8220;cron_stat.txt&#8221; will be blank&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/miscellaneous\/cron-overlapping\/\">Continue reading <span class=\"screen-reader-text\">Prevent CRON from overlapping<\/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":[1,3],"tags":[],"class_list":["post-173","post","type-post","status-publish","format-standard","hentry","category-miscellaneous","category-php","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":2,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/173\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}