简体   繁体   中英

How to run a php script using cron?

I am trying to run a script every hour that check if a file has been changed, and if it has then it sends an email. So I found in another post ( PHP - Email notification whenever a remote file changes ) this:

$url='http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json';
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$execute = curl_exec($ch);

$fp=fopen('old.json','w+');
$oldjson=fread($fp,filesize('old.json'));

if($execute!=$oldjson){
 mail('your@mail.com','Yoohoo', 'File changed');
 fputs($fp,$execute);
}
fclose($fp);

and placed both files (the blank old.json and scriptTestForCron.php) in my cron.hourly folder. I changed the url given in the example to the filepath to the file I want to see if is changed, as well as the mailing information. The problem is that it does not notify me that it failed, and does not send an email so I do not know if it actually ran or not. Is there any way I could check? Also what reason could it not currently be working?

I have attempted to change my crontab file to run this and send logs to a specific file but have not been able to receive anything yet.

# run-parts
15 * * * * root run-parts /etc/cron.hourly

# php
1 * * * * php /etc/cron.hourly/scriptTestForCron.php >> /var/log/testLog

Shouldn't one of those two lines be running the script file?

How to run a php script using cron?

On linux create a crontab entry, crontab -e

* 24/1 * * * php /path/to/script.php >> /path/to/logfile.log 2>&1
  • /path/to/logfile.log holds all messages/errors from the script

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