php tp5 queue linux 部署 开启服务及关闭服务
tp5的queue服务安装,请参考composer 安装的方法!这里不再累述。本文主要是针对开发【开启服务】和【关闭服务】
本文仅支持在linux上使用。
根据linux的脚本实例,如:
kill -9 11340;
kill -9 8825;
linux多命令的执行使用英文;隔开。这个就为关闭多个queue服务提供了条件。
设计思路:
一、创建进程,写入进程号【开启服务】
队列: OrderUnPay ---对未支付的订单进行延时作废。
脚本命令如下:
"cd /www/wwwroot/saas.com/; php think queue:work --queue OrderUnPay --daemon > /dev/null 2> /dev/null & echo true"
php exec 执行以上linux脚本后。
使用命令
ps -aux | grep queue
可以看到里面的15865进程ID
那么如何才能将这个进程ID 15862输出,并写入数据库,方便后面的【关闭服务】
ps -aux|grep OrderUnPay |awk '{print $2}' | head -1
可以使用以上这个命令,通过关键词搜索,并倒序拿出当前的结果
以下php代码,进程ID就可以通过$out[0]
$pid_sq="ps -aux|grep OrderUnPay |awk '{print $2}' | head -1";
$process=exec($pid_sq,$out);
$process_id=isset($out[0])?$out[0]:0;
二.使用多个kill -9 制作【关闭服务】
kill -9 15862;
kill -9 ....;
以下是我tp下的【单个开启服务】和【关闭全部服务】功能
#关闭服务
public function closed(){
$out=array();
$message='关闭服务失败!';
$output='';
if(strpos(PHP_OS,"Linux")!==false){
#Linux套字
$map['process_id']=array('gt',0);
$process_ids=$this->model->where($map)->column('process_id');
if(!empty($process_ids)){
$exec_power ='';
foreach($process_ids as $k=>$v){
$exec_power .="kill -9 ".$v." ;";
}
$exec_power .=" echo true";
#exit($exec_power);
$message = exec($exec_power,$output, $ret);
}else{
$message ='没有进程数据';
}
#$exec_power ="kill -s 9 `ps -aux | grep queue| grep -v grep | awk '{print $2}'` & echo true";
}else if(strpos(PHP_OS,"WIN")!==false){
#Win套字
$exec_power ='cd /d 2>&1'. ROOT_PATH .' & php think queue:restart';
$message = exec($exec_power,$output, $ret);
}else{
$this->error('未识别的操作系统');
}
#命令执行
#$message = shell_exec($exec_power);
if(count($output)>0){
foreach($output as $k=>$v){
$info=iconv('GB2312', 'UTF-8',$v);
$out[$k]=$info;
$message.=$info;
}
}
#返回结果
if(!$ret){
$this->model->where(array('status'=>1))->update(['work_status'=>0,'process_id'=>0]);
$this->success('关闭服务成功','',$exec_power);
}else{
$this->error($message,'');
}
}
#启动服务
public function powers(){
$id =$this->request->param('id');
$info=$this->model->where(array('id'=>$id))->find();
$daemon =1;
#获取标识
$job_code =$info['job_code'];
if(!empty($job_code)){
#判断win还是linux
if(strpos(PHP_OS,"Linux")!==false){
#Linux套字
if($daemon){
$exec_power ='cd '. ROOT_PATH .'; php think queue:work --queue ' .$job_code.' --daemon > /dev/null 2> /dev/null & echo true';
}else{
$exec_power ='cd '. ROOT_PATH .'; php think queue:work --queue ' .$job_code.' & echo true';
}
}else if(strpos(PHP_OS,"WIN")!==false){
#Win套字
if($daemon){
$exec_power ='cd /d '. ROOT_PATH .' & start /B php think queue:work --queue ' .$job_code.' --daemon'.' & echo true';
}else{
$exec_power ='cd /d "'. ROOT_PATH .'" & php think queue:work --queue ' .$job_code.' & echo true';
}
}else{
$this->error('未识别的操作系统');
}
#命令执行
$message ='';
$process_id='';
if($daemon>0){
if(strpos(PHP_OS,"WIN")!==false){
#打开一个进程
$win_pid =popen($exec_power, 'r');
$ret =fgets($win_pid);
pclose($win_pid); #对应的是int(1)
}else{
#执行命令
$ret = shell_exec($exec_power);
#查看进程
$pid_sq ="ps -aux|grep $job_code |awk '{print $2}' | head -1";
$process=exec($pid_sq,$out);
$process_id=isset($out[0])?$out[0]:0;
}
}else{
$ret = shell_exec($exec_power);
}
#获取进程ID
$process_id=$process_id>0?$process_id:'';
if($ret){
$this->model->where(array('id'=>$id))->update(['work_status'=>1,'process_id'=>$process_id]);
$this->success('启动成功','',$exec_power);
}else{
$this->error('启动失败','',$exec_power);
}
}else{
$this->error('未找到对应的任务标识');
}
}