【问题标题】:php ssh echo $! is not show real pidphp ssh 回显 $!不显示真实的 pid
【发布时间】:2016-08-10 05:29:59
【问题描述】:

经过多次尝试,我们未能获得 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

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

编辑 2:

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

【问题讨论】:

  • 你是否正确地转义了 $ 以便 PHP 不认为它是一个变量? echo \$!
  • “真实”进程ID和“非真实”进程ID有什么区别?这里的“真实”真的是什么意思?请记住,ssh 连接肯定会创建另一个 shell 环境,因此会创建另一个进程。与 php 的 exec 命令相同。
  • Brandon 添加此 \ 不更改 id pid 不起作用
  • @arkascha if not put this " > /dev/null" 显示无限文本
  • 抱歉,我不明白你最后的评论是什么意思...

标签: php linux bash ssh


【解决方案1】:

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

【讨论】:

  • 没有帮助。应用程序 1 访问连接远程服务器 2 的 Web 服务器(服务器 1)并启动 sopcast 播放器。但是在来自服务器 1 的 php 中出现了来自服务器 2 的 pid
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
相关资源
最近更新 更多