Jump to content

Can't run a php command in background on a remote box (using expect module)


aniraj

Recommended Posts

 

Hi, I have this command with me nohup $path -h $source:11211 -b 0 -d $destination:11211 -N $destination -F -A -v>/tmp/mylog.log

 

I have written a remote execution function using expect module in php. I want the command to run on a local box and update mylog , I want to initiate this command on the remote box from my local php script but my script shouldn't wait for this to continue.

 

i had tried giving & in the end. Tried forking the command in another script ( Here i got the desired process running, but my main script waits for this one to complete )

 

Any leads? :shrug:

Note:

 

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

 

Edit

 

Never-mind, I just noticed you're already doing that. Are you running on safe-mode?

system() is same as shell_exec() right? i've tried that, i even tried writing the command to a shell script, copying the script using scp and tried executing the script on the remote box using my remote_execution function, this works fine for all the commands, but with an & , the thing gets stuck.

see, using shell_exec() or system() or whatever, i can run a command in background using & , eg:- shell_exec("command &"), the same doesn't work with remote execution. i thought it was because i didn escape &. tried shellescapearg("&") also . Didn't work.

i wrote this function

 

function remote_execution($remote_machinename, $command_to_be_executed)

{

                global $username,$password;

                ini_set("expect.loguser", "Off");

                $stream = fopen("expect://ssh $username@$remote_machinename $command_to_be_executed", "r");

 

                $cases = array (

                                array (0 => "password:", 1 => "password"),

                                array ("yes/no)?", "yesno")       

                );

 

                switch (expect_expectl ($stream, $cases)) {

                                case "password":

                                                fwrite ($stream, "$password\n");

                                                break;

               

                                case "yesno":

                                                fwrite ($stream, "yes\n");

                                                break;

 

                                default:

                                                die ("Error was occurred while connecting to the remote host!\n");

                }

 

                $output = "";

                while ($line = fgets($stream)) {

                                  $output = $output.$line;

                }

                fclose ($stream);

                return $output;

}

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.