473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Starting a PHP script in the background?

What is the appropriate way to start a PHP script in the background? I
have a script that manipulates image data, and it takes a long time to
run. I'd like to send this process to the background at the appropriate
time, so that it can go do its job without holding up the user.

I've seen something like this used:

system("php myscript.php &");

But the PHP manual entry for the system() function call indicates the
following:

"Note: If you start a program using this function and want to leave it
running in the background, you have to make sure that the output of
that program is redirected to a file or some other output stream or
else PHP will hang until the execution of the program ends."

I don't expect this background script to output anything, but suppose
an error occurs during execution? Will PHP get tied up as a result
(trying to output an error message)?

How do I properly execute a PHP script in the background from another
PHP script?

Thanks in advance!
Jonah Bishop

Sep 22 '06 #1
2 2054
NC
Jonah Bishop wrote:
>
I have a script that manipulates image data, and it takes
a long time to run. I'd like to send this process to the
background at the appropriate time, so that it can go do
its job without holding up the user.

I've seen something like this used:

system("php myscript.php &");

But the PHP manual entry for the system() function call
indicates the following:

"Note: If you start a program using this function and want
to leave it running in the background, you have to make
sure that the output of that program is redirected to a file
or some other output stream or else PHP will hang until
the execution of the program ends."

I don't expect this background script to output anything,
but suppose an error occurs during execution? Will PHP
get tied up as a result (trying to output an error message)?
There are at least two ways of dealing with this issue:

1. Redirect the script's output into a file:

system("php myscript.php & myscriptlog.txt");
system("php myscript.php & >myscriptlog.txt");

The former will write any output the script produces into
a newly created file myscriptlog.txt; the latter will append
output the script produces to myscriptlog.txt

2. Buffer the output at the beginning of the script and
discard it at the end:

@ob_start();
// the script goes here...
@ob_end_clean();

Cheers,
NC

Sep 22 '06 #2
NC wrote:
Jonah Bishop wrote:
>>
But the PHP manual entry for the system() function call
indicates the following:

"Note: If you start a program using this function and want
to leave it running in the background, you have to make
sure that the output of that program is redirected to a file
or some other output stream or else PHP will hang until
the execution of the program ends."

There are at least two ways of dealing with this issue:

1. Redirect the script's output into a file:

system("php myscript.php & myscriptlog.txt");
system("php myscript.php & >myscriptlog.txt");
<snip>
almost right.
>
2. Buffer the output at the beginning of the script and
discard it at the end:

@ob_start();
// the script goes here...
@ob_end_clean();
wrong - this leaves stdout bound to the same file as the parent process so
will be ineffective.

Since the parent process must wait for a sig child, simply redirecting the
output is not a very clean solution. Really you want the new process to run
in a seperate session - its possible to this with fork() and
posix_setsid(), but a much simpler solution is to hand it off to daemon
like 'at':

`echo /usr/bin/php -q $my_php_script | at now`

C.
Sep 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Arun | last post by:
Hi, This is a scripting question, but since I am writing the script in python I am posting this question here: I have a python script that runs a simulator (that was written in c++, so I use...
1
by: bayouprophet | last post by:
Cant get menu script to to put linked page in the same frame. I am new to Java and I am wondering what am I doing wrong? below are my java applet file, frame.html file, and my text file and one...
3
by: P Wolpert | last post by:
This is my first post. I hope I don't sound stupid. I have a script conflict when I put two scripts on one page. Both scripts will work if I use one at a time but the menu button script will not...
1
by: David. E. Goble | last post by:
I have a slideshow, it uses sigsImages=new Array()"); sigsImages="sigs/finished1.jpg" sigsImages="sigs/foghorn.jpg" sigsImages="sigs/motto.jpg" sigsImages="sigs/sig.jpg"...
8
by: cypher543 | last post by:
This has been driving me insane for the last hour or so. I have search everywhere, and nothing works. I am trying to use the subprocess module to run a program and get its output line by line. But,...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
7
by: tim | last post by:
Hi all, I want to execute a VB application from a ASP page (not .Net). Want I want to do is that I write some parameters to a database and then start the external application that will pickup...
5
by: camphor | last post by:
hi, I have found an upload script in hotscripts and have implemented it into the website, I followed the installation steps to 'give write permissions to php on the upload folder (which is...
7
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.