I have a php script and want to run it on an schedule. I am using local web server on windows (WAMP server) and need a way to run my_script.php every 10 min.
How to run a cron job on a PHP script, on localhost in windows?
recently I had sort of problems to run a cron job on a php script on localhost (WAMP server) in windows 7, when I was on a test to chronically fetch some links from www out there.
By the way I am sharing this for anyone that is on the same thing.
You will need a shellscript to run chronically, using Windows Task Scheduler. Also you will need a batch script (script.bat) to call the php.exe and run your php script (here called as my_process.php)
shellscript.vbs
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\path\to\script\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing
script.bat
"C:\wamp\bin\php\php5.4.12\php.exe" -f "C:\wamp\www\website\my_process.php"
Now, we are ready to set the Windows Task Scheduler to run shellscript.vbs at the required time interval:
Open Task Scheduler from windows Start menu
Go to Action menu and hit Create Task...
in General tab, fill the Name and Description fields as you want
in Triggers tab, hit New button.
from Begin the Task dropdown, select On a schedule and choose Daily
from Advanced settings section, select Repeat task every as you want and set for a duration on Indefinitely.
on Actions tab, from Action dropdown, select Start a program.
on the Program\script box, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
leave Add argumentts (optional) section empty.
in Start in (optional) box, enter parent directory of shellscript.vbs like C:\path\to\.
Hit upvote on this tutorial :) Have fun.
To setup a Windows machine to run cron.php at a specific time follow the specific instructions below. This can be useful if you are not familiar with Linux/Unix, or if your web host does not offer the ability to run cron jobs; you can run them remotely from your own computer.
Note: These instructions were written for Windows XP but should be similar in other versions of Windows.
https://www.drupal.org/node/31506
If you use answer from Trix and get same problem as Metafaniel:
I have a problem, with this procedure, the task is beinge executed,
however my php script it's not running, I got the "Open with"
dialogue. If I see the properties of my task, it states: ActionName
C:\Windows\system32\OpenWith.exe even if I set it to the vbs file as
you suggested. What am I doing wrong?
you should use this:
Instead "enter path to shellscript.vbs like C:\path\to\shellscript.vbs." use "C:\Windows\System32\wscript.exe"
Instead "leave Add argumentts (optional) section empty" use (with quotes):
"C:\path\to\shellscript.vbs"
After many failed attempts at using Windows Task Scheduler this is the method that worked for me on my Windows 10 Professional machine. I just skipped adding a .vbs and .bat file and posted the PATH to the script file directly.
Under "Actions > Settings" Where it gives the option to browse for a "Program/Script" that's where I put the full PATH AND Filename to the script I wanted to run WITHOUT quotes.
C:\Server\Scripts\script.py
Then in the same area under "Actions > Settings" right below the text box where you just entered your "Program/Script", it has the area that says "Start in (optional)". That's where I put the PATH to the program that runs the script (ie: python.exe in this instance). But I did not specify "python.exe" directly I used the PATH ONLY without quotes like shown below.
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
If you installed Python directly without Visual Studio it will be under a different path probably something like "C:\Program Files\Python\Python37" or something similar if I remember correctly...
Little bit late but just for someone who don't want it to be lengthy and don't want to waste time in testing (as the aim is to host it to server), can do one thing by adding http content refresh in the html meta tag of that php file and add the required timer to it and run it in the browser.
NOTE: You have to make the system remain open untill you don't get the required output.
Again I am saying this is not the perfect and proper way but ya can save someone's configuration setting time.
Related
I have a local WAMP install.. and my understanding is that you can set up CRON jobs here (being Windows and all)..
I wanted to test out calling a .php script at a schedule time, using the Windows Task Scheduler.
I set up a simple .php script that sends an email (nothing more).
I tested that it works but going directly to the script in my browser (email received, no issues)
So I set about trying to configure a Scheduled Task on my Windows 7 machine:
Opened up Task Scheduler
Right clicked Task Scheduler Library > Create Task
General Tab:
Name: Email_Test
Run whether user is logged in or not (checked)
Actions Tab:
Action: Start a program
Program Script: C:\wamp\bin\php\php5.4.12\php.exe (I know I know.. I'll update to PHP 7 soon!)
Arguments: -f C:\wamp\www\cron_jobs/email_test.php
Start in: C:\wamp\www\cron_jobs\
Triggers:
Begin task: On a schedule
Daily: (setting time for a minute or two ahead of current time to test)
Repeat: every 5 minutes
Expire: Tomorrow
I only need it go off once a day, but wanted to put those settings in to keep testing.
I have yet to receive an email using this as a Schedule Task..
I originally had a .bat file set up as well.. but read some different tutorials that showed to do it this way? But I have no problem changing some path to a .bat file instead somewhere if that is my error/issue?
I also enabled History.. and all entries say things were successful? (Task/Action Started/Completed)
What am I doing wrong? and more so, how can I fix it? :)
Updated information:
I forgot to mention that for my WAMP/PHP install.. I am using SENDMAIL as my mailer.. perhaps this is part of the problem? DO I need to somehow allow the sendmail.exe to execute or something as well?
I do NOT want to directly use sendmail by itself.. I would prefer to use the PHP script.. eventually I want to add in a DB log, before I sent the email out.. but for now.. the PHP script ONLY has mail() function in it.
So upon reading.. I guess the php.ini file in APACHE is not the same as used in the CLI PHP execution?
I couldnt find php.ini # C:\wamp\bin\php\php5.x.x\php.ini
But I did find and update:
C:\wamp\bin\php\php5.x.x\php.ini-development
&
C:\wamp\bin\php\php5.x.x\php.ini-production
it didnt say.. but are these supposed to be renamed to be php.ini?
(although I'm not sure what those files are for.. but I made them match my php.ini file here:
C:\wamp\bin\apache\Apache2.X\bin\php.ini
Yet still.... no email arrives!
If I open script in browser.. I get email always?
(as that works fine for sending mail without issues?)
I have a script that sends a mail if a condition is true. In order to run the script I have to visit localhost/index.php. Every time I refresh the webpage the script checks if the condition is true, and if it is, it sends me an email.
How do I make this script run every minute without having to have my browser on refreshing the page every minute?
My current solution is to put this in my PHP file:
<meta http-equiv="refresh" content="60;url=index.php" />
Use Windows Task Scheduler. It allows you to run scheduled tasks like Cron for *nix.
update
PHP works from the command line like many other server side languages. This means if parameters are required you can pass them as such:
php C:\Path\to\script\script.php param1 param2
param1 and param2 are passed to your script in a similar way that $_POST and $_GET variables are. See PHP command line usage for more details on how this works. But it would mean very little change to your script which is nice.
You can run a PHP script without the browser by to following line put into
Start -> run
c:\program files\xampp\path\to\php\bin\php.exe -f c:\htdocs\script\script.php
Put this into the Windows Task Scheduler run input.
EDIT:
As the OP said they were using WAMP, PHP is actually in this folder structure
\wamp\bin\php\phpx.y.z\php.exe
In windows you need Task Scheduler for that.
you need to run php.exe file in some regular interval and that file will execute your php script for example,
C:\Path\to\php.exe -f "C:\Path\to\file.php"
thus, with Windows Task Schedular you can run php.exe file in some regular interval with specified condition. Here are the steps,
1. Open Task Scheduler
2. In Task Scheduler, Click on "Action -> Create Task" from menubar.
3. In "General" tab write "Name"- Name of Task and "Description" - Description for Task
4. In "Triggers" tab click on "New" and give triggering time as per requirement.
5. In "Actions" tab click on "New" and select Action - Start Program (which is Default).
And from browse button select your php.exe file from your php installed files.
And in "argument" give file path with Option ex. -f c:\Path_To_Htdocs\fileName.php (here -f is option).
6. In "Conditions" tab you can set condition when to run task means about the your pc status like idle/on AC power, etc. I suggest you to leave it as default.
7. In "Settings" tab you can specify some extra settings. I suggest you to leave it as default.
8. Finally As you done, Click on "Run" from "Action" to run it manually.
I've installed Apache 2.4 with PHP 5.4 on Windows Server 2008 following instructions from this manual:
Apache installing manual.
Apache runs as a service now.
My application requires a php websocket script to run in the background. I'm running it manually with:
php myscript.php
The question is: Is there a way to start a background script automatically on system(apache) restart?
I found the following topic, but I didn't know where I could find an apache startup script for Windows.
Any help will be much appriciated.
I come up with a solution :)
Create an environment variable pointing to your Apache directory
APACHE_HOME = C:/PATH/TO_APACHE
Rename %APACHE_HOME%\bin\httpd.exe to %APACHE_HOME%\bin\httpdVendor.exe
Create a batch file and put the following code :
php myscript.php
%APACHE_HOME%\bin\httpdVendor.exe -k runservice
exit 0
Download/Install the free software BatToExeConverter (next, next, ...)
Open the installed converter and open your freshly created batch file
Click on the button Build EXE (let the default configuration)
Save the file : %APACHE_HOME%\bin\httpd.exe
Start your Apache Server
Tested on : Windows 7, Apache 2.4, Advanced Bat to Exe Converter 2.92
Use built in Windows Task Scheduler which triggers .bat script, which calls curl with defined url.
Download curl from http://curl.haxx.se/download.html and extract curl.exe on any directory, but we will use c:\backgroundtasks
Adjust script below to your needs:
cd c:\
cd c:\backgroundtasks
curl http://localhost/path/to/script.php
exit
Configure Task Scheduler to run as basic task:
General tab - as system account (to run when you are not logged in server)
Triggers tab - adjust frequency
Settings tab - at bottom set If the task is already running... to Stop the existing instance
The best method here would be to use Windows services dependencies.
Make a php-websocket-server.cmd file with any necessary environment settings (e.g. changing to a directory, setting PATH, etc...) with the last line:
php myscript.php
Install the Windows Server Resource Kit Tools, to get srvany and instsrv to create a user defined service. Note the install path as you'll need it in the next step.
Open a cmd shell and run:
<path_to_resource_kit>\instsrv PHPWebSocketServer <path_to_resource_kit>\srvany.exe
Next, create a file php-websocket-server.reg containing the following (update for your environment):
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PHPWebSocketServer\Parameters]
"Application"="c:\\path\\to\\php-websocket-server.cmd"
Import it by double-clicking or regedit /s php-websocket-server.reg
Back in your cmd shell:
sc config Apache2.4 depend= PHPWebSocketServer
to make the Apache2.4* service depend on your php service. Now, when Apache is started, the php service will be brought up first. And likewise, if you stop the php service Apache will stop along with it.
*the howto indicates that the service is named "Apache2.4" but you may want to verify in your installation.
When running as service, you won't have the startup script.
Execute some service implementation that allows running other programs as services, and then make the new service (which is running your script) a dependency of the Apache service. However, this will not restart the script when apache restarts.
One possible solution using SrvStart, and another using ServiceEx.
Perhaps don't install Apache as a service, and then edit the startup/restart script, and use the above method to run Apache as service (instead of using Apache's own installer).
Create bat file,e eg 'myphp.bat' containing path/php myscript.php. Include the correct path to php if it's not path'd.
create a bat file, eg runmyphp.bat containing
AT 00:00 /every:M,T,W,Th,F "cmd /c /path/myphp.bat", again including the correct path.
Then use explorer to drag runmyphp into the startup folder, so it will always run on system startup.
Google 'windows at command' or 'windows cron' to get all the correct syntax for the 'at' command, but you can currently find a detailed explanation here.
I found another answer C:\wamp\scripts\wampserver.lib.php this file is run every time when your wamp starts
include your file path include_once("file_path"); to this file and its done . this is perfect solution which you want
Enjoy!!!!!!!!!
Although the solution of Halayem Anis is very creative, I think its important to note that you can never be sure that a PHP script keeps running in the background. So if you choose to start your script on "Apache start", then you probably end op resetting Apache quite often, simple to reboot your script.
I assume that's even how you came to this question, as on a normal server you never have to touch the Apache reset button. It starts on system start and then it just runs. If that was the case, you could simple run your php myscript.php command on start up.
Considering there is no way to make sure the script keeps running, I would use a different approach, where I check if it is running and if not, restart it.
So the first step is to make it possible to track if the script is running. I would go for the simple approach where your myscript.php writes a single byte to a file every 5seconds or so. This way I can use the last modified time on the file to see if it is still running, because last modified time + 5 seconds < now == not running.
You could also store the last access time in a database every 5 seconds or so. Might be slightly faster then accessing files if you have a lot of traffic.
The second part is to have each request check if the script is running. For this two work I would use the PHP.ini to prepend a php script on every request. You can do it with the auto_append_file option.
This prepend script would work like this:
<?php
$filename = 'checkonline.txt';
$cmd = "php myscript.php";
if (filemtime($filename)+5<time()) {
//run in background without freezing php
//based on code posted on PHP exec manual, linked below
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
?>
Make sure to check how filemtime and exec work and what you need to keep in mind. They work slightly different on Windows/*nix.
Wrap-up all your required processes in a batch file and use RunAsService
With some tweaking, you can ensure that your service starts before Apache.
I am looking for a way to set cron job using PHP. All I would like to do is run a PHP script at a specific time. The user first inputs a time in a script, according to the time specified the server will run the script. I am using windows 7 and xampp.
What I have found is:
Create a php file that calls the cron.php file: Using notepad (or whatever), paste the following into a new file:
$data = file(“http://pearl.supplychain.com/cron.php”);
you’ll need to put it inside the regular php tags, with the “less than sign” ? php at the front, and the ? “greater than sign” at the end. (I can’t seem to just type that because it is “suspicious content” and drupal doesn’t allow it)
Save it as executecron.php, into the same directory as cron.php (htdocs).
Set up a scheduled task that calls this regularly:
Open Start–All Programs–Accessories–System tools–Scheduled tasks.
Double-click on scheduled tasks.
Set up a Daily task that starts at 12:00 am and runs every half hour (or whatever) until 11:59 pm. Tell the task to “run” the following:
C:\cms\xampp\php\php.exe c:\cms\xampp\htdocs\executecron.php
(On this system, php.exe is installed in C:\cms\xampp\php, but you’ll probably have to change the path).
As you can see, to do this, one must Open Start–All Programs–Accessories–System tools–Scheduled tasks.
Can it specific by php code or using another way to do this? Because i want all the work done on php / server instead of need my user config the cron job themselves. Which means i want my php code can set the cron in server and server will look at the cron?
To stefgosselin:
To create the batch file
Open Notepad.
Paste the line "C:\xampp\php\php.exe C:\wamp\www\index.php"
Click "File" -> "Save As"
Ensure "Save as type:" is set to "All Files"
Save the file as "cron.bat" to your C drive
To schedule the batch file to run
Open Command Prompt
Paste the following "schtasks /create /sc minute /mo 20 /tn "PHP Cron Job" /tr C:\cron.bat"
Press Enter
This will make the script run every 20 minutes, the first time 20 minutes from now.
I am able to create a bath file using php, however, are there any way to Paste the following "schtasks /create /sc minute /mo 20 /tn "PHP Cron Job" /tr C:\cron.bat
using php instead of using os? Thank you
Thank you
I found my answer to that question at waytocode.com
They provide 3 possible solutions to run cron jobs on Windows:
Solution-1 using Task scheduler
In your Windows 7/windows 2005/2008.
Go to Startmenu->All Programs->Accessories->System Tools->Task Scheduler->create Task
In the new window:
General (Give the Task name and for testing you can select “Run when User is logged in“)
Trigger (You can Select the running interval as “daily,weekly,monthly”. )
Action (This is most important part. Select a Mozilla firefox as the “program/script” and in the Argument provide the URL to fire with Mozilla firefox).
Solution-2 using Task scheduler and PHP from your XAMPP server
In Windows Xp,no need to copy or install anything(Already PHP is installed on the server like XAMPP)
Goto Task scheduler
Create a task give Running time, then in avanced setting option in the “RUN” command textbox type
C:\xampp\php\php.exe -f c:/xampp/htdocs/waytocode/mycron.php
In Windows 7/server 2005/2008
No need to copy or install anything(Already PHP is installed on the server)
Create a task give Running time in Trigger setting.Then in Action setting option in the “Program/Script” command textbox type
C:\xampp\php\php.exe
and in the “Add arguments (optional)” type
-f c:/xampp/htdocs/mycron.php
Solution–3 install a Windows exe file that will simulate the cron job from *nix system
I don't like to install any exe file to my servers or development machine,
but I'll provide the solution as they posted:
In Windows Xp,Copy all 2 DLL file with wget.exe to the C:\windows folder
Create a task give Running time then in avanced setting option in the “RUN” command textbox type
C:\Windows\wget.exe -q -O NUL http://localhost/mycron.php
In Windows 7/server 2005/2008 ,Copy all 2 DLL file with wget.exe to the C:\windows folder
Create a task give Running time then in avanced setting option in the “Program/Script” command textbox type
C:\Windows\wget.exe
and in the “Add arguments (optional)” type
-q -O NUL http://localhost/mycron.php
Solution-4 using a .bat file and the task scheduler
I found it here at Stackoverflow and it is similar to the first 2:
Create a cron.php file (the code you want to execute at a regular interval)
Create a CRON.BAT file, copy and past the below code in the file
D:\xampp\php\php.exe D:\xampp\htdocs\Application\cron.php
The path I have written is according to my xampp and cron.php file, update the path of files according to your system directory
To schedule a task
Click on start > All Programs > Accessories > System Tools > Scheduled Tasks
Or you can go directly
Control Panel > Scheduled Tasks
Right click in the folder
New > Schedule Task
Give appropriate name to the Task.
In the RUN text field… Type the complete path of the CRON.BAT file
in my case it is
D:\xampp\htdocs\Application\CRON.BAT
Set the schedule of the job, you can use advanced button if required.
Solution-5
I don't like it either because one script can't depend on someone else website but it is a solution anyway.
Use an external online cron job service.
https://www.google.ca/search?q=cron+job+online+service
Chose one solution that it is more appropriate for you. Hope this will help someone.
UPDATE
Solution-6 (Based on the answers below, and works with CodeIgniter too!)
Create the cron.bat file and write the following command and save it.
#ECHO OFF
c:
cd C:\Program Files\Internet Explorer
START iexplore.exe http://localhost/path/to/cron/job/1
Create a task give Running time in Trigger setting.Then in Action setting option in the “Program/Script” command textbox type
C:\xampp\path\htdocs\folder\includes\cron.bat
END UPDATE
Answering your question:
Can it specific by php code or using another way to do this? Because i
want all the work done on php / server instead of need my user config
the cron job themselves. Which means i want my php code can set the
cron in server and server will look at the cron?
There are other ways to do this:
Using cron manager from within PHP
Using cron manager from within PHP
Managing Cron Jobs with PHP
http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php-2--net-19428
Unfortunately, all solutions with PHP needs a *nix server type and / or cPanel and are more or less complicated to implement.
Ok, if I understood correctly, you would like to have a cron job created on a system, without a user having to create the task.
Basically, this can easily be done in a .bat file, (that could even be called from php).
The schtasks app can easily automate the creation of a scheduled task. For example:
schtasks /create /tn UNO /tr YOURAPP.EXE /sc HOURLY /mo 2
See the official MS support page for more info on this nifty application.
Another option that can even be easier for the user is to use an installer. I have not created that many windows apps myself but did have the pleasure of playing with NSIS, and this app also has options to create scheduled tasks, among many many other features way too numerous to name here. Highly recommended if you need a user-installable package.
Hope that helps, happy coding friend.
Another great tool is available for free on Windows, nncron. It uses the exact same syntax than unix' cron:
http://www.nncron.ru/
I find it easier to manage that this horrible schtasks :)
The lite version is the one I would suggest to use. The full powered version supports scripting in the config files. It could be handy but somehow over killed for normal cron jobs.
Steps Create Cron in Windows :
Step 1> Create .bat file
1. open notepad.
2. click File -> Save As.
3.Give the file a name i.e. MyBatchFile.bat Underneath the filename box change the file type to All Files. The .bat on the end of the filename will instruct the PC to open it as a batch file.
Step 2> IN MyBatchFile.bat write following command
ECHO OFF
START File Full Path write here like http://www.google.com
Step 3>
Open Start–All Programs–Accessories–System tools–Scheduled tasks.
Click on create task from right side bar.
name:any one
trigger: set time from drop down and select per day
Action:select .bat file
Click on OK button
Steps Create Cron in Windows OS:
Step 1> Create .bat file
1. Open notepad.
2. Click File -> Save As.
3. Give the file a name i.e. MyBatchFile.bat Underneath the filename box change the file type to All Files. The .bat on the end of the filename will instruct the PC to open it as a batch file.
Step 2> IN MyBatchFile.bat write following command
ECHO OFF
START File Full Path write here like http://www.google.com
Step 3>
Open Start–All Programs–Accessories–System tools–Scheduled tasks.
Click on create task from right side bar.
name:any one
trigger: set time from drop down and select per day
Action:select .bat file
Click on OK button
Another quite simple approach is powershell, to fetch the content of your php-file.
Task-Scheduler, target: Powershell.exe, use arguments:
-Command "(New-Object Net.WebClient).DownloadString('http://myhost/cron/cron.php')"
(Ups, old post)
It is the complete solution I have tried for my own project for windows 10.
1) Go to "start" menu and search for "task scheduler" and open it.
If you are facing any issue of permissions, open it as "run as system administrator".This option is exactly below "open".
And still you are facing issues of permissions contact your system admin.
2) Click on "new task" in right sidebar and a window will be opened.
3) In this window "general" tab will be selected by default. Give a suitable name to task and if require provide short description.
4) Go to "triggers" set here frequency at you want to run it and start date-time, if require expires date-time.and set all require parameters.
5) Got to "Actions" tab. This is the most important part.
In "Program/script", write the absolute path of "php.exe".
In "Start in", write the absolute path of the folder where your intended php file for cron is.
In "Add arguments", write the name of the php file which you want to get run thought the task job.
In "Add arguments", you can also specify the arguments to the files space separated after php file name. and if it has text or special characters, enclose it with double quotes.
If you provide arguments to scripts, you will receive them in $argv[ 1 ], $argv[ 2 ] and so on respectively.
For more details visit: https://medium.com/#shraddha_kulkarni/run-php-cron-in-windows-513fb1aa53a5
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.
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 !
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.
Can't you run your bash and firefox as the same user as apache?