2

I have the following which call the exec to run the script test.php in the background.

exec("/home/gooffers/test.php?one=one &");

Script test.php contains the following

$test = $_GET['one'];
echo $test;

However this is creating an infinite loop (infinite number of processes) which is crashing the server. Why is this happening.

4
  • 1
    We need to see more of how exec() is being used in your code, to see if there's an infinite loop in your main code. Commented Nov 28, 2012 at 22:33
  • @davis, This is all the code that i have as you see it in my question. Commented Nov 28, 2012 at 22:35
  • Hudhud - Thank you for replying - But just the same, could you please update your post with the complete contents of both your main script (which contains the exec() ) and the test.php script... By complete I mean "<?php" and all - full copy/paste of the entire file. In addition could you please show how you are executing your main script ? Thank you Commented Nov 28, 2012 at 22:40
  • @david, you don't understand. This is all the code in both files. One file contains just the exec and the other file contains just the 2 lines GET and echo. That's all the code that i have. Commented Nov 28, 2012 at 22:46

2 Answers 2

5

$_GET is not availible when you are running a script via commandline (php-cli).

See here on how to pass arguments to a command line script in php: How do I pass parameters into a PHP script through a webpage?

Basically, it's

exec("/home/gooffers/test.php arg1 arg2");

and then fetching them via

$argument1 = $argv[1];
$argument2 = $argv[2];
Sign up to request clarification or add additional context in comments.

6 Comments

$argv[0] refers to the script file (/home/gooffers/test.php), so $argv[1] equals the first argument. But you're right, this is the good way. Working with php processes, I'm used to use something like: list($command,$args) = array(array_shift($argv), $argv).
Reading thru pages online i thought it's possible to have it the way i posted. Although i haven't verified it yet since exec for some reason keeps going into a loop and crashing the server. Your post seems to be the correct way to do it. I will try it and see how it works. I still don't understand why exec is going into a loop. I found many posts online talking about exec and infinite loops. One person suggested that we use php-cli instead on php. Don't know if that would make a difference.
I would suggest trying it the correct way with command line arguments. Maybe this solves your infinite loop problem right away.
Very strange, must have to do something with your server / php configuration. never experienced any problems with exec. But give it a shot!
|
-3

I dont know what is happening, but i think it should be

exec("php /home/gooffers/test.php?one=one &");

1 Comment

No. Your command will work if you use wget, something like exec("wget http://localhost/gooffers/test.php?one=one &"), but not if you're using php cli. PHP cli uses C-like nommage to take arguments, so $argc and $argv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.