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('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?

Community
  • 1
  • 1
Eric
  • 25
  • 4
  • Can you give us more detail? Show us how you define the cron, etc.? – Jay Blanchard Jan 17 '17 at 16:33
  • Also check your systems log files, cron logs its activity and result _unless you specifically suppress that..._ – arkascha Jan 17 '17 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? – Eric Jan 17 '17 at 16:44

1 Answers1

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
JustOnUnderMillions
  • 3,741
  • 9
  • 12
  • So your saying to not put the file in my cron.daily and create my own timing for it in my crontab? – Eric Jan 17 '17 at 16:48
  • @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. – JustOnUnderMillions Jan 17 '17 at 16:51
  • 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 Jan 17 '17 at 16:53
  • @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. – JustOnUnderMillions Jan 17 '17 at 16:55
  • 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? – Eric Jan 17 '17 at 17:03
  • http://stackoverflow.com/questions/31767818/running-a-php-script-with-crontab, im out :) – JustOnUnderMillions Jan 17 '17 at 17:04
  • I have never used inotify either nor did I know it existed – Eric Jan 17 '17 at 17:05
  • @Eric Your Question was to complex in the first time. There are some many parts that can go wrong (and you dont have any real error message for us). So i tryed to help to log all output from php into a logfile. But what if Email sned not working or other stuff is broken (rights to access files as given user). So im sorry, but cant really help more here. test, Debug, Collect Data and reopen an detail question with errros and so on .... – JustOnUnderMillions Jan 17 '17 at 17:16
  • I am new to this, and am unsure on everything you have mentioned.You stated to create a crontab entry crontab -e to which I'm assuming means to just create a new line in the crontab file. In your example you put /path/to/logfile.log 2>&1 and I do not know what the 2>&1 mean or if it has to be sent to a specific log file or if I can just make a new one for easy access. – Eric Jan 17 '17 at 17:22
  • `2>&1` Read here: http://stackoverflow.com/questions/2342826/how-to-pipe-stderr-and-not-stdout#2342841 im really out now. – JustOnUnderMillions Jan 17 '17 at 17:24