简体   繁体   中英

Running a php script in crontab

I'm trying to run a php script every day at 01:00 UTC. I have the line
00 01 * * * /usr/bin/php -f /var/www/html/test.php
in my crontab, but for some reason it's not running my script. I know the script itself works because I've tested it. It just won't run in the crontab. I've also tried using

00 01 * * * php /var/www/html/test.php  

but that didn't work either. I've added #!/usr/bin/php to the very beginning of the php script I am trying to run. I've also tried a -q instead of -f. Nothing has worked, and I'm not sure what I can do to fix this. Is there some other method I should try if this still doesn't work?

Make sure your paths are correct. Try this syntax:

cd /var/www/html; php -q -c ./ test.php

The -q flag suppresses any header() from being written to standard out. This forces your script to be executed with the php-cgi binary instead of the command-line version.

The -c flag prevents the OS from changing directories since you do that with cd .

What I ended up doing was writing a ksh script that exported more paths and then ran the php script. This worked.

export ORACLE_HOME=/opt/oracle/product/11.2.0/client export LD_LIBRARY_PATH=/opt/oracle/product/11.2.0/client/lib/ export PATH=$PATH:/opt/oracle/product/11.2.0/client/bin php /var/www/html/test.php

having a local web server running, you may call

http://localhost/test.php

from your crontab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM