PHP Tutorial – Forking using wget in the background
I see a lot of people asking on how to use a fork to run a php script or web page in the background.
Here's a quick script I created using linux's wget to run another process in the background. You could use this to automatically upload something to another server, or just about anything that needs to happen after a user action.
Note: you must have exec enabled for this to work. This is a security risk, so make sure you know the potential damage that can be done by enabling exec, and by the script that you are running. Make absolutely sure to sanitize any input being transferred to the executed script.
In my case, I needed to initiate a file upload to a remote server after a certain action was taken. Because of the nature of the upload, it would have taken about 30 seconds before displaying any response to my user, which is an unacceptable delay.
First, make the script that needs to run in the background.
-
<?php
-
-
//ALWAYS USE THIS FOR UNATTENDED SCRIPTS, YOU DON'T WANT THEM RUNNING FOREVER
-
-
/*
-
SCRIPT THAT DOES SOMETHING YOU WANT TO RUN IN THE BACKGROUND
-
THIS SCRIPT CAN DO ANYTHING. IT IS A NORMAL PHP SCRIPT
-
HOWEVER, DO NOT MAKE A SCRIPT THAT WILL OUTPUT TO THE BROWSER
-
AS NOBODY WILL EVER SEE IT
-
*/
-
-
//YOU CAN ALSO USE sleep($seconds) TO RUN THIS AFTER A SPECIFIED AMOUNT OF TIME
-
-
$output = 'SOME DATA THAT I GENERATED';
-
-
$ftp_user_name = 'user';
-
$ftp_user_pass = 'pass';
-
$ftp_server = 'ftp.someplace.com';
-
$ftp_dir='/this_folder/';
-
-
-
// connect, login, and transfer the file
Next, make the script that executes the background script.
-
<?php
-
-
//DO A BUNCH OF STUFF HERE...
-
-
//LOCATION OF THE PAGE YOU WANT TO EXECUTE IN THE BACKGROUND
-
$page_to_execute = 'http://www.mysite.com/background_script.php';
-
-
/*
-
COMMAND TO EXECUTE THE PAGE USING WGET
-
YOU CAN SEND GET VARIABLES USING THIS
-
BE ABSOLUTELY SURE TO PROTECT AGAINST INJECTION
-
*/
-
$command = 'wget -q '.$page_to_execute;
-
-
//ALTERNATELY TO WGET YOU COULD USE SOMETHING LIKE
-
//$command = "nohup /usr/local/bin/php -f ".$page_to_execute;
-
-
/*
-
NOW EXECUTE THE PAGE IN THE BACKGROUND
-
THIS WILL NOT RETURN ANY ERROR IF IT FAILS
-
YOU WILL NEED TO WATCH YOUR SERVER LOGS
-
TO PROPERLY DEBUG
-
*/
-
There you have it. This will run a script in the background (any web page that you specify). I do not use a local path to the file, because it will often break and can pose additional security risks. Using the above code, the server can only execute wget, and the actual page is parsed just like any other page on your website.
Again, make sure you understand the potential problems enabling exec can cause. There are also other ways of doing this such as running php from the exec command (path/to/php my_script). These can pose much greater security risks if they are not properly coded.
Lastly, this should be run on user or administrator initiated event, and not on events such as page views. This could spawn a huge number of processes if it is ever allowed to run based on every page view or something similar.
Subscribe to the RSS feed and have all new posts delivered straight to you.
Are the two scripts on different machines? Why not simply call the php CLI from exec?
In this case they were.
Normally I would go with php cli:
php -f /path/to/page > /dev/null &