Running php script as cronjob having file handling functions

April 4, 2012 - PHP

Problem:
For some reasons i was not able to run the script having file handling functions from crontab, after few experiments on how to run the php script having file handling functions as cron job, have come up with the solution.


script.php
<?php

$fp = fopen(‘expireddaterange.txt’, ‘r’);
$file = file_get_contents(‘expireddaterange.txt’);

if(strlen($file) > 0){
….
}else{
echo ‘File[expireddaterange.txt] is Empty';
exit;
}
?>
expireddaterange.txt contains:
20120206000000,20120221000000

when i run the php script from terminal

[root@xxxx ]# php script.php
the script executed porperly, but when the same script made as cron job entry,

[root@xxxx ]# crontab -e
35 14 * * * /usr/bin/php -d include_path=.:/full path of the script/script.php

And when the script, script.php executed at 2:35 pm, the script was throwing message  “File[expireddaterange.txt] is Empty”, ie., cursor has fall in else part,

Solution:
For this to work, i made changes in the script.php
<?php

$fp = fopen(‘/full path/expireddaterange.txt’, ‘r’, true);
$file = file_get_contents(‘/full path/expireddaterange.txt’, true);
if(strlen($file) > 0){

….
}else{
echo ‘File[expireddaterange.txt] is Empty';
exit;
}
?>

give permission to the file expireddaterange.txt

[root@xxxx ]# chmod 666 /full path/expireddaterange.txt

And the next time the crontab entry

35 14 * * * /usr/bin/php -d include_path=.:/full path of the script/script.php

will work.

Comments

  1. Jan Drenner says:

    Some genuinely good information, Sword lily I noticed this.

  2. I just like the helpful information you provide on your articles. I will bookmark your blog and test again right here regularly. I am somewhat sure I’ll learn plenty of new stuff right here! Best of luck for the next!

  3. You actually make it appear really easy along with your presentation however I in finding this matter to be really something that I think I might never understand. It sort of feels too complicated and very wide for me. I am taking a look forward in your subsequent publish, I will attempt to get the hold of it!

  4. Dominic Ravo says:

    Have you ever thought about publishing an e-book or guest authoring on other blogs? I have a blog based upon on the same information you discuss and would love to have you share some stories/information. I know my readers would appreciate your work. If you’re even remotely interested, feel free to send me an e mail.

  5. Yeah bookmaking this wasn’t a bad determination good post! .

  6. Wonderful web site. Lots of helpful information here. I’m sending it to a few pals ans also sharing in delicious. And of course, thanks in your sweat!

  7. Helpful information. Lucky me I discovered your web site by accident, and I’m stunned why this accident didn’t happened in advance! I bookmarked it.

  8. You really make it seem really easy with your presentation however I in finding this topic to be actually something that I believe I might by no means understand. It seems too complex and very large for me. I’m having a look ahead to your subsequent put up, I will attempt to get the cling of it!

  9. muscle says:

    Great write-up, I am normal visitor of one¡¦s blog, maintain up the excellent operate, and It is going to be a regular visitor for a lengthy time.

  10. I’ve been browsing on-line greater than three hours as of late, but I by no means discovered any fascinating article like yours. It¡¦s beautiful worth sufficient for me. In my view, if all site owners and bloggers made good content as you probably did, the internet might be a lot more useful than ever before.

  11. lamia says:

    Thanks used for another informative place. Everyplace else might I get that kindly of info printed in such a great way? I’ve a project that I’m just at this moment functioning on

  12. 1 2

Leave a Reply