監控多個進程的腳本
『壹』 幫忙寫一個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)