简体   繁体   中英

How to run a php script in cron

I have found many questions and articles about this but i still have some difficulties. I'm using the following command /usr/bin/php home/domain.com/public_html/cron/script.php I receive the following error Status: 404 Not Found X-Powered-By: PHP/5.2.8 Content-type: text/html

No input file specified.

i'm using Cpanel, the file is hosted on domain.com/cron/script.php Anyideas, thanks :p

Put a leading slash on the script name, ie

/usr/bin/php /home/domain.com/public_html/cron/script.php

Unless you actually intend to run the script through the web, as in lacqui's answer, and you don't mind random third parties being able to run it any time they like, there's no reason you should put it inside your public_html directory; quite the opposite.

Try:

wget -O - http://domain.com/cron/script.php

and see if you get a better result.

Edit: added "- O - " to not write output to home folder.

I'm realising that it is an old question and that you may have found a solution but none of the answers above helped me and I was getting the same 404 error when I was running a cron script.

The problem was related to the way in which the path to the php script was written. The path must start from public_html like this /usr/bin/php public_html/public/index.php

您可能需要使用称为 php-cli 的二进制文件,而不仅仅是 php。

In several shared hosting wget and curl commands are not available from cron. If one wants to execute a web (http) request from cron, then it can be done by calling the desired web url as php curl inside cron php script.

Below is an example code to be put inside cron php file:

<?php
function callRemoteHttp($url)
{

    $curl = curl_init();    

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);
    $ret_arr = array('data' => $result, 'status_code' => curl_getinfo($curl, CURLINFO_HTTP_CODE));
    curl_close($curl);

    return $ret_arr;
}
$ret = callRemoteHttp('http://example.com?param1=value1&param2=value2');
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM