使用ssh和shell

时间:2015-09-16 09:32:38

标签: php linux shell ssh phpseclib

我可以运行位于linux服务器上的php脚本,如下所示:

nclude('/Net/SSH2.php');

$ssh = new Net_SSH2('ip address');
if (!$ssh->login('user name', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('/usr/bin/nohup php  /path/to/script/run.php > /path/to/log/run_log.log 2>&1 &');

现在我需要添加一些代码,如下所示,它将回复作业的完成。

$output = shell_exec('if [ $? -eq "0" ];then echo "All done" else echo "Not Work" fi');
echo $output;

但它不起作用。方法run.php在linux服务器上运行但是当它完成时,$output不会打印任何内容。能否请你帮忙?

1 个答案:

答案 0 :(得分:0)

所以这是我的答案:

$pid=0;
$pid=$ssh->exec("(ps -ef | grep run.php | grep -v grep | awk '{print $2}')");

while ($pid >0)
{
echo "Process id when running=".$pid."\n";
$pid =$ssh->exec("(ps -ef | grep run.php | grep -v grep | awk '{print $2}')");
}
echo "Process is not running \n";

<强>说明: 从Linux开始,输出将如下所示

bash-3.2$ ps -ef | grep run.php 
506       1455     1   3 10:56 ?        00:00:02 /path/to/script/run.php

所以awk'{print $ 2}'将提供值1455,这是流程的pid

相关问题