Linux下进程管理工具

50 篇文章 3 订阅

在Linux下我们可以会开启一些后台的php进程来处理一些任务,因此我们需要来管理这些进程,启动,关闭,重启。

方法1:写一个php程序来监控他们,染乎将这个程序后台运行
check_process.php

<?php
// 监控进程并重启

ini_set('date.timezone','Asia/Shanghai');

class checkProcess{
	protected $dir = '';
	protected $deadtime = 0;
	public function __construct($dirname){
		$this->dir = $dirname;
	}
	//检查是否在执行
	public function check_php($filename){
		$fullname = $this->dir . "/" . $filename;
		// ps -ax|grep /mydata/data/mainscript/script_api/script_game_record.php|grep -v grep|wc -l
		$str = "ps -ef | grep " . $fullname . '|grep -v grep|wc -l'; //查看符合条件的进程数量,排除grep进程本身
		$tmp = exec($str);
		//不在运行
		if ( $tmp < 1 ){
			$this->start_php($fullname);
			// 写记录
			$logstr = "echo '[ ".date('Y-m-d H:i:s')." ] [ ".$filename." ] has been restarted ...' >> " . $this->dir . "/restart_log.log";
			exec($logstr);
			return true;
		}
	}
	public function start_php($fullname){
		$cmd = "nohup php " . $fullname . ' > /dev/null 2>&1 &';
		exec($cmd);
	}
}
// ----------------------------------------------------------------------------------------------
$file_arr = [
	'script_coin_count.php',
	'script_game_record.php',
	'script_activity.php',
	'script_other.php',
	'script_rewite_game_record.php',
];
$dirname = '/mydata/data//async_script';
$handller = new checkProcess($dirname);
while(true){
	if(!empty($file_arr)){
		foreach($file_arr as $val){
			$handller->check_php($val);
		}
	}
	sleep(60);
}

方法2:使用Supervisor程序
Supervisor 是一个 Linux 下的进程管理器,需要安装

文档:http://supervisord.org/index.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值