Jump to content

[SOLVED] php script in cron


Jonob

Recommended Posts

Have to say that I am totally stumped by this one.

 

1. I created the following a directory on my domain called testy as follows : www.mydomain.com/testy

2. I created a file called rename.php with the following code:

<?php

$old = "old.gz";
$new = "new2.gz";

if (file_exists($old))
{
echo "<br>Original file (" . $old . ") exists";
} else {
echo "<br>Original file  (" . $old . ") does NOT exist, exiting...";
die();
}

rename($old,$new);

if (file_exists($new))
{
echo "<br>Success!! New file (" . $new . ") created";
} else {
echo "<br>Failure. New file (" . $new . ") was not created";
die();
}
?>

3. I placed a small file called old.gz in the above directory

4. I navigate on my browser to http://www.mydomain.com/testy/rename.php and I get the output:

Original file (old.gz) exists
Success!! New file (new.gz) created

5. I check ftp, and file has indeed been changed. OK cool, that that works fine.

6. I rename the file back to old.gz

7. I create a cron as follows:

/usr/local/bin/php /home/foo/public_html/testy/rename.php

8. Cron runs, and I get an error message from the cron:

Original file  (old.gz) does NOT exist, exiting...

9. I log in via ftp and sure as hell old.gz is there

10. I go again in browser to http://www.mydomain.com/testy/rename.php and I get the same success message as above. Double check in ftp - yup, file name has been changed.

 

Any ideas why this is working via http but not via cron? Any tips greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/143034-solved-php-script-in-cron/
Share on other sites

the problem is the current working directory...when running from the web, it's the same dir as the script. but when running in cron, it is most likely somewhere else. the easiest fix is to put this at the top of the script:

 

chdir(dirname(__FILE__));

 

edit: that will change the current working directory to the folder the script is in...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.