php中exec函数超时_PHP: exec - Manual

本文介绍了一个专为Linux用户设计的PHP类,用于方便地管理进程,包括启动、停止、状态检查。通过nohup命令结合自定义类,可以轻松获取和控制运行中的进程ID,适用于需要对进程进行一定程度控制的场景。
摘要由CSDN通过智能技术生成

(This is for linux users only).

We know now how we can fork a process in linux with the & operator.

And by using command: nohup MY_COMMAND > /dev/null 2>&1 & echo $! we can return the pid of the process.

This small class is made so you can keep in track of your created processes ( meaning start/stop/status ).

You may use it to start a process or join an exisiting PID process.

if ($process.status()){

echo"The process is currently running";

}else{

echo"The process is not running.";

}?>

* Ever wanted to execute a process in php, but you still wanted to have somewhat controll of the process ? Well.. This is a way of doing it.

* @compability: Linux only. (Windows does not work).

* @author: Peec

*/classProcess{

private$pid;

private$command;

public function__construct($cl=false){

if ($cl!=false){$this->command=$cl;$this->runCom();

}

}

private functionrunCom(){$command='nohup '.$this->command.' > /dev/null 2>&1 & echo $!';exec($command,$op);$this->pid= (int)$op[0];

}

public functionsetPid($pid){$this->pid=$pid;

}

public functiongetPid(){

return$this->pid;

}

public functionstatus(){$command='ps -p '.$this->pid;exec($command,$op);

if (!isset($op[1]))returnfalse;

else returntrue;

}

public functionstart(){

if ($this->command!='')$this->runCom();

else returntrue;

}

public functionstop(){$command='kill '.$this->pid;exec($command);

if ($this->status() ==false)returntrue;

else returnfalse;

}

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值