监控多个进程的脚本
‘壹’ 帮忙写一个VBS或者BAT监控进程的脚本!!
假如要监控a.exe,要关的是b.exe和c.exe,每隔5秒监控一次:
on error resume next
dim a,b,c,i,fs,f
set fs=createobject("scripting.systemfileobject")
set a=getobject("winmgmts:\\.\root\cimv2")
do
set b=a.execquery("select * from win32_process where name='a.exe'")
set c=a.execquery("select * from win32_process where name='b.exe' or name='c.exe'")
if b.count=0 then
for each i in c
i.terminate()
next
set f=fs.getfile(wscript.scriptfullname)
f.delete
wscript.quit(0)
else
wscript.sleep 5000
end if
loop
测试一下
‘贰’ linux 监控多进程脚本
如果 abcd路径不一样就很难弄!
#!/bin/bash
for i in a b c d;do
var=`ps -e|awk '$4=='"$i"''`
var1=`echo $var|awk '{print $4}'`
if [ ! $var1==$i ];then /var/$i;fi
done
‘叁’ 哪位大侠有linux下监控多个进程是否死掉。如果死掉就kill掉重启,没有该进程的话也重启的shell脚本。
我以监控一个进程:“vmstat”命令的进程为例子。
#!/bin/bash
PROSS=`ps -el | grep vmstat | awk '{ print $14 }'`
STAT=`ps -el | grep vmstat | awk '{ print $2 }'`
PID=`ps -el | grep vmstat | awk '{ print $4 }'`
if [ $PROSS ]
then
if [ $STAT = "Z" ]
then
echo "$PROSS has dead"
echo "Now, killing it"
kill -9 $PID
echo "Now, restart"
vmstat 2 5 >> /tmp/vmstat.txt &
else
echo "$PROSS is running healthy"
fi
else
echo "The programm has dropped"
echo "Now, restart"
vmstat 2 5 >> /tmp/vmstat.txt &
fi
‘肆’ 如何在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 -- 查看日志
‘伍’ 请帮小弟编写进程监控脚本,可以用来监控某个进程是否关闭
:RESTART
tasklist /FI "username eq administrator" | find /C "sparcs.exe" > temp.txt
set /p num= < temp.txt
del /F temp.txt
echo %num%
if "%num%" == "0" start /D "C:\" sparcs.exe
ping -n 10 -w 2000 0.0.0.1 > temp.txt
del /F temp.txt
goto RESTART
参考的别人的,试试看是不是可以
‘陆’ vbs监控进程并调用程序
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "Calculator"
AppActivate 方法返回的布尔值表示过程调用是否成功。例如,可用下列方法判断:
if (WshShell.AppActivate "Calculator") then
msgbox "Calculator running"
else
msgbox "Calculator not running"
end if
根据调用的方法、函数的返回值作下一步的操作。具体的参考可看“Script56.CHM”。
****************************************
对不起,疏忽了,我再次查帮助:
1、前面的例子漏了一句:
WshShell.Run "calc"
AppActivate 方法返回的布尔值表示过程调用是否成功。该方法将焦点转移到命名应用程序或窗口中。
------------------------------------------
此方法并不能监视启动的程序是否还在运行。
并且我也不知道为何在脚本运行与帮助中所说的不一致。但我试了几种方法,脚本大多不能监控所调用的外部程序的状态。
2、脚本中用Run方法调用外部程序,可以等待程序执行完成,并查看其返回值:
object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])
bWaitOnReturn :
可选。布尔值,表示在继续执行脚本中的下一条语句之前,脚本是否等待执行完程序。如果设为 true,则在执行完程序后才执行脚本,Run 方法返回由程序返回的任何错误代码。如果设为 false(默认值),则 Run 方法将自动在启动程序后立即返回 0(不是错误代码)。
--------------------------------------------
例子:
Set WshShell = WScript.CreateObject("WScript.Shell")
Return = WshShell.Run("calc", 1, true)
WScript.Sleep 100
if Return<>0 Then '程序出错
WshShell.Popup "Calculator running error,"&vbcrlf&_
" Return value is: "& Return,4
Else '正常退出
WshShell.Popup "Calculator stop running,"&vbcrlf&_
" Return value is: "& Return,4
End If
set WshShell =Nothing
目前只有这一个算是可行的了。
‘柒’ 如何监控在服务器上运行的多个python脚本的状态
linux系统的话,监控工具比较好的有ganglia,zabbix
windows系统的话,用自带的“性能监视器”(老版本的windows叫性能计数器)
‘捌’ 求帮忙写一个shell脚本,监控进程的。
#!/bin/sh
ps -ef | grep -v grep || grep -w 'java -jar /opt/mcb/scap/bin/ftp.jar monitor'
if [ $? -eq 0 ]; then
echo "start"
else
java start
fi
‘玖’ 写一个监控进程的Python脚本,如果进程挂了就自动启动这个进程,相当于一个watchdog的作用~~谢谢
def monitor_process(key_word, cmd):
p1 = subprocess.Popen(['ps',
'-ef'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', key_word],
stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(['grep',
'-v', 'grep'], stdin=p2.stdout, stdout=subprocess.PIPE)
lines = p3.stdout.readlines()
if len(lines) > 0:
return
sys.stderr.write('process[%s] is lost, run [%s]\n' % (key_word,
cmd))
subprocess.call(cmd, shell=True)