简体   繁体   English

作为Cron的PHP脚本不起作用,但是来自CLI

[英]PHP Script as Cron doesn't work, but does from CLI

I have a php script that triggers some magento actions, and I set it to a cron of: 我有一个PHP脚本触发一些magento操作,我将其设置为一个cron:

cd /home/dir/public_html; php -f file.php;

This starts the script, however it does not finish executing for some reason, the cron runs as the user "user", and when I run the command from the terminal as root it works perfectly. 这启动了脚本,但由于某种原因它没有完成执行,cron以用户“user”运行,当我从终端以root身份运行命令时,它完美地工作。 All the files it uses are chowned to user however. 但是,它使用的所有文件都是用户使用的。 I thought it was an issue with paths which is why I added the CD command to the front of it, however that wasn't it. 我认为这是一个路径问题,这就是为什么我将CD命令添加到它的前面,但不是这样。

I'm thinking this may be an issue with the creation of a lock file, I have it create a lock file, run the script, then delete the lock file in order to prevent it from running if it already is. 我认为这可能是创建锁文件的问题,我让它创建一个锁文件,运行脚本,然后删除锁文件以防止它运行(如果已经存在)。 The lock file is generated but never deleted, my knowledge is if it creates it as user "user" then it should be able to delete it as that user as well. 生成锁文件但从未删除,我的知识是,如果它创建为用户“user”,那么它应该能够删除它作为该用户。

Any thoughts? 有什么想法吗? Much appreciated. 非常感激。

Try to put the full path of PHP, or define the PATH variable in the first lines of crontab: 尝试放置PHP的完整路径,或在crontab的第一行中定义PATH变量:

PATH=/usr/local/bin:/usr/bin:/bin:/usr/games:/sbin:/usr/sbin

Edit : moreover, you can log your script like that : 编辑 :此外,您可以像这样记录您的脚本:

* * * * * cd /home/dir/public_html; /usr/bin/php -f file.php; &>/tmp/file.log

Instead of calling the php from within the cronjob, call a shell script which is calling the php file then. 而不是在cronjob中调用php,然后调用一个调用php文件的shell脚本。

You can then change the environment the script is running in w/o the need to change the cronjob and you can easier test-drive the cron command (as you can just call the shell-script). 然后,您可以更改脚本运行的环境,无需更改cronjob,您可以更轻松地测试驱动cron命令(因为您可以调用shell脚本)。

You can then, in the shell script, change the directory where the php-script expects to be in which will most certainly solve your issue. 然后,您可以在shell脚本中更改php脚本期望的目录,这将最有效地解决您的问题。

Additionally you can change ini directives, handle logging and shell error handling, like piping STDERR to a file etc. 此外,您可以更改ini指令,处理日志记录和shell错误处理,例如将STDERR管道传输到文件等。

This is not exactly your problem, but the information given in this question might solve your issue: How can I force PHP Version for Command Line? 这不完全是您的问题,但此问题中给出的信息可能会解决您的问题: 如何强制PHP版本用于命令行? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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