php ssh 回显 $!不显示真实的 pid

标签 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] => 9397 )

真正的pid是7452

编辑: 桌面访问网络服务器(服务器 1)和服务器 1 在服务器 2 sopcast 播放器中启动。服务器 2 向服务器 1 发送 pid 以在 Web 服务器 json 中显示。

enter image description here

编辑 2:

pid 号是从网络服务器发出的 ppid 号,而不是它运行通过 ssh 发送的命令的服务器 2。

最佳答案

此时,你想要哪个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 可以提供帮助。

关于php ssh 回显 $!不显示真实的 pid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38853417/

相关文章:

linux - 找到 : missing argument to -exec with ssh

c - Linux 操作系统 - 来自父进程、子进程、父进程的输入输出管道

php - 我的查询不起作用。我能做些什么?

php - 如果 secure 为 false,是否可以在 HTTPS 中制作 cookie,并在 HTTP 中使用?

python - 仅在数据到达时管道

linux - 如何从多个文件中打印每第 n 行并将其写为第 n 行的每一组?

php - 处理 csv、bash、php、c/c++ 的最快处理速度

bash - 脚本终止后删除文件

javascript - 如何跟踪通过 jQuery (ajax) 提交到服务器的请求的进度?

php - PHP 和 MySQL 的问题