PHP / SSH2 Multi-threading

Posted by Asad Moeen on Server Fault See other posts from Server Fault or by Asad Moeen
Published on 2012-07-10T10:13:53Z Indexed on 2012/10/15 15:40 UTC
Read the original article Hit count: 255

Filed under:
|

I'm basically done using SSH2 with PHP. Some may already that while using it, the PHP code actually waits for all the listed commands to be executed in SSH and when everything is done, it then gives back the results. Where that is fine for the work I am doing, but I need some commands to be multi-threaded.

$cmd= MyCommand; echo $ssh->exec($cmd);

So I just want this to run in parallel 2 times. I googled some stuff but didn't get along with it. For a basic thing, I came across to this way posted by someone but it didn't work out for me.

for ($i = 0; $i < 2; $i += 1)
    {
        exec("php test_1.php $i > test.txt &");  
    //this will execute test_1.php and will leave this process executing in the background and will go to next iteration of the loop immediately without waiting the completion of the script in the test_1.php , $i  is passed as argument .
}

I tried to put it this way exec("echo $ssh->exec($cmd) $i > test.txt &"); in the loop but either it never entered the loop or the echo $ssh->exec failed. I don't really need a very neat multi-threading. Even a single second delay would do good, thank you.

© Server Fault or respective owner

Related posts about php

Related posts about multi