监控进程(二进制程序)运行状态的C语言实现与脚本实现

项目需要,要监控二进制程序是否在运行状态,如果没有在运行状态,则检查是否存在升级文件、升级文件是否可用、是否存在备份文件、备份文件是否需要删除等等功能……
注意这里会使用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/sh

workd=/opt/
fileName=hello
fileNameOld=$fileName"old"
fileNameNew=$fileName"new"

bFileValid=0
bFileOldValid=0
bFileNewValid=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 $fileName
bFileValid=$?
chechFileValid $fileNameNew
bFileNewValid=$?
chechFileValid $fileNameOld
bFileOldValid=$?

#echo $bFileValid $bFileNewValid $bFileOldValid

while [ 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;
    fi
done;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值