Linux 下实现进程退出后自动重启
2016-03-03 18:32
501 查看
Linux 下实现进程退出后自动重启
一般方案有2
1.采用脚本,网上很多,但对一些嵌入式系统因资源限制,大部分命令裁剪了,故不可行
2.采用fork,父进程等待子进程退出
一下采用fork 方法实现,对于重启多个进程,
可采用(pid = waitpid( -1, &status, WNOHANG))避免多个进程同时重启导致僵尸进程
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string>
using namespace std;
static const char* FULL_NAME= "/usr/bin/server";
static const char* PROGRAM = "server";
static void StartProcess(std::string &name, std::string ¶m)
{
int i = 2;
char tmp[200], *argv[32];
const char *delim=" ";
memcpy(tmp, param.c_str(), 200);
argv[0] = ( char * ) name.c_str();
argv[1] = strtok( ( char * ) tmp, delim);
while ( (argv[i++] = strtok(NULL, delim) ));
argv[i] = NULL;
int status = 0;
string cmd = "killall ";
cmd += PROGRAM;
while (true)
{
int r = system(cmd.c_str());
if (r!=0)
{
printf("WatchDog system err=%d:%s\n",errno,strerror(errno));
}
pid_t pid = fork();
if (pid < 0)
{
printf("WatchDog fork err=%d:%s\n",errno,strerror(errno));
sleep(5);
continue;
}
if (pid == 0)
{
printf("[WatchDog] StartProcess %s ok!\n",name.c_str());
execvp(argv[0], argv);
exit( -1);
}
printf("[WatchDog] chirend pid=%d ok!\n",pid);
if (pid>0)
{
pid_t exitPid = wait(&status);
printf("[WatchDog] pid=%d exitPid=%d!\n",pid,exitPid);
sleep(5);
}
}
}
int main(int argc,char** argv)
{
string name = FULL_NAME;
string param;
StartProcess(name,param);
return 0;
}
Linux 下实现进程退出后自动重启
一般方案有2
1.采用脚本,网上很多,但对一些嵌入式系统因资源限制,大部分命令裁剪了,故不可行
2.采用fork,父进程等待子进程退出
一下采用fork 方法实现,对于重启多个进程,
可采用(pid = waitpid( -1, &status, WNOHANG))避免多个进程同时重启导致僵尸进程
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string>
using namespace std;
static const char* FULL_NAME= "/usr/bin/server";
static const char* PROGRAM = "server";
static void StartProcess(std::string &name, std::string ¶m)
{
int i = 2;
char tmp[200], *argv[32];
const char *delim=" ";
memcpy(tmp, param.c_str(), 200);
argv[0] = ( char * ) name.c_str();
argv[1] = strtok( ( char * ) tmp, delim);
while ( (argv[i++] = strtok(NULL, delim) ));
argv[i] = NULL;
int status = 0;
string cmd = "killall ";
cmd += PROGRAM;
while (true)
{
int r = system(cmd.c_str());
if (r!=0)
{
printf("WatchDog system err=%d:%s\n",errno,strerror(errno));
}
pid_t pid = fork();
if (pid < 0)
{
printf("WatchDog fork err=%d:%s\n",errno,strerror(errno));
sleep(5);
continue;
}
if (pid == 0)
{
printf("[WatchDog] StartProcess %s ok!\n",name.c_str());
execvp(argv[0], argv);
exit( -1);
}
printf("[WatchDog] chirend pid=%d ok!\n",pid);
if (pid>0)
{
pid_t exitPid = wait(&status);
printf("[WatchDog] pid=%d exitPid=%d!\n",pid,exitPid);
sleep(5);
}
}
}
int main(int argc,char** argv)
{
string name = FULL_NAME;
string param;
StartProcess(name,param);
return 0;
}
相关文章推荐
- Linux socket 初步
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程
- Linux 下无损图片压缩小工具介绍