views:

504

answers:

3

I'm building a web page screen capture application for an internal R&D project.

Environment: Ubuntu 9.04 (default desktop install), Apache, PHP.

So far I've got a bash script that takes one parameter (URL), fires up firefox, grabs the screen and saves it as a PNG. I've tried running this from terminal and it works fine.

Here's the Bash script:

#!/bin/bash
firefox $1 # Start firefox and go to the passed in URL
scrot -d 5 test.png # Take screen grab with 5 second delay

Next I created a simple PHP page that uses shell_exec to run the script:

<?
  // Sample URL
  $url = 'http://www.google.com'; 
  // Run the script
  shell_exec('sh script.sh ' . $url);
  // Out put HTML to display image
  echo '<img src="test.png" />';
?>

However, when the PHP page is called the screen is not captured. A quick look in the apache error logs show the following message:

Error: no display specified
giblib error: Can't open X display. It *is* running, yeah

I'm guessing this is because apache is running as a different user and hasn't got access to my X display.

So, can anyone shed any light on what I'm doing wrong or how I can capture the current user display.

Thanks.

A: 

Can't you run your bash and firefox as the same user as apache?

Time Machine
Can you elaborate further please.I'm assuming that Apache runs the bash script as it's own user which doesn't have X running? I'm a bit of a noob to this so in the process of learning how it all works.
Andrew Mason
+2  A: 

Launching firefox from PHP running under Apache seems to me like a bad idea (it definitly feels wrong).

The way I would do that :

  • a PHP webpage (which runs under Apache) that receives the URL ; something like a form, for instance
    • that page inserts the URL in a database-like system, that will be used as a queue
    • this URL is marked as "to process"
  • a PHP (or another language) script, totally independant from Apache ; for instance, launched by the crontab
    • this scripts selects an URL from the queue in the database (least recent one, for instance), and marks it as "processing"
    • it then lauches your shell-script, which launches firefox and does the screenshot
    • one the screenshot is done, the URL in the queue is marked as "done", and the screenshot's path is associated to the URL
    • this will work, as it is independant from Apache
  • another web page displays the queue, and the status of each URL ("to process", "processing", "done + path to the screenshot"
    • you can even imagine having an association betwen a user and an URL, to not display every URL+screenshot to everyone.

With this system, several advantages :

  • php+apache for the webpages
  • php outside of apache for the "system" parts
  • you can have the webpages on one server
  • and you can have several machines (linux, windows, mac -- maybe using virtual machines) to make the screenshots
    • allow you to get screenshots from different OSes
    • scales way better ^^

It's not really an answer to the question, but I think it's a better way... Hope this helps !

Pascal MARTIN
Thank you for the comprehensive response.My original method was a very quick idea mock-up, but you're right, it does feel *wrong* and your proposed system is a better approach.
Andrew Mason
You're welcome :-) (only problem is that I am now thinking "this would be fun to code, let's do it" -- except I already have tooooo much to do :-D )
Pascal MARTIN
+1  A: 

Here is a guide to do screen-capturing using firefox and xvfb. The advantage with this approach is that there will be no firefox windows opening and closing on your main X server. It will also solve your problem with permissions.

Inshallah