linux进程监控源码
‘壹’ linux进程转换状态函数源代码怎样获取
可以使用google代码搜索,http://code.google.com/intl/zh-CN/
帮你找了一下下,
http://www.google.cn/codesearch/p?hl=zh-CN#7_68JW9_sJ8/linux/kernel/fork.c&q=sys_exit()
进去找就有了
‘贰’ Linux 简单的守护进程 程序源代码是什么怎么写
//根据麦子学院视频改编。。。。 #include #include #include #include #include #include #include int main(int argc,char**argv) { int num=0; //sundy print log char content[128]=""; //1,创建一个子进程,退出父进程 pid_t pid=fork(); if(pid0) { exit(0); } //2,设置新的会话 pid_t ret=setsid(); if(ret0) { sprintf(content, "sundy print log = %d\n", num); write(fd,content,strlen(content)); close(fd); } num++; sleep(3); } return 0; }
‘叁’ 如何在Linux下用c语言创建守护进程并监控系统运行期间的所有进程
可以分三步来做:
- 做两个简单的守护进程,并能正常运行
- 监控进程是否在运行
- 启动进程
综合起来就可以了,代码如下:
被监控进程thisisatest.c(来自):
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>
void init_daemon()
{
int pid;
int i;
pid=fork();
if(pid<0)
exit(1); //创建错误,退出
else if(pid>0) //父进程退出
exit(0);
setsid(); //使子进程成为组长
pid=fork();
if(pid>0)
exit(0); //再次退出,使进程不是组长,这样进程就不会打开控制终端
else if(pid<0)
exit(1);
//关闭进程打开的文件句柄
for(i=0;i<NOFILE;i++)
close(i);
chdir("/root/test"); //改变目录
umask(0);//重设文件创建的掩码
return;
}
void main()
{
FILE *fp;
time_t t;
init_daemon();
while(1)
{
sleep(60); //等待一分钟再写入
fp=fopen("testfork2.log","a");
if(fp>=0)
{
time(&t);
fprintf(fp,"current time is:%s ",asctime(localtime(&t))); //转换为本地时间输出
fclose(fp);
}
}
return;
}
监控进程monitor.c:
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<limits.h>
#define BUFSZ 150
void init_daemon()
{
int pid;
int i;
pid=fork();
if(pid<0)
exit(1); //创建错误,退出
else if(pid>0) //父进程退出
exit(0);
setsid(); //使子进程成为组长
pid=fork();
if(pid>0)
exit(0); //再次退出,使进程不是组长,这样进程就不会打开控制终端
else if(pid<0)
exit(1);
//关闭进程打开的文件句柄
for(i=0;i<NOFILE;i++)
close(i);
chdir("/root/test"); //改变目录
umask(0);//重设文件创建的掩码
return;
}
void err_quit(char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
// 判断程序是否在运行
int does_service_work()
{
FILE* fp;
int count;
char buf[BUFSZ];
char command[150];
sprintf(command, "ps -ef | grep thisisatest | grep -v grep | wc -l" );
if((fp = popen(command,"r")) == NULL)
err_quit("popen");
if( (fgets(buf,BUFSZ,fp))!= NULL )
{
count = atoi(buf);
}
pclose(fp);
return count;
// exit(EXIT_SUCCESS);
}
void main()
{
FILE *fp;
time_t t;
int count;
init_daemon();
while(1)
{
sleep(10); //等待一分钟再写入
fp=fopen("testfork3.log","a");
if(fp>=0)
{
count = does_service_work();
time(&t);
if(count>0)
fprintf(fp,"current time is:%s and the process exists, the count is %d ",asctime(localtime(&t)), count); //转换为本地时间输出
else
{
fprintf(fp,"current time is:%s and the process does not exist, restart it! ",asctime(localtime(&t))); //转换为本地时间输出
system("/home/user/daemon/thisisatest"); //启动服务
}
fclose(fp);
}
}
return;
}
具体CMD命令:
cc thisisatest.c -o thisisatest
./thisisatest
cc monitor.c -o monitor
./monitor
tail -f testfork3.log -- 查看日志
‘肆’ linux下如何监听进程
一、supervise
Supervise是daemontools的一个工具,可以用来监控管理unix下的应用程序运行情况,在应用程序出现异常时,supervise可以重新启动指定程序。
使用:
mkdir test
cd test
vim run 写入希望执行的操作
supervise test (注意这里是的参数是run文件上层的文件夹,改变run的为可执行 chmod +x run)
二、monit
monit是一个小型的开放源码工具来管理和监控Unix系统。Monit可以自动维护进程,及时避免进程异常退出等产生的问题。
系统: monit可以监控问题的发生,包括进程状态、系统cpu负载、内存占用情况等,例如当apache服务的cpu负载以及内存闸弄情况过高时候,它会重启apache服务。
进程: monit可以监控守护进程,包括系统进程。例如当某个进行down掉,它会自动恢复重启该进程。
文件系统:Monit可以监控本地文件、目录、文件系统的变化,包括时间戳、校验值、大小的变化。例如,可以监控文件sha1以及md5的值,来监控文件是否发生变化。
网络:monit可以监控网络连接,支持TCP、UDP、Unix domain sockets以及HTTP、SMTP等。
定时脚本:monit可以用来定时测试程序和脚本,获取程序输出结果,进而判断是否成功或其他情况。
安装:
sudo apt-get install monit
编辑配置:
sudo vim /etc/monit/monitrc
启动、停止、重启:
sudo /etc/init.d/monit start
sudo /etc/init.d/monit stop
sudo /etc/init.d/monit restart
设置页面监控状态:
set httpd port 2812 and
allow 0.0.0.0/0.0.0.0
allow localhost
增加监控:
需要注意的是,这里需要添加start和stop,缺一个都是不行的
1.根据程序名称来监控
check process test with MATCHING test.py
start program = "/home/yxd/test.py"
stop program = "xxxxx"
2.根据pid监控
check process apache with pidfile /var/run/httpd.pid
start program = "/etc/init.d/rcWebServer.sh start https"
stop program = "/etc/init.d/rcWebServer.sh stop https"
if changed pid then aler
参考:用monit监控系统关键进程
supervisord
Supervisor是一个C/S系统,它可以在类unix操作系统让用户来监视和控制后台服务进程的数量。它是由python编写的,常用于进程异常退出的重启保护。
安装:
pip install supervisor
查看配置文件:
echo_supervisord_conf
从该命令的结果中,可以看到各个模块的配置信息。
创建配置文件:
echo_supervisord_conf > /etc/supervisord.conf
配置应用:
[program:test]
command=python /root/test_supervisor.py
process_name=%(program_name)s
stdout_logfile=/root/test.log
stderr_logfile=/root/test.log
保存,启动:
/usr/bin/supervisord -c /etc/supervisord.conf
‘伍’ 如何在Linux下用c语言创建守护进程并监控系统运行期间的所有进程
可以分三步来做:
做两个简单的守护进程,并能正常运行
监控进程是否在运行
启动进程
综合起来就可以了,代码如下:
被监控进程thisisatest.c
#include<unistd.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/param.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<time.h>
void init_daemon()