Bash commands not executed when through cron job - PHP

跟風遠走 提交于 2019-12-13 04:38:21

问题


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

回答1:


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.

$output = `$command`;
error_log($output);



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/2748557/bash-commands-not-executed-when-through-cron-job-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!