Running php script via cron

Im running a php backup script to backup a website(files and database). I can directly call the script in a browser and the script will run fine with all backups created. When I schedule and run the script via a cron job I get the errors below.

Any help would be appreciated

/usr/local/bin/php -q /home/site/public_html/batch_script/backup.php

Warning: opendir(…/wp-content/uploads/): failed to open dir: No such file or directory in /home/site/public_html/batch_script/functions.php on line 465

Warning: opendir(…/wp-content/themes/): failed to open dir: No such file or directory in /home/site/public_html/batch_script/functions.php on line 465

Warning: opendir(…/wp-content/plugins/): failed to open dir: No such file or directory in /home/site/public_html/batch_script/functions.php on line 465

Warning: fopen(backup/backup-21-10-10 03-44-01.zip): failed to open stream: No such file or directory in /home/site/public_html/batch_script/backup.php on line 121

Warning: fwrite(): supplied argument is not a valid stream resource in /home/site/public_html/batch_script/backup.php on line 122

Warning: fclose(): supplied argument is not a valid stream resource in /home/site/public_html/batch_script/backup.php on line 123

Sorry. I can be like that, I try not to be though. Honest. :slight_smile:

The CRON job invokes the PHP CLI to execute the script you pass it, you can use the -c [URL=“http://www.php.net/manual/en/features.commandline.options.php”]option to tell the CLI which php.ini to use.

-c <path>|<file> Look for php.ini file in this directory

So your command you posted earlier would be…

/usr/local/bin/php -q -c /path/to/php.ini /home/site/public_html/batch_script/backup.php

I think…

Thanks got rid of that message, now I get the below message, ive tried adding a local php.ini file but that doesnt reslove the error.

;; set memory limit for functions.php
memory_limit = 64M

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14167822 bytes) in /home/site/public_html/batch_script/functions.php on line 110

OK makes sense, will give it a try.

When you run a program from the command line, any relative paths accessed are relative to the current working directory of the user executing the program. When cron runs your script, its current working directory is not the root of your website, so “…/wp-content” for example does not exist.

Within the code of your backup script, specify the full absolute paths to all files you are going to open.

Check your cwd, I think you’ll find your script isn’t running from where you think it is. :wink:

You’ve lost me there…

Pretty sure the CLI uses a different php.ini, you can however specify the ini to use with the -c argument.