php ssh echo $!不显示真正的pid

时间:2016-08-09 14:22:41

标签: php linux bash ssh

经过多次尝试,我们未能做到的是获得pid号码。

代码:

    <?php

    $server = "optimus.xxx.xx";
    //ip address will work too i.e. 192.168.254.254 just make sure this is your public ip address not private as is the example

    //specify your username
    $username = "root";

    //select port to use for SSH
    $port = "22";

    //command that will be run on server B
    $command = "./sp-sc-auth sop://broker.sopcast.com:3912/149255 3908 8908 > /dev/null &";
    //$command = "./sop.sh sop://broker.sopcast.com:3912/149255 1234 > /dev/null & echo $!";
    //$command = "./sop.sh sop://broker.sopcast.com:3912/149255 1234 & echo $!"; // scoate si restu de info
    //$command = "ps - ef | grep -v grep | grep ./sop.sh sop://broker.sopcast.com:3912/149255 1234 | awk '{print $2}'";

    //form full command with ssh and command, you will need to use links above for auto authentication help
    $cmd_string = "ssh -p ".$port." ".$username."@".$server." ".$command;


    //this will run the above command on server A (localhost of the php file)
    exec($cmd_string, $output);
    //return the output to the browser
    //This will output the uptime for server B on page on server A

    echo '<pre>';
    print_r($output);
    echo '</pre>';

?>

如果从此交换:

$command = "./sp-sc-auth sop://broker.sopcast.com:3912/149255 3908 8908 > /dev/null & echo $!";

显示另一个数字而不是实数pid。

如果从bash中放入这个工作正常并显示pid。 我不明白哪里错了。

来自php的

显示此pid:

Array

(     [0] =&gt; 9397 )

和真正的pid是7452

编辑: 桌面访问Web服务器(服务器1)和服务器1在服务器2 sopcast播放器内启动。并且服务器2发送pid服务器1以便在web服务器json中显示。

enter image description here

编辑2:

pid number是从web服务器输出的ppid数,而不是通过ssh发送命令的服务器2。

1 个答案:

答案 0 :(得分:0)

此时此刻,你想要哪个pid并不明显。所以这就是我之前想出来的方式:

来自pids.sh

#!/bin/bash
echo "Current pid: $$"
echo "Current process: $(ps -f $$)"

ppid=$(ps -o ppid= -p $$)
echo "Parent pid: $ppid"
echo "Paren process: $(ps -f $ppid)"

gppid=$(ps -o ppid= -p $ppid)
echo "Grand Parent pid: $gppid"
echo "Grand Paren process: $(ps -f $gppid)"

./ pids.sh

Current pid: 611
Current process: UID        PID  PPID  C STIME TTY      STAT   TIME CMD
root       611 31197  0 06:41 pts/1    S+     0:00 /bin/bash ./pids.sh
Parent pid: 31197
Paren process: UID        PID  PPID  C STIME TTY      STAT   TIME CMD
root     31197 31195  0 04:55 pts/1    Ss     0:00 -bash
Grand Parent pid: 31195
Grand Paren process: UID        PID  PPID  C STIME TTY      STAT   TIME CMD
root     31195  1216  0 04:55 ?        Ss     0:00 sshd: root@pts/1

此外,linux命令pstree可以提供帮助。