473,411 Members | 2,186 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,411 software developers and data experts.

run a php in the background using http

Hi,

i have a script (script1) which needs to call another script (script2).
It should not wait for the answer, so script2 should run in the background.

I really need to call the php script using:
http://urlof myscript/script2.php

I don't want to use it as a cgi and use CLI.

I did a test with fsockopen and curl and a timeout of 10 seconds and call
http://urlof myscript/script1.php from my browser:
Script1 stops after 10 seconds => OK
Script2 has been called by script1 and continue to run in the background
=>OK
After 60 seconds Script2 is killed by php!! => KO :(

The script is working fine if i call http://urlof myscript/script2.php from
my browser. It needs around 120 seconds to finish.

Why is it stopping after 60 seconds when I use fsockopen and call it from
script1?

Here is the script in SCRIPT1:
$fp = fsockopen("$host", 80, $errno, $errstr, 30);
stream_set_timeout($fp,10);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET $path HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);
$status = socket_get_status($fp);

while (!feof($fp) && !$status['timed_out']) {
echo fgets($fp, 128);
$status = socket_get_status($fp);
}
fclose($fp);
}
I have added this in SCRIPT2:
ignore_user_abort(FALSE); // becareful, FALSE really means it should ignore
user abort !!!
set_time_limit (120);
ini_set("max_input_time",120);
ini_set("max_execution_time",120);
Do you have any idea??

Thanks you for your help

Rod
Jul 17 '05 #1
12 3023
WebRod wrote:
Hi,

i have a script (script1) which needs to call another script (script2).
It should not wait for the answer, so script2 should run in the background.

I really need to call the php script using:
http://urlof myscript/script2.php

I don't want to use it as a cgi and use CLI.

I did a test with fsockopen and curl and a timeout of 10 seconds and call
http://urlof myscript/script1.php from my browser:
Script1 stops after 10 seconds => OK
Script2 has been called by script1 and continue to run in the background
=>OK
After 60 seconds Script2 is killed by php!! => KO :(

The script is working fine if i call http://urlof myscript/script2.php from
my browser. It needs around 120 seconds to finish.

Why is it stopping after 60 seconds when I use fsockopen and call it from
script1?

Here is the script in SCRIPT1:
$fp = fsockopen("$host", 80, $errno, $errstr, 30);
stream_set_timeout($fp,10);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET $path HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);
$status = socket_get_status($fp);

while (!feof($fp) && !$status['timed_out']) {
echo fgets($fp, 128);
$status = socket_get_status($fp);
}
fclose($fp);
}
I have added this in SCRIPT2:
ignore_user_abort(FALSE); // becareful, FALSE really means it should ignore
user abort !!!
set_time_limit (120);
ini_set("max_input_time",120);
ini_set("max_execution_time",120);
Do you have any idea??

Thanks you for your help

Rod


Why not exec() it?

exec("nohup /path/to/php /path/to/script2.php &");

I used something similar to launch mpg123 when I was playing around with
a web-based media player.

NM

--
convert UPPERCASE NUMBER to a numeral to reply
Jul 17 '05 #2

"WebRod" <no****@bouygtel.fr> wrote in message
news:42***********************@news.club-internet.fr...
Hi,

i have a script (script1) which needs to call another script (script2).
It should not wait for the answer, so script2 should run in the background.
I really need to call the php script using:
http://urlof myscript/script2.php

I don't want to use it as a cgi and use CLI.

I did a test with fsockopen and curl and a timeout of 10 seconds and call
http://urlof myscript/script1.php from my browser:
Script1 stops after 10 seconds => OK
Script2 has been called by script1 and continue to run in the background
=>OK
After 60 seconds Script2 is killed by php!! => KO :(

The script is working fine if i call http://urlof myscript/script2.php from my browser. It needs around 120 seconds to finish.

Why is it stopping after 60 seconds when I use fsockopen and call it from
script1?

Here is the script in SCRIPT1:
$fp = fsockopen("$host", 80, $errno, $errstr, 30);
stream_set_timeout($fp,10);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET $path HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);
$status = socket_get_status($fp);

while (!feof($fp) && !$status['timed_out']) {
echo fgets($fp, 128);
$status = socket_get_status($fp);
}
fclose($fp);
}
I have added this in SCRIPT2:
ignore_user_abort(FALSE); // becareful, FALSE really means it should ignore user abort !!!
set_time_limit (120);
ini_set("max_input_time",120);
ini_set("max_execution_time",120);
Do you have any idea??

Thanks you for your help

Rod


It would help if you use ignore_user_abort() correctly.
ignore_user_abort(true) means ignore user abort.
Jul 17 '05 #3
News Me wrote:
WebRod wrote:
Hi,

i have a script (script1) which needs to call another script (script2).
It should not wait for the answer, so script2 should run in the
background.


Why not exec() it?

exec("nohup /path/to/php /path/to/script2.php &");

I used something similar to launch mpg123 when I was playing around with
a web-based media player.


'cos:
1) the OP said he didn't want to run it from the CLI
2) it's part of the same process group so the original script will hang
around while it's running (hint: use 'at now ...' instead of '...&'

C.

Jul 17 '05 #4
> It would help if you use ignore_user_abort() correctly.
ignore_user_abort(true) means ignore user abort.


actually with windows xp sp2 and php 4.3.10, it's the opposite.
ignore_user_abort(false) means ignore user abort.

with linux
ignore_user_abort(true) means ignore user abort.
Rod

Jul 17 '05 #5
NC
WebRod wrote:

i have a script (script1) which needs to call another script (script2). It should not wait for the answer, so script2 should run in the background.
I really need to call the php script using:
http://urlof myscript/script2.php

I don't want to use it as a cgi and use CLI.
OK, then use wget and fork its execution into background.
Why is it stopping after 60 seconds when I use fsockopen
and call it from script1?


Perhaps your system has an execution time limit not overridable
from the scripts?

Cheers,
NC

Jul 17 '05 #6

"WebRod" <no****@bouygtel.fr> wrote in message
news:42**********************@news.club-internet.fr...
It would help if you use ignore_user_abort() correctly.
ignore_user_abort(true) means ignore user abort.


actually with windows xp sp2 and php 4.3.10, it's the opposite.
ignore_user_abort(false) means ignore user abort.

with linux
ignore_user_abort(true) means ignore user abort.


I don't know where you got your info from, pal, but it is wrong.

main.c, line 1759

/* {{{ php_handle_aborted_connection
*/
PHPAPI void php_handle_aborted_connection(void)
{
TSRMLS_FETCH();

PG(connection_status) = PHP_CONNECTION_ABORTED;
php_output_set_status(0 TSRMLS_CC);

if (!PG(ignore_user_abort)) {
zend_bailout();
}
}
/* }}} */

Jul 17 '05 #7
Colin McKinnon wrote:
News Me wrote:

WebRod wrote:
Hi,

i have a script (script1) which needs to call another script (script2).
It should not wait for the answer, so script2 should run in the
background.


Why not exec() it?

exec("nohup /path/to/php /path/to/script2.php &");

I used something similar to launch mpg123 when I was playing around with
a web-based media player.

'cos:
1) the OP said he didn't want to run it from the CLI
2) it's part of the same process group so the original script will hang
around while it's running (hint: use 'at now ...' instead of '...&'


1) The OP is not running it from the command line, the webserver is.
2) When I ran the script on a Fedora Core System, it didn't hang.

NM

--
convert UPPERCASE NUMBER to a numeral to reply
Jul 17 '05 #8
> I don't know where you got your info from, pal, but it is wrong.

Actually i did a small test to ckeck that.
The same test (script 1 is calling script2 using fsockopen and a small
timeout):

ignore_user_abort(true)
- windows => script2 stops WHEN script 1 stops (!!)
- linux => script2 will stop only at the end. (as expected)

ignore_user_abort(false)
- windows => script2 will stop only at the end. (!!)
- linux => script2 stops WHEN script 1 stops (as expected)

Maybe i have a corrupted version of php??
I have windows XP SP2, php 4.3.10

Rod
Jul 17 '05 #9
1) The OP is not running it from the command line, the webserver is.

Actually it's the same thing.
I explain you why I don't want to use it. My web server is on OVH.COM.
My script running from the CLI (from telnet or exec) use too many
ressources (!!) on the server and they kill it and send me an email to stop
this script or they will disable my website!!
The same script (I just change the extension from .cg to .ph and remove the
first line which specify the php path) is running very well, they don't kill
it, no problem!!

They don't explain me why there is a difference, so i don't want to use the
CLI anymore with them.

Jul 17 '05 #10
Hi to all,

I have solved the problem.
The buffer was full because my script (script2) was displaying some output.

When the buffer is full, php stops the execution of the script!
I guess it happens because script1 (the caller) is already stopped

So i have removed all the "echo" and now it works like a charm!!!!

Rod
Jul 17 '05 #11
"WebRod" <no****@bouygtel.fr> wrote in message
news:42**********************@news.club-internet.fr...
I don't know where you got your info from, pal, but it is wrong.


Actually i did a small test to ckeck that.
The same test (script 1 is calling script2 using fsockopen and a small
timeout):

ignore_user_abort(true)
- windows => script2 stops WHEN script 1 stops (!!)
- linux => script2 will stop only at the end. (as expected)

ignore_user_abort(false)
- windows => script2 will stop only at the end. (!!)
- linux => script2 stops WHEN script 1 stops (as expected)

Maybe i have a corrupted version of php??
I have windows XP SP2, php 4.3.10

Rod


Your test was probably faulty. PHP only knows a connection is broken when it
tries to output data to the network socket. A snippet like the following
will run to the end despite a user abort since there isn't enough data to
overfill the default 4096 byte buffer:

for($i = 0; $i < 120; $i++) {
echo "Hello";
sleep(1);
}
Jul 17 '05 #12
> Your test was probably faulty.

I don't think so
PHP only knows a connection is broken when it
tries to output data to the network socket.
You're absoluty right, i know that point.
A snippet like the following

[..]

You're right, in that case could you provide a "right" script?

What is sure is that script2 (the real one) returns a lot of information.
(1) If I have ignore_user_abort(true), then script2 stops WHEN script 1
stops.
(2) If I have ignore_user_abort(false), then script2 will sotp at the end

What is the difference between (1) and (2)?
The flag and the duration of the script. (the content of the script is the
same, if it was a problem of empty buffer as you said, (1) and (2) will both
stop only at the end of the script)

But as i said it could only be a problem with [php 4.3.10/xp sp2] or a
specific problem on my machine (??)

Rod

Jul 17 '05 #13

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

Similar topics

7
by: ryan.fairchild | last post by:
Ok I basically am creating a container for a bunch of other <div> tags. But I want to make four jpegs/gif/whatever that goes in each corner so they look rounded. I thought this is how I would do...
8
by: Ron Holmes | last post by:
I want to place a background image on a windows form. Is there a way to prevent the image from Tiling without using an image box resized to the size of the form? I am using Visual Studio 2003...
5
by: proximus | last post by:
Hi, I am trying to change the background of table TD's. The problem is that I have no access to the HTML code. SO I am trying to alter this using Javascript/DOM in an external .js file. I...
7
by: Nilesh | last post by:
I am using background-image attribute in a CSS file and linking the CSS file to aspx page. But strangly, background-image attribute is not working for relative URL. e.g. If I apply following css...
3
by: Viken Karaguesian | last post by:
Hello all, I need somehelp with background tiling. I have a sneaking suspicion that what I want to do is not possible, but I'll ask anyway. :>) First some background: Here's the site in...
2
by: Mark | last post by:
IE creates an object of every ID'd HTML tag (so it appears), and each object sets a property for any parameter I set in the tag. For example, my HTML might be: <td id='cell1'...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
8
by: Brian Ward | last post by:
I am looking for a simple way to set the image transparency in a PictureBox. I have a moving PictureBox containing a graphic image .. moving by incrementing its Left property. The background...
2
XedinUnknown
by: XedinUnknown | last post by:
Hi! I am new to this forum, but not new to web design and programming. Nevertheless, I have never tried to use PNG so extensively in my pages. here's the problem. First, I have found that the...
2
by: thephatp | last post by:
I'm having a problem with IE rendering correctly. I'm experimenting with using all div's in my pages now, and I'm not very familiar with the quirks of IE. I have created a sample page, and I'm...
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: 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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.