473,395 Members | 1,442 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,395 software developers and data experts.

Launch a background task from a PHP script?

Any idea how I can launch a background task from a PHP script?

For example, when a user posts on my message board, it may fire many
e-mail notifications to other users, and other tasks. I want the
posting confirmation page to end displaying quickly, without waiting
for those notifications or other tasks to complete.

So basically what I want is to launch 'background.php' from 'main.php',
with the user seeing only the output of - and waiting only for the end
of - 'main.php'. Without the need to wait for the end of the possibly
long 'background.php', like if I had simply included it in 'main.php'.

According to the PHP Manual, the function register_shutdown_function()
was the solution "in PHP 4.0.6 and earlier under Apache", but "since
PHP 4.1 the shutdown functions are called as the part of the request"
so that PHP waits until the shutdown functions are completed before
closing the connection to the user.

E.g. in PHP 4.1+ this

<?php
function background () {
echo 'Starting background task... ';
// Do something long that I don't want the user to see or wait for
sleep(5);
echo 'End of background task. ';
}

echo 'Starting main script... ';
register_shutdown_function('background');
echo 'End of main script. ';
?>

will output:

Starting main script... End of main script. Starting background task...
End of background task.

I want a way to output only:

Starting main script... End of main script.

and do the remaining in background.

Feb 20 '06 #1
6 9615
Francois Bonzon wrote:
Any idea how I can launch a background task from a PHP script?

For example, when a user posts on my message board, it may fire many
e-mail notifications to other users, and other tasks. I want the posting
confirmation page to end displaying quickly, without waiting for those
notifications or other tasks to complete.

So basically what I want is to launch 'background.php' from 'main.php',
with the user seeing only the output of - and waiting only for the end
of - 'main.php'. Without the need to wait for the end of the possibly
long 'background.php', like if I had simply included it in 'main.php'.

According to the PHP Manual, the function register_shutdown_function()
was the solution "in PHP 4.0.6 and earlier under Apache", but "since PHP
4.1 the shutdown functions are called as the part of the request" so
that PHP waits until the shutdown functions are completed before closing
the connection to the user.

E.g. in PHP 4.1+ this

<?php
function background () {
echo 'Starting background task... ';
// Do something long that I don't want the user to see or wait for
sleep(5);
echo 'End of background task. ';
}

echo 'Starting main script... ';
register_shutdown_function('background');
echo 'End of main script. ';
?>

will output:

Starting main script... End of main script. Starting background task...
End of background task.

I want a way to output only:

Starting main script... End of main script.

and do the remaining in background.

What platform are you on?

if this is UNIX then you can do an exec() or system() call or something like
`./executethisfile > out.log &`
note these are back-ticks (key on the top row far left ~` )

the "&" tells the shell script to execute in the background. Not sere how you
would accomplish this on the Windows platform, but then again if you are doing
anything serious, Windows is not your server platform.

--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Feb 20 '06 #2
On Mon, 20 Feb 2006 02:00:38 +0100, wrote:
Any idea how I can launch a background task from a PHP script?


pcntl_fork will do that for you.

--
http://www.mgogala.com

Feb 20 '06 #3
On Mon, 20 Feb 2006 02:44:13 +0000, Michael Austin wrote:
What platform are you on?

if this is UNIX then you can do an exec() or system() call or something like
`./executethisfile > out.log &`
note these are back-ticks (key on the top row far left ~` )


On the Unix-like operating systems, there is PCNTL extension which has
calls like fork, exec, wait and signal. This extension is not available
on Windows. On windows, there are .COM and .NOT functions in the manual.
Not being a windows person, I don't really know whether those can be
used.

--
http://www.mgogala.com

Feb 20 '06 #4
d
"Michael Austin" <ma*****@firstdbasource.com> wrote in message
news:12******************@newssvr25.news.prodigy.n et...
Francois Bonzon wrote:
Any idea how I can launch a background task from a PHP script?

For example, when a user posts on my message board, it may fire many
e-mail notifications to other users, and other tasks. I want the posting
confirmation page to end displaying quickly, without waiting for those
notifications or other tasks to complete.

So basically what I want is to launch 'background.php' from 'main.php',
with the user seeing only the output of - and waiting only for the end
of - 'main.php'. Without the need to wait for the end of the possibly
long 'background.php', like if I had simply included it in 'main.php'.

According to the PHP Manual, the function register_shutdown_function()
was the solution "in PHP 4.0.6 and earlier under Apache", but "since PHP
4.1 the shutdown functions are called as the part of the request" so that
PHP waits until the shutdown functions are completed before closing the
connection to the user.

E.g. in PHP 4.1+ this

<?php
function background () {
echo 'Starting background task... ';
// Do something long that I don't want the user to see or wait for
sleep(5);
echo 'End of background task. ';
}

echo 'Starting main script... ';
register_shutdown_function('background');
echo 'End of main script. ';
?>

will output:

Starting main script... End of main script. Starting background task...
End of background task.

I want a way to output only:

Starting main script... End of main script.

and do the remaining in background.
What platform are you on?

if this is UNIX then you can do an exec() or system() call or something
like
`./executethisfile > out.log &`
note these are back-ticks (key on the top row far left ~` )

the "&" tells the shell script to execute in the background. Not sere how
you would accomplish this on the Windows platform, but then again if you
are doing anything serious, Windows is not your server platform.


Grow up :) There are plenty of serious things one can do on Windows, infact
some one can only do on Windows.
--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)

Feb 20 '06 #5
d wrote:
"Michael Austin" <ma*****@firstdbasource.com> wrote in message
news:12******************@newssvr25.news.prodigy.n et...
Francois Bonzon wrote:

Any idea how I can launch a background task from a PHP script?

For example, when a user posts on my message board, it may fire many
e-mail notifications to other users, and other tasks. I want the posting
confirmation page to end displaying quickly, without waiting for those
notifications or other tasks to complete.

So basically what I want is to launch 'background.php' from 'main.php',
with the user seeing only the output of - and waiting only for the end
of - 'main.php'. Without the need to wait for the end of the possibly
long 'background.php', like if I had simply included it in 'main.php'.

According to the PHP Manual, the function register_shutdown_function()
was the solution "in PHP 4.0.6 and earlier under Apache", but "since PHP
4.1 the shutdown functions are called as the part of the request" so that
PHP waits until the shutdown functions are completed before closing the
connection to the user.

E.g. in PHP 4.1+ this

<?php
function background () {
echo 'Starting background task... ';
// Do something long that I don't want the user to see or wait for
sleep(5);
echo 'End of background task. ';
}

echo 'Starting main script... ';
register_shutdown_function('background');
echo 'End of main script. ';
?>

will output:

Starting main script... End of main script. Starting background task...
End of background task.

I want a way to output only:

Starting main script... End of main script.

and do the remaining in background.

What platform are you on?

if this is UNIX then you can do an exec() or system() call or something
like
`./executethisfile > out.log &`
note these are back-ticks (key on the top row far left ~` )

the "&" tells the shell script to execute in the background. Not sere how
you would accomplish this on the Windows platform, but then again if you
are doing anything serious, Windows is not your server platform.

Grow up :) There are plenty of serious things one can do on Windows, infact
some one can only do on Windows.


I have not found anything "serious" I can do on windows that I can't do on other
platforms - and MY primary OS does not end in IX. :)
--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Long Live OpenVMS.
Feb 20 '06 #6
On 2006-02-20 03:44:13 +0100, Michael Austin said:
Francois Bonzon wrote:
Any idea how I can launch a background task from a PHP script?

For example, when a user posts on my message board, it may fire many
e-mail notifications to other users, and other tasks. I want the
posting confirmation page to end displaying quickly, without waiting
for those notifications or other tasks to complete.

So basically what I want is to launch 'background.php' from 'main.php',
with the user seeing only the output of - and waiting only for the end
of - 'main.php'. Without the need to wait for the end of the possibly
long 'background.php', like if I had simply included it in 'main.php'.

According to the PHP Manual, the function register_shutdown_function()
was the solution "in PHP 4.0.6 and earlier under Apache", but "since
PHP 4.1 the shutdown functions are called as the part of the request"
so that PHP waits until the shutdown functions are completed before
closing the connection to the user.

E.g. in PHP 4.1+ this

<?php
function background () {
echo 'Starting background task... ';
// Do something long that I don't want the user to see or wait for
sleep(5);
echo 'End of background task. ';
}

echo 'Starting main script... ';
register_shutdown_function('background');
echo 'End of main script. ';
?>

will output:

Starting main script... End of main script. Starting background task...
End of background task.

I want a way to output only:

Starting main script... End of main script.

and do the remaining in background.

What platform are you on?

if this is UNIX then you can do an exec() or system() call or something like
`./executethisfile > out.log &`
note these are back-ticks (key on the top row far left ~` )

the "&" tells the shell script to execute in the background. Not sere
how you would accomplish this on the Windows platform, but then again
if you are doing anything serious, Windows is not your server platform.


I'm on a UNIX platform. I tested your solution, and it works fine. Thank you.

However, some hosts where I plan to use this disabled the exec() and
system() functions. The pcntl extension is also disabled there. So I
guess it can't be helped here.

Feb 20 '06 #7

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

Similar topics

8
by: Bob Bedford | last post by:
I've a script that must "launch" an other script on some situations. firstscript.php: .... If ($newarticle) //launch secondscript.php .... The "secondscript.php" may take quite long, and is...
11
by: Konrad Den Ende | last post by:
I have a function returning a string but the problem is that the color of it is blue which suits me well for some pages but not for others. Is it possible to "feel" what the color of the background...
1
by: hsmcdonald | last post by:
Hello all, I have a mail function that sends parsed information to an employee distribution list. I was trying to setup a process where the admin can initiate a letter to this list, and...
8
by: Marcus | last post by:
I have this application I have made that I launch when the user logs into Windows XP. I would like to delay the launch of the application so that it starts 1 minute after the user has logged in. ...
8
by: Ulysse | last post by:
Hello, I have a python script which runs all the time (using of library threading). I would like this scipt to run on a remote linux Os using Putty. The problem is, when I close Putty command...
2
by: Cappamontana | last post by:
Hi, I was wondering if somebody knows a simple way to update a (status) text in PHP without refressing. The situation is as follows; I have a class that scrawls a domain and gathers the keyword...
7
by: rahismailbox | last post by:
I need to run a script in every 10 seconds in background of the site of 10-15 php pages. So how do i activate that script? Is there a code to be embedded in every php page so that the script got...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.