Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to call shell and not block php script

Posted on 2007-08-11
6
Medium Priority
?
278 Views
Last Modified: 2013-11-05
I would like to call "curl http://www.google.com" and not have it block my php script.

For example:

<?

function dontBlock(){
    [calls curl]
}

dontBlock();
dontBlock();
dontBlock();

echo "Done.";

?>

This script should finish immediately, rather than waiting for each request of google to complete.
0
Comment
Question by:samsonite1023
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:The_Blasted_One
ID: 19677408
Sorry, you can't do that. Script needs to be runned while it runs.
You can only do
ini_set('max_execution_time', $time_in_seconds)
to be sure the script will be terminated in a short time (if connection is delayed for some reason, for ex.)
Or you can try to use command line version of CURL via "exec". You will be able to run CURL in background then (although I see no reason for this, because CURL will occupy CPU and memory instead of PHP).
0
 
LVL 5

Expert Comment

by:The_Blasted_One
ID: 19677439
Sorry, haven't figured you already use command line CURL, not php extension.

Yes, of course, "exec" can run in background.
"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."
So, use exec("curl http://www.google.com >/dev/null") instead if you don't need the output.
0
 
LVL 1

Author Comment

by:samsonite1023
ID: 19677604
The Blasted One

I tried:

<?php
exec("curl http://www.google.com >/dev/null");
echo "done.";
?>

It's still blocking, as the script hangs for a bit [curl runs extremely slow on the environment I'm in] then echo's "done" If I change the exec line to "exec(echo 'hello');" it  finishes quickly, thus it is not merely the invokation to exec() that is slow.

As an aside, running "curl http://www.google.com >/dev/null" from command line itself still outputs the statistics of the operation... perhaps setting a flag on curl would prevent this?

Any other ideas?

Your help is greatly appreciated
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
The_Blasted_One earned 375 total points
ID: 19677752
Flag... yes, you can try
exec ("curl 'http://www.google.com' -o /dev/null > /dev/null &");

And of course, if you (by accident) debug under Windows, you should use "nul" instead of "/dev/null"
0
 
LVL 1

Author Comment

by:samsonite1023
ID: 19678019
When I run [curl 'http://www.google.com' -o /dev/null > /dev/null &] from console it STILL outputs stuff.

Maybe there's a php function that just spawns a shell and lets it run, regardless of its output?
0
 
LVL 5

Expert Comment

by:The_Blasted_One
ID: 19678096
Weird behaviour.
BTW, have you already tried to exec it in php? "&" should force the process to run in background regardless of its input.
Nope, there's no such straight solutions((

There's a kind of hack to stop any output (and thus set the script free), you can check it also:
pclose(popen($cmd));

Hope this will help.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question