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()