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条)

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。