{"id":64,"date":"2011-05-12T10:30:45","date_gmt":"2011-05-12T08:30:45","guid":{"rendered":"http:\/\/en.nisi.ro\/blog\/?p=64"},"modified":"2014-04-23T08:32:54","modified_gmt":"2014-04-23T06:32:54","slug":"progress-bar-long-running-cron-scripts-php","status":"publish","type":"post","link":"https:\/\/en.nisi.ro\/blog\/development\/php\/progress-bar-long-running-cron-scripts-php\/","title":{"rendered":"&#8220;Progress bar&#8221; for long running cron scripts in PHP"},"content":{"rendered":"<p>Long running scripts can be a pain if you don&#8217;t know how much will be running. If you need to see a progress and flush() or ob_flush() do not work because of browser cache you can implement an &#8220;email progress bar&#8221;.<\/p>\n<p><strong>PHP example:<!--more--><\/strong><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">$step = 0;\r\n$last_percent = 0;\r\n$total_rows =\r\nwhile(1) {\r\n   $step++; \/\/count step\r\n\r\n   \/\/ here should be is the code witch consume time for every loop\r\n   \/\/ end consuming time code\r\n\r\n   $percent= ($step * 100) \/ $total_rows;\r\n   if ($last_percent != round($percent)){\r\n      \/\/ because you do not want to receive an email for every step\r\n      $last_percent = round($percent);\r\n      if (($last_percent % 10) == 0){\r\n         \/\/ this will send email from 10 % to 10 % - total 10 emails\r\n         mail(your_email_here, 'Progress '.$last_percent.'% done' , &quot;Progress: &quot;.$contor.&quot;\/&quot;.$total_rows);\r\n      }\r\n   }\r\n\r\n} \/\/ end while\r\n\r\n<\/pre>\n<p>As you can see the idea is very simple and you can implement it in any language.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Long running scripts can be a pain if you don&#8217;t know how much will be running. If you need to see a progress and flush() or ob_flush() do not work because of browser cache you can implement an &#8220;email progress bar&#8221;. PHP example:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[82,83,84,85,86],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-php","tag-cron","tag-cron-progress","tag-email-progress-bar","tag-long-running","tag-script"],"_links":{"self":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":2,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":66,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions\/66"}],"wp:attachment":[{"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/en.nisi.ro\/blog\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}