C++学习笔记————watchdog守护进程自动重启
最近写完一个服务端后,希望能在启动后如果程序异常退出了能够自动重启。
经过查询,自己手动实现看门狗进程自动重启一般有两个方法:
一.脚本重启:
#!/bin/sh
while true
do
ps -ef | grep "test(程序名)" | grep -v "grep"
if ["$?" -eq 0]
then
./test
echo "wath process has been restarted! "
else
echo "watch process already started ! "
fi
sleep 1
done
开启脚本使其不断轮询查看是否有我们的服务进程,没有则在命令行中将服务运行一次。
二.看门狗进程:
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#define ture 1
#define false -1
void *ServerFunc(void *);
void ChildForkReload(int);
int