Cron没有执行shell脚本..但是从命令行工作

时间:2013-08-29 08:37:35

标签: php bash shell cron ubuntu-12.04

我的操作系统是Ubuntu 12.04

我正在尝试从cron标签执行shell脚本..

当我在命令行中直接执行时,shell脚本工作正常..比如

sh out.sh

它工作正常..但是当我为这个shell脚本设置cron时它无法正常工作

我的shell脚本:out.sh:

#!/bin/bash
firefox "http://localhost/acceptance/selenium-main/shell.php"

它将在firefox浏览器中打开shell.php网页...直接从CLI执行时它工作正常

..我是这样的设置cron工作

sudo crontab -e

然后

23 13 * * * bash /usr/share/nginx/www/acceptance/selenium-main/out.sh

这不起作用..

我甚至试过

33 13 * * * /usr/share/nginx/www/acceptance/selenium-main/out.sh

这也不起作用..

即使我厌倦了从bin执行:/usr/local/bin/out.sh

没有一种方法正在运作

请建议如何解决这个问题..因为crontab没有执行shell脚本..

5 个答案:

答案 0 :(得分:2)

尝试使用绝对路径:

23 13 * * * /bin/bash /usr/share/nginx/www/acceptance/selenium-main/out.sh

如果某些内容无效,请尝试在登录模式下运行bash。

23 13 * * * /bin/bash -l /usr/share/nginx/www/acceptance/selenium-main/out.sh

那会尝试修复其他变量,比如PATH。如果没有,请尝试在脚本中明确设置它,或者只在所有地方使用绝对路径。

答案 1 :(得分:0)

尝试将相同的解决方案konsolebox添加到shell脚本中的firefox行。也许firefox不在你的路上

答案 2 :(得分:0)

在配置GUI应用程序时,它看起来像cron的行为。尝试创建一个包装脚本,如

<强> wrapper_script

export DISPLAY=:0.0
xhost + 2>>/tmp/err.log
firefox "http://localhost/acceptance/selenium-main/shell.php" 2>>/tmp/err.log

将cron条目添加为

  

33 13 * * * bash /path/to/wrapper_script.sh

答案 3 :(得分:0)

怎么样

php /path_to_localhost/acceptance/selenium-main/shell.php

如果它只是生成报告而没有用户交互,只需运行脚本即可。您不需要浏览器来运行PHP脚本。

答案 4 :(得分:0)

cron和命令行中运行某些内容之间的最大区别在于您的配置文件是针对您的shell运行的,因此脚本所需的所有环境变量都已正确设置,但cron使用一个赤裸裸的&#34;无轮廓&#34;外壳

尝试使用一个脚本来设置所需的所有环境变量,然后再执行脚本所做的任何操作,因此它自己包含&#34;。