1、新建php脚本文件
class Server
{
const PORT = 8814;
public function port(){
$shell = "netstat -anp 2>/dev/null | grep ".self::PORT." | grep LISTEN |wc -l";
$result = shell_exec($shell);
if($result != 1){
//发送报警服务(邮件/短信)
echo date('Y-m-d H:i:s').'--error'.PHP_EOL;
}else{
echo date('Y-m-d H:i:s').'--success'.PHP_EOL;
}
}
}
//定时请求(每2秒请求一次)
swoole_timer_tick(2000,function ($timer_id){
(new Server())->port();
});
2、将脚本放入进程中运行
nohup php安装目录 php脚本文件目录 > /www/wwwroot/swoole.demo/tp5/script/bin/monitor/demo.txt
ps:这里要用绝对路径
php指安装目录下(例:/www/server/php/74/bin/php)
Server.php 指脚本目录(例:/www/wwwroot/swoole.demo/monitor/Server.php)