用Linux守护进程检测某个程序是否运行2
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
环境:
主机:Fedora12
目标板:SC6410
目标板LINUX内核版本:2.6.36
说明:
第一版程序(参考http://blog.csdn.net/jdh99/article/details/7300641)已经可以正常工作,不过在运行一个月后发现有两台平板出现不能启动的问题,检查后发现是nand flash坏块很多,导致系统不能启动。究其原因,因该是对flash频繁的写操作造成的。上一版本的守护程序每分钟会写操作一次,这样一天的写操作就达千次。在这一版本中,判断需要守护的进程是否存在,是通过读取/proc/pid目录来判断的。参考链接:http://kb.cnblogs.com/a/2360817/
驱动源代码:
daemon_service.c:
//守护进程,守护AlarmInterface进程
//作者:jdh
//时间:2012-4-27
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syslog.h>
#include <dirent.h>
//程序名字
#define NAME "AlarmInterface"
//要运行的程序
#define RUN_NAME "DuiJiang &"
#define READ_BUF_SIZE 1024
int daemon(int nochdir,int noclose)
{
pid_t pid;
//让init进程成为新产生进程的父进程
pid = fork();
//如果创建进程失败
if (pid < 0)
{
perror("fork");
return -1;
}
//父进程退出运行
if (pid != 0)
{
exit(0);
}
//创建新的会话
pid = setsid();
if (pid < -1)
{
perror("set sid");
return -1;
}
//更改当前工作目录,将工作目录修改成根目录
if (!nochdir)
{
chdir("/");
}
//关闭文件描述符,并重定向标准输入,输出合错误输出
//将标准输入输出重定向到空设备
if (!noclose)
{
int fd;
fd = open("/dev/null",O_RDWR,0);
if (fd != -1)
{
dup2(fd,STDIN_FILENO);
dup2(fd,STDOUT_FILENO);
dup2(fd,STDERR_FILENO);
if (fd > 2)
{
close(fd);
}
}
}
//设置守护进程的文件权限创建掩码
umask(0027);
return 0;
}
//存在返回1,不存在返回0
int judge_pid_exist(char* pidName)
{
DIR *dir;
struct dirent *next;
int i = 0;
FILE *status;
char buffer[READ_BUF_SIZE];
char name[READ_BUF_SIZE];
///proc中包括当前的进程信息,读取该目录
dir = opendir("/proc");
if (!dir)
{
printf("Cannot open /proc\n");
return 0;
}
//遍历
while ((next = readdir(dir)) != NULL)
{
//跳过"."和".."两个文件名
if ((strcmp(next->d_name, "..") == 0) || (strcmp(next->d_name, ".") == 0))
{
continue;
}
//如果文件名不是数字则跳过
if (!isdigit(*next->d_name))
{
continue;
}
//判断是否能打开状态文件
sprintf(buffer,"/proc/%s/status",next->d_name);
if (!(status = fopen(buffer,"r")))
{
continue;
}
//读取状态文件
if (fgets(buffer,READ_BUF_SIZE,status) == NULL)
{
fclose(status);
continue;
}
fclose(status);
//读取PID对应的程序名,格式为Name: 程序名
sscanf(buffer,"%*s %s",name);
//判断程序名是否符合预期
if (strcmp(name,pidName) == 0)
{
//符合
closedir(dir);
return 1;
}
}
closedir(dir);
return 0;
}
int main(int argc,char *argv[])
{
int fd = 0;
char buf[100];
//开启守护进程
daemon(0,0);
while (1)
{
//打开日志
openlog(argv[0],LOG_CONS|LOG_PID,LOG_USER);
//判断是否有程序文件运行
if (judge_pid_exist(NAME))
{
syslog(LOG_INFO,"jdh success:)!!!!!!!!!!");
//printf("hello,jdh,exist\n");
}
else
{
syslog(LOG_INFO,"jdh fail:(!!!!!!!!!!");
//运行程序
system(RUN_NAME);
//printf("hello,jdh,oh,no\n");
}
//休眠
sleep(60);
}
//关闭日志
closelog();
return 0;
}
分享到:
相关推荐
linux c++ 守护线程,判断程序是否运行,不存在就启动
Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux...
linux守护进程程序模板带详细注释 守护进程日志 不能被多次运行 详细解释:https://i-beta.cnblogs.com/posts/edit;postId=12095412
Linux下 tomcat的守护进程、心跳程序 自行重启TOmcat服务
编写Linux守护进程
LINUX守护进程介绍 amd:自动安装NFS(网络文件系统)守侯进程 apmd:高级电源管理 Arpwatch:记录日志并构建一个在LAN接口上看到的以太网地址和IP地址对数据库 Autofs:自动安装管理进程automount,与NFS相关,...
编写Linux守护进程.pdf
Linux守护进程的编程方法 出处未知 pdf格式
该文档说明在linux下实现守护进程,代码简单易懂,思路清楚。
Linux守护进程 Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程Linux守护进程
守护进程,每隔一段时间检测程序运行状态。
Linux中将Tomcat添加到守护进程,让Tomcat随Linux系统自动启动;以及解决执行tomcat start时,“Invalid user name 'tomcat' specified”的异常处理。
linux守护进程,ubunt守护进程
以守护进程方式运行的信息服务器,含服务端与客户端 以守护进程方式运行的信息服务器,含服务端与客户端
运用crontab检测程序是否在运行,可通过检测进程或检测端口,如果程序没在运行,可调用指定的命令
Linux守护进程的研究Linux守护进程的研究Linux守护进程的研究Linux守护进程的研究
Linux 守护进程的编程方法,几个源码实例
Linux系统下守护进程编程方法 参考文章
红帽企业Linux 4 Update 2改进了对审核子系统的内核和用户支持。审核子系统可以被系统管理员用来监测...系统管理员还可以使用auditctl工具程序来修改 auditd守护进程运行时的审核参数、syscall规则和文件系统的查看。