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.

hedhud
  • 73
  • 1
  • 2
  • 9
  • 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. – David Farrell Nov 28 '12 at 22:33
  • @davis, This is all the code that i have as you see it in my question. – hedhud Nov 28 '12 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 " – David Farrell Nov 28 '12 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. – hedhud Nov 28 '12 at 22:46

2 Answers2

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];
Community
  • 1
  • 1
David Müller
  • 5,291
  • 2
  • 29
  • 33
  • 1
    `$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)`. – Alain Tiemblo Nov 28 '12 at 22:41
  • 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. – hedhud Nov 28 '12 at 22:52
  • http://stackoverflow.com/questions/3615713/exec-cause-an-infinite-loop-of-starting-requested-program – hedhud Nov 28 '12 at 22:53
  • I would suggest trying it the correct way with command line arguments. Maybe this solves your infinite loop problem right away. – David Müller Nov 28 '12 at 22:54
  • Very strange, must have to do something with your server / php configuration. never experienced any problems with exec. But give it a shot! – David Müller Nov 28 '12 at 22:55
  • @hedhud Try what http://www.php.net/manual/en/function.exec.php#101506 suggests for running a background process. – irrelephant Nov 28 '12 at 22:57
-3

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

exec("php /home/gooffers/test.php?one=one &");
Germán Rodríguez
  • 4,284
  • 1
  • 19
  • 19
  • 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`. – Alain Tiemblo Nov 28 '12 at 22:47