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