如何监控linux服务器被重启

如何监控linux服务器被重启,第1张

[monitor@i-755-42433-VM ~]$ cat admintest.sh 

#! /bin/sh  

  

APP_MAIN=com.upyoo.agent.AgentServer  

proc_num()                      #查询进程数量

{

    num=`ps -ef|grep $APP_MAIN | awk '{ if($8=="./jre/bin/java") print $2}'`

    return $num

}

proc_num  

number=$?                       #获取进程数量

if [ $number -eq 0 ] 悉喊           #如果进程数量为0

then    睁滑野                     让塌   #重新启动服务器,或者扩展其它内容。

  cd /home/monitor/alert-agent/  

  sudo bash agentadmin.sh 3

fi

APP_MAIN是进程名

里面是执行

then下面是绝对路径执行

可以分三步来做:

做两个简竖族单的守护进程,并能正常运行

监控进程是否在运行

启动进程

综合起来就可以了,代码如下:

被监控进程thisisatest.c(来自http://www.cnblogs.com/ringwang/p/3528093.html):

#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=0i<NOFILEi++)

  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\n",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=0i<NOFILEi++)

  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\n",asctime(localtime(&t)), count) //转换为本地时间输出

          else

          {

              fprintf(fp,"current time is:%s and the process does not exist, restart it!\n",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   -- 查看日志

ps -fe|grep X if [ $? -eq 0 ] then service restart X fi 然后把这段老银脚本放到crontab里面,侍笑宴每分钟执行一次升灶就可以了


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12212099.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存