Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to start selenium and the headless Xvfb X server using a init script and the daemon tool.

But when I start my environment using this method, Firefox fails to launch. It looks like it can not find Firefox 2 and then stops. When I start that environment normally, it also does not find Firefox 2 but then it searches for Firefox 3, finds it and starts properly.

This is my init script:

#!/bin/bash
#
# Starts a Selenium-RC headless environment
# License: https://github.com/amenk/SelfScripts/blob/master/LICENSE.md

#
# BETA
# Fixmes:
# - We should take care of running the environment under a non-priviledged user


#
# Source function library.
. /lib/lsb/init-functions


LOG=/var/log/selenium.log

DISPLAY_NO=99

JAVA=/usr/bin/java
XVFB=/usr/bin/Xvfb
SELENIUM_JAR=/opt/selenium-server-standalone-2.20.0.jar
FIREFOX_TEMPLATE=/home/amenk/.mozilla/firefox/aph73r3f.selenium/


start() {
    log_daemon_msg "Starting Xvfb"
    daemon --dbglog=$LOG --errlog=$LOG --stdout=$LOG --stderr=$LOG\
        --name Xvfb -- $XVFB :$DISPLAY_NO -ac -screen 0 1024x768x8
    log_end_msg $?
    log_daemon_msg "Starting Selenium RC"
    daemon --dbglog=$LOG --errlog=$LOG --stdout=$LOG --stderr=$LOG\
         --name Selenium-RC --env=DISPLAY=$DISPLAY_NO\
         -- java -jar $SELENIUM_JAR -log /var/log/selenium-debug.log -browserSideLog -firefoxProfileTemplate $FIREFOX_TEMPLATE
    log_end_msg $?
    return
}

stop() {
    log_daemon_msg "Stopping Selenium RC"
    daemon --stop --name Selenium-RC
    log_end_msg $?

    log_daemon_msg "Stopping Xvfb"
    daemon --stop --name Xvfb
    log_end_msg $?
    return
}

status() {
    daemon -v10 --running --name Xvfb
    daemon -v10 --running --name Selenium-RC

}

case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    status)
    status
    ;;
    restart)
    stop
    start
    ;;
    *)
    echo "Usage:  {start|stop|status|reload|restart[|probe]"
    exit 1
    ;;
esac
exit $?

Starting the whole thing works fine, but when I run a selenium test on that server I get the following message in /var/log/selenium-debug.log:


13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Discovering Firefox 2...
13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/Applications/Minefield.app/Contents/MacOS/firefox-bin' is valid...
13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/Applications/Firefox-2.app/Contents/MacOS/firefox-bin' is valid...
13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/Applications/Firefox.app/Contents/MacOS/firefox-bin' is valid...
13:21:29.207 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/usr/lib/firefox/firefox-bin' is valid...
13:21:29.209 INFO [11] org.openqa.selenium.server.SeleniumDriverResourceHandler - Got result: Failed to start new browser session: java.lang.NullPointerException on session null

When I run selenium and xvfb with the same parameters under the same user, I get

...
13:31:16.413 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 2 launcher at :'/usr/lib/firefox/firefox-bin' is valid...
13:31:16.414 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.CombinedFirefoxLocator - Did not find Firefox 2, now discovering Firefox 3...
13:31:16.415 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Discovering Firefox 3...
13:31:16.433 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 3 launcher at :'/usr/lib/firefox-addons/firefox-bin' is valid...
13:31:16.434 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 3 launcher at :'/usr/lib/firefox-addons/firefox' is valid...
13:31:16.434 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 3 launcher at :'/usr/lib/firefox-11.0/firefox-bin' is valid...
13:31:16.434 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Checking whether Firefox 3 launcher at :'/usr/lib/firefox-11.0/firefox' is valid...
13:31:16.434 DEBUG [11] org.openqa.selenium.browserlaunchers.locators.BrowserLocator - Discovered valid Firefox 3 launcher  : '/usr/lib/firefox-11.0/firefox'
13:31:16.435 INFO [11] org.openqa.selenium.server.BrowserSessionFactory - Allocated session cb49662833c84b6cb62fcd5a4a7e9f61 for http://example.com/foo/bar/, launching...
share|improve this question
    
Your failed log shows OS X-style application paths, the successful log shows *nix-style app paths. Is there a reason for this? Can you describe the environment? –  dwurf Apr 26 '12 at 7:41
    
Environment is the same. What I think is, that the run via the daemon tool just searches more. The normal run somehow finds it. –  Alex Apr 26 '12 at 10:00
    
I don't think that's right... note how they are looking in completely different paths. I think there is a difference in interpreter or environment variables. –  dwurf Apr 26 '12 at 10:09
    
Oh! looks like the environment variables are pretty empty since I use the -e option. Looks like I also have to use -i –  Alex Apr 26 '12 at 10:40
    
Thank you, that was it! –  Alex Apr 26 '12 at 10:58

3 Answers 3

As noted in the comments above, your environment looks different when running as a daemon :)

share|improve this answer
up vote 1 down vote accepted

The problem was the environment inside the daemon executing. The --env cleared up the enviroment variables. You can easily see this with

daemon -O /tmp/export --env="FOO=BAR" -- bash -c export
cat /tmp/export

The solution is to start daemon with the "-i" switch to copy the path setting. Using this change, Firefox will be found.

The working version of the script is available on GitHub.

share|improve this answer

The init scripts in here might be a solution.

But Selenium is put to the background simply with the "&" operator. So I think this init script would not survive exiting the shell that it was called from.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.