[英]Run php script in linux server from another php script using ssh and shell
我可以運行位於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
不會輸出任何內容。 能否請你幫忙?
所以這是我的答案:
$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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.