通过 cron 作业时未执行 Bash 命令 - PHP
我有一个 cron 作业,每五分钟运行一次 PHP 脚本; PHP 脚本在脚本末尾执行两个 bash 命令。我知道该脚本正在运行,因为它附加了一个日志文件。当我通过 Ubuntu Gnome 终端手动运行 PHP 脚本时,两个 bash 命令都能完美执行;但是,当通过 cron 触发 PHP 脚本时,这两个 bash 命令不会运行。有什么想法吗?
$command = 'notify-send "' . count($infoleakPosts) . ' New Posts."';
`$command`;
$command = 'firefox http://example.com';
`$command`;
*/1 * * * * php /home/andrew/grab.php USERNAME PASSWORD # JOB_ID_1
I have a cron job running a PHP script every five minutes; the PHP script executes two bash commands at the end of the script. I know the script is running due to a log file it appends to. When I run the PHP script manually via the Ubuntu Gnome Terminal both bash commands execute flawlessly; however when the PHP script is triggered via cron, the two bash commands are not ran. Any ideas?
$command = 'notify-send "' . count($infoleakPosts) . ' New Posts."';
`$command`;
$command = 'firefox http://example.com';
`$command`;
*/1 * * * * php /home/andrew/grab.php USERNAME PASSWORD # JOB_ID_1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,您的 cron 脚本将在不同的用户帐户下运行,并且可能设置不同的环境路径。
尝试将命令行设置为使用命令的完整路径,即。
/path/to/notify-send“x 新帖子”
。您可以从常规终端使用
which notification-send
来获取要放入脚本中的路径。您还可以获取命令的输出以帮助调试。使用反引号运算符将返回输出,因此您可以将其分配给变量和/或转储它。
Generally your cron scripts are going to be run under a different user account, and probably have a different environment path set up.
Try setting your command lines to use the full path to the command, ie.
/path/to/notify-send "x New Posts"
.You can use
which notify-send
from your regular terminal to get the path to put into your script.You can also grab the output from your command to help debugging. Use of the backtick operator will return the output, so you can assign it to a variable and/or dump it.
当您在 cron 下运行脚本时,您没有输出 tty 或 X-windows DISPLAY env-var。我怀疑命令正在运行但失败。
when you're running the script under cron you don't have an output tty or X-windows DISPLAY env-var. I suspect that the commands are running but failing.
对上面关于 cron 的答案的评论: cron 将以 crontab 所在的用户身份运行命令。因此,如果您设置了 crontab,它会像您一样运行命令。它运行的 shell 启动脚本集与您登录时获得的脚本略有不同 - 它知道它没有 tty,因此它只执行 ~/.bashrc 文件而不是配置文件集。查看 cron 和 bash 的手册页了解详细信息
a comment on the answer above about cron: cron will run the commands as the user whose crontab it is. So if you set up the crontab it will run the commands as you. It does run a slightly different set of shell startup scripts to those you get when you login - it knows it doesn't have a tty and so it only executes the ~/.bashrc file and not the set of profile files. Check the man pages for cron and bash for details