dongxingji3882 2013-11-06 11:52
浏览 69
已采纳

无法在cron'd PHP中运行shell_exec,$ PATH也不可见

I'm trying to implement a testing integration solution in our environment. The simple workflow is like this: I have a CentOS EC2 instance in AWS where there is a MySQL DB with rows representing tests to be run (each one has a unique id, absolute file path, started time, completed time, etc). These tests are actually python scripts (using unittest framework).

I have created a PHP script (my language of choice) that takes all the non-started tests from the DB and runs them locally using python or even py.test which I execute through shell_exec() in my code.

Before running the PHP script though, I also execute (at least the first time) the following bash script to setup a local testing environment using headless Firefox with WebDriver, Selenium Server and Xvfb for a "display":

#!/bin/bash

if [ `whoami` != "root" ] ; then
        echo "Run as root!"
        exit 1
fi

#Start X virtual frame buffer
/etc/init.d/xvfb start

#Add fake display to env
export DISPLAY=:99

#Start Selenium Server
java -jar /opt/selenium-server/selenium-server-standalone-2.33.0.jar &

If I run my PHP script from the terminal like this:# php script.php everything runs correctly, php calls shell_exec() which in turns runs python to execute the test through py.test using the fake display etc.

BUT, when I try to add this PHP script in a cron job it fails. My cron.d file looks like this:

* * * * * root /usr/bin/php /path/to/script.php >> /path/to/log.log

I have read online that a solution would be to create a shell script that will call the PHP script inside it and put that into cron instead. I tried that and it seemed to do something but it stuck in another place (in my shell script first of all I had export DISPLAY=:99 just in case, so the python script will see the virtual display but I'm not sure if needed).

Anyway, following the above road, somehow made py.test to execute but then it throws an exception when it tries to locate the Firefox binary (which is located at /usr/local/bin/firefox). It seems that the shell script cannot see the $PATH. But I have also read the following: a shell script that executes another script inside it (PHP in our case) actually spawns a child for this, so is not always true that this child can see the $PATH even if set on the parent script? I'm not sure about this.

Can you help me? What's the best way to have all of the above run in cron? How to make the PHP or shell script see the $PATH? (if that's the way to go at least)

  • 写回答

2条回答 默认 最新

  • dongzhun8449 2013-11-06 12:07
    关注

    You can export PATH in the beginning of your cron command.

    Find the correct PATH

    $) echo $PATH
    /some/path:/some/other/path
    

    In your crontab, write

    * * * * * export PATH="/some/path:/some/other/path" && /usr/bin/php /path/to/script.php >> /path/to/log.log
    

    Alternatively, you can set it in the beginning of your crontab file as well as shown here

    Since you are setting the value of the DISPLAY environment variable and starting the xvfb sever, you should consider setting the path as well within your shell script and run the shell script as the cron job (instead of the php script I use in my example)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。