监控进程(二进制程序)运行状态的C语言实现与脚本实现
来源:互联网 发布:泰豪软件学院 编辑:程序博客网 时间:2024/06/04 18:06
项目需要,要监控二进制程序是否在运行状态,如果没有在运行状态,则检查是否存在升级文件、升级文件是否可用、是否存在备份文件、备份文件是否需要删除等等功能……
注意这里会使用file命令检查文件是否是execute,如果程序是可执行脚本,是无法监测的。
先放C语言实现方式,代码简单就没写备注
#include "stdint.h"#include "stdio.h"#include "stdlib.h"#include "string.h"#include "unistd.h"#define fileName "/opt/hello"#define fileNameOld fileName"old"#define fileNameNew fileName"new"int main(int argc, char *argv[]){ FILE * stream; char buf[1024]; uint32_t i; int ret; uint8_t bValidOld = 0; uint8_t bValid = 0; uint8_t bValidNew = 0; if(argc > 1) { if(argv[1][0] == 'v') { printf("20160122\n"); return 0; } } printf("app deamon running\n"); while(1) { memset(buf,0,sizeof(buf)); stream = popen( "ps -ef | grep "fileName" | grep -v grep | wc -l", "r" ); fread( buf, sizeof(char), sizeof(buf), stream); pclose(stream); for(i=0;i<sizeof(buf);i++) { if((buf[i] <= '0') || ('9' >= buf[i])) { buf[i] = 0; break; } } ret = atoi(buf); printf("ret = %d\n",ret); if(ret) { sleep(2); continue; } printf("dest app isn't running , now check it\n"); bValidOld = 0; bValid = 0; bValidNew = 0; if(0 == access(fileNameNew,F_OK)) { printf("found "fileNameNew); system("chmod +x "fileNameNew); memset(buf,0,sizeof(buf)); stream = popen( "file "fileNameNew, "r"); fread( buf, sizeof(char), sizeof(buf), stream); pclose(stream); if(strstr(buf,"executable")) { printf(" and it is executable \n"); bValidNew = 1; } else { printf(" but it isn't executable , remove it\n"); remove(fileNameNew); } } if(0 == access(fileName,F_OK)) { printf("found "fileName); system("chmod +x "fileName); memset(buf,0,sizeof(buf)); stream = popen( "file "fileName, "r"); fread( buf, sizeof(char), sizeof(buf), stream); pclose(stream); if(strstr(buf,"executable")) { printf(" and it is executable \n"); bValid = 1; } else { printf(" but it isn't executable , remove it\n"); remove(fileName); } } if(0 == access(fileNameOld,F_OK)) { printf("found "fileNameOld); system("chmod +x "fileNameOld); memset(buf,0,sizeof(buf)); stream = popen( "file "fileNameOld, "r"); fread( buf, sizeof(char), sizeof(buf), stream); pclose(stream); if(strstr(buf,"executable")) { printf(" and it is executable \n"); bValidOld = 1; } else { printf(" but it isn't executable , remove it\n"); remove(fileNameOld); } } if(bValidNew) { if(bValid) { if(bValidOld) { printf("remove old\n"); remove(fileNameOld); } printf("rename current to old\n"); rename(fileName,fileNameOld); } printf("rename new to current\n"); rename(fileNameNew,fileName); } else { if(bValid) { if(bValidOld) { printf("current is valid , remove old\n"); remove(fileNameOld); } } else { if(bValidOld) { printf("rename old to current\n"); rename(fileNameOld,fileName); } } } system("sync"); system(fileName); sleep(2); } return 0;}
下面是sh脚本实现,基本上就是把C语言版本做了个“翻译”
#!/bin/shworkd=/opt/fileName=hellofileNameOld=$fileName"old"fileNameNew=$fileName"new"bFileValid=0bFileOldValid=0bFileNewValid=0#检查文件是否可执行checkFileExecutable(){ #echo "check file $1 executable" local ret=`file $1 | grep executable | grep -v grep | wc -l` return $ret;}#检查文件是否有效,先检查是否存在,再检查是否可执行chechFileValid(){ local valid if [ -f $1 ]; then #echo "file $1 exist" checkFileExecutable $1 valid=$? if [ $valid -eq 1 ] ; then #echo "file $1 is executable" valid=1 else #echo "file $1 is note executable, remove it" rm $1 fi else #echo "file $1 not exist" valid=0; fi return $valid;}chechFileValid $fileNamebFileValid=$?chechFileValid $fileNameNewbFileNewValid=$?chechFileValid $fileNameOldbFileOldValid=$?#echo $bFileValid $bFileNewValid $bFileOldValidwhile [ true ] ; do processExist=`ps -ef | grep $fileName | grep -v grep | wc -l` if [ $processExist -gt 0 ] ; then sleep 2; else chechFileValid $fileName bFileValid=$? chechFileValid $fileNameNew bFileNewValid=$? chechFileValid $fileNameOld bFileOldValid=$? if [ $bFileNewValid -eq 1 ] ; then #echo "new file valid" if [ $bFileValid -eq 1 ] ; then if [ $bFileOldValid -eq 1 ] ; then #echo "file old found, remove it" rm $fileNameOld fi #echo "current file valid ,rename it to old" mv $fileName $fileNameOld fi #echo "new file valid ,rename it to current" mv $fileNameNew $fileName else if [ $bFileValid -eq 1 ] ; then if [ $bFileOldValid -eq 1 ] ; then #echo "file old found, remove it" rm $fileNameOld fi else #echo "rename old to current" mv $fileNameOld $fileName fi fi sync $workd$fileName sleep 1; fidone;
0 0
- 监控进程(二进制程序)运行状态的C语言实现与脚本实现
- Java 实现程序运行状态监控 之我的思路(守护进程)
- shell脚本实现某一进程是否运行结束的监控
- Java实现对程序运行状态的监控
- C语言实现二进制与十进制转换
- 进程监控程序的开发(程序运行监控)
- 二进制算术编码器的C语言实现
- 进程监控脚本程序
- 顺序栈的c语言实现(程序可运行)。。。2014.5.11
- cpu运行状态监控脚本
- C语言实现的一个程序只能运行一次,不能重复运行
- 创建Service程序并监控有界面的进程实现
- C语言大型程序的项目管理与实现
- C语言简单校园导游程序的设计与实现
- C语言大型程序的项目管理与实现
- 服务进程自启动脚本,实现每分钟检查服务运行状态
- 利用PHP实现对服务器性能与状态的监控
- c语言进程池的简单实现
- Linux内核之内核锁
- 制表符(tab 键)的概念
- Quartz job学习
- 【HDU】 2818 Building Block
- SGU 326 Perspective (网络流)
- 监控进程(二进制程序)运行状态的C语言实现与脚本实现
- UIGraphicsBeginImageContext系列知识
- Android intent返回数据给上一个活动
- AVL树
- JAVA多线程
- LeetCode-3.Longest Substring Without Repeating Characters
- 欢迎使用CSDN-markdown编辑器
- 日本 西暦と和暦の変換
- mysqladmin命令修改Mysql密码