doujing6436
2013-12-04 20:02
浏览 272
已采纳

在后台运行脚本并从php返回其pid

I try to invoke an app inside php:

ob_start();
passthru("(cd /opt/server/TrackServer/release && ./TrackServer& ) && pidof TrackServer");
$pid = ob_get_clean();
var_dump($pid);
exit;

The goal is to run TrackServer within its path and to get it's process id so I can close it after I do some test.

When I run the command in terminal:

(cd /opt/server/TrackServer/release && nohup ./TrackServer&) && pidof TrackServer

I get correct pid returned but in php the command stops and doesn't go further, the TrackServer is started and running but I have to kill it from terminal to unblock the php script, after killing the process the php script prints the correct pid for the process I've just closed from terminal.

  1. Why the command stops?

  2. Is there a way to make it run in php the way I'am trying to run it (without forking to a new thread)?

图片转代码服务由CSDN问答提供 功能建议

我尝试在php中调用一个应用程序:

  ob_start  (); 
passthru(“(cd / opt / server / TrackServer / release&& ./TrackServer&)&& pidof TrackServer”); 
 $ pid = ob_get_clean(); 
var_dump($ pid)  ; 
exit; 
   
 
 

目标是在其路径中运行TrackServer并获取其进程ID,以便我在进行一些测试后关闭它。

当我在终端中运行命令时:

 (cd / opt / server / TrackServer / release&& nohup ./TrackServer&)  &安培;&安培;  pidof TrackServer 
   
 
 

我得到正确的pid返回但是在php中命令停止并且不再进一步,TrackServer启动并运行但是我必须从 终端解锁php脚本,在杀死进程后,php脚本为我刚从终端关闭的进程打印正确的pid。

  1. 为什么 命令停止?

  2. 有没有办法让它在php中运行,就像我试图运行它一样(不需要换新线程)?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题