0

I'm attempting to use this code from here: https://stackoverflow.com/a/4350418/3672303

for ($i=0; $i<10; $i++) {
    // open ten processes
    for ($j=0; $j<10; $j++) {
        $pipe[$j] = popen('script2.php', 'w');
    }

    // wait for them to finish
    for ($j=0; $j<10; ++$j) {
        pclose($pipe[$j]);
    }
}

When I execute it, I get the following:

sh: 1: script2.php: not found
sh: 1: script2.php: not found
sh: 1: script2.php: not found...... (repeated a bunch of times)

script2.php is in the same directory. I tried using the full path to the file, and my result is the same.

I'm assuming it could be some type of weird PHP path/environment issue?... How do I fix?

Any help is much appreciated.

Wyatt Jackson
  • 303
  • 1
  • 2
  • 11
  • 1
    try `popen(__DIR__ . '/script2.php', 'w')` – Sysix Jun 22 '21 at 19:31
  • 1
    popen executes from the context of php's current directory. Give it a path to the script you want to execute. – NotMe Jun 22 '21 at 19:33
  • @Sysix I tried popen(__DIR__ . '/script2.php', 'w') and the error message changed to: /fullpath/script2.php: 1: /fullpath/script2.php: cannot open ?PHP: No such file /fullpath/script2.php: 3: /fullpath/script2.php: Syntax error: "(" unexpected – Wyatt Jackson Jun 22 '21 at 20:22
  • The contents of script2.php has no errors when ran alone. It just echos out a test message. echo "test"; – Wyatt Jackson Jun 22 '21 at 20:23
  • 2
    My first guess is that your computer doesn't know how to run PHP files by themselves, they aren't executable. I think you need to run `popen('php ' . __DIR__ . '/script2.php', 'w')`, and the `php` might need to be fully qualified, too, such as `/usr/bin/php` – Chris Haas Jun 22 '21 at 21:31
  • @ChrisHaas This worked! Thank you so much. – Wyatt Jackson Jun 22 '21 at 22:01

1 Answers1

1

"Chris Hass" in the comments above came up with the solution. Changing this line worked.

$pipe[$j] = popen('script2.php', 'w');   // Does not work

to

popen('php ' . __DIR__ . '/script2.php', 'w')  // Works correctly.
Wyatt Jackson
  • 303
  • 1
  • 2
  • 11