0

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('[email protected]','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?

3
  • Can you give us more detail? Show us how you define the cron, etc.? Commented Jan 17, 2017 at 16:33
  • Also check your systems log files, cron logs its activity and result unless you specifically suppress that... Commented Jan 17, 2017 at 16:38
  • the above code is inside a file named scriptTestForCron.php which I put inside of my cron.hourly folder. inside of my crontab file states: # run-parts 15 * * * * root run-parts /etc/cron.hourly is that what you mean? Commented Jan 17, 2017 at 16:44

1 Answer 1

0

How to run a php script using cron?

On linux create acrontab 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
Sign up to request clarification or add additional context in comments.

10 Comments

So your saying to not put the file in my cron.daily and create my own timing for it in my crontab?
@Eric cron.daily did daily run every hour? Im working a lot with cronjobs and i only useing the crontab function under linux. And it works fine.
I'm sorry I meant to type cron.hourly cron.hourly is ran every 15 minutes I believe since the crontab file states: # run-parts 15 * * * * root run-parts /etc/cron.hourly
@Eric Ok, then mostly this part of my answer helps a little >> /path/to/logfile.log 2>&1 this part pipes all output and errors of PHP into a given log file.
I don't think I should add that to cron.hourly since there are other things being done by it and sent to logs, should I create a seperate entry under "#run-parts" and put what you said?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.