Reputation: 91
php /home/test9/public_html/degerlendir/test-4567.php "var1=18&var2=22"
I need to run one page at background with cron job. I tested my code with command at above. But I get this error:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/extensions/no-debug-non-zts-20100525/imagick.so' - /usr/lib64/extensions/no-debug-non-zts-20100525/imagick.so: cannot open shared object file: No such file or directory in Unknown on line 0
PHP Warning: require_once(../config.php): failed to open stream: No such file or directory in /home/test9/public_html/degerlendir/test-4567.php on line 2
PHP Fatal error: require_once(): Failed opening required '../config.php' (include_path='.:/usr/lib64/php') in /home/test9/public_html/degerlendir/test-4567.php on line 2
The problem is page doesn't include config.php in the parent directory. The page working in browser normally. I tried to use different require_once variations like require_once ($_SERVER['DOCUMENT_ROOT']."/config.php"). I could not get it to work.
Upvotes: 1
Views: 3786
Reputation: 3707
In your cron job, you need to cd to the correct working directory to allow your PHP file to find it's includes. I do this by creating small shell scripts, and run the shell scripts from cron:
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${DIR}
php test-4567.php
exit 0
This also gives you the ability to do some useful things like checking to see if the script is already running to make sure you don't spin up multiple threads if that's something you want to avoid.
#!/bin/bash
FIND_PROC=`ps -ef |grep "php test-4567.php" | awk '{if ($8 !~ /grep/) print $2}'`
# if FIND_PROC is empty, the process has died; restart it
if [ -z "${FIND_PROC}" ]; then
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${DIR}
php test-4567.php
fi
exit 0
Upvotes: 2
Reputation: 1712
from the command line there is no $_SERVER['DOCUMENT_ROOT']
. That one is only available from the http-server (Apache).
The working directory will not be automatically set. If you prompt is currently at /some/path/ the script will try to find config.php in /some/config.php.
Try cd to the current path by using __DIR__
in the start of your script
<?php chdir(__DIR__); ?>
Upvotes: 4
Reputation: 399
Cron jobs always take full path of the included file from your root.
/home/test/.../your-file
Upvotes: 3