{"id":54,"date":"2008-04-19T14:46:28","date_gmt":"2008-04-19T19:46:28","guid":{"rendered":"http:\/\/home.iseeancestors.com\/blog\/?p=54"},"modified":"2008-04-19T14:52:08","modified_gmt":"2008-04-19T19:52:08","slug":"calling-a-php-script-from-crontab","status":"publish","type":"post","link":"https:\/\/home.iseeancestors.com\/blog\/2008\/04\/19\/calling-a-php-script-from-crontab\/","title":{"rendered":"Calling a PHP Script from Crontab"},"content":{"rendered":"<p>If you use a shared-hosting web service, it may be more difficult to run a PHP script through Cron (Crontab).\u00a0 If your script uses MySQL calls for example, your hosting provider may not provide access to the MySQL library when your script runs through Cron.\u00a0 Here&#8217;s a\u00a0method that works just as well.<\/p>\n<p><!--more--><\/p>\n<p>Normally you might have a cron entry like this:<\/p>\n<p><code>10,0,*,*,*,\/usr\/bin\/php \/path\/to\/myscript.php<\/code><\/p>\n<p>But if the script contains calls to MySQL, you might get the error message:<\/p>\n<p><code>PHP Fatal error: Call to undefined function mysql_connect()<\/code><\/p>\n<p>In this case your hosting provider does not offer MySQL access when your script is executed through Cron.\u00a0 You can try placing the following code as the first line in your script file:<\/p>\n<p><code>#!\/usr\/bin\/php -q<\/code><\/p>\n<p>and then change your cron entry to:<\/p>\n<p><code>10,0,*,*,*,\/path\/to\/myscript.php<\/code><\/p>\n<p>But you might get the error:<\/p>\n<p><code>\/usr\/bin\/php: bad interpreter: No such file or directory<\/code><\/p>\n<p>Perhaps you can figure out the correct directory to specify for the PHP interpretter.\u00a0 A simpler method is to use cURL.\u00a0 This utility executes your script as if it were retrieved via an Internet browser.\u00a0 Remove the<\/p>\n<p><code>#!\/usr\/bin\/php -q<\/code><\/p>\n<p>line from your script and change your Cron entry to:<\/p>\n<p><code>10,0,*,*,*,\/usr\/bin\/curl -s http:\/\/www.example.com\/myscript.php<\/code><\/p>\n<p>You&#8217;ll probably want to do a<\/p>\n<p><code>chmod 760<\/code><\/p>\n<p>on myscript.php so the outside world cannot read or execute it.\u00a0 The -s parameter tells cURL to run in silent mode.\u00a0 Otherwise you&#8217;ll get an e-mail with a progress indicator every time the script runs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you use a shared-hosting web service, it may be more difficult to run a PHP script through Cron (Crontab).\u00a0 If your script uses MySQL calls for example, your hosting provider may not provide access to the MySQL library when your script runs through Cron.\u00a0 Here&#8217;s a\u00a0method that works just as well.<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-software"],"_links":{"self":[{"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":0,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"wp:attachment":[{"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/home.iseeancestors.com\/blog\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}