0

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)

Ion
  • 1,033
  • 2
  • 13
  • 18

2 Answers2

2

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)

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • Thanks @ansh0l, this worked! I am not sure if I need the shell script after all and setting the DISPLAY env variable, since it seems that Xvfb creates a "new" screen with another ID on the fly. – Ion Nov 06 '13 at 13:45
  • Cool! That was just a suggestion in case you want to go ahead with the shell script instead of the php one. – Anshul Goyal Nov 06 '13 at 13:47
1

Create a crontab job from root user and remove root from: * * * * * root /usr/bin/php /path/to/script.php >> /path/to/log.log.

Manolo
  • 24,020
  • 20
  • 85
  • 130
  • Hi, I tried ansh0l's solution above and it worked, but I'm curious about what you wrote. Is there a difference between the root's crontab and the regular cron with root specified as the user under whose account to run the script? – Ion Nov 06 '13 at 14:09
  • Let me question first, how does the crontab job know the root password if you run it from other user? – Manolo Nov 06 '13 at 15:20
  • If you run it as root, you could omit the `root` word. – Manolo Nov 06 '13 at 15:21