当前位置: 技术问答>linux和unix
如何写一个脚本监视进程
来源: 互联网 发布时间:2015-07-28
本文导语: 比如说我想监视一个进程为gaim,每两分钟查看他是否运行,如果没有运行则开启进程. | #!/bin/sh # # Shell Script for Watching the Simph323-client # # Usage function usage() { echo "Usage: $0 classname listen-port...
比如说我想监视一个进程为gaim,每两分钟查看他是否运行,如果没有运行则开启进程.
|
#!/bin/sh
#
# Shell Script for Watching the Simph323-client
#
# Usage function
usage()
{
echo "Usage: $0 classname listen-port log-number"
echo "Example:"
echo " $0 2004060400006 1755 6"
echo ""
}
# Check input parameters
args=$#
if [ $args -ne "3" ]
then
usage
exit 1
fi
# Watch the simph323-client status
app="./simph323 -g 61.159.46.62 -u $1 -u $1#1 -a -l -i 61.159.46.63:$2"
port=$2
cout=0;
echo "$app"
while [ 1 ]
do
# Inquire if the process exist or not
rs=`ps -x | grep "$app" | grep -v "grep"`
# echo "rs = $rs"
if [ -z "$rs" ]
then
echo "`date "+%Y-%m-%d %H:%M:%S"` The process is not found !"
# Change port, and restart ...
if [ $port -eq $2 ]
then
port=`expr $port + 10`
else
port=$2
fi
app="./simph323 -g 61.159.46.62 -u $1 -u $1#1 -a -l -i 61.159.46.63:$port"
echo $app
sleep 1
$app 1>>$3.out 2>>$3.out &
cout=0
else
if [ $cout -eq "0" ]
then
echo "`date "+%Y-%m-%d %H:%M:%S"` The process is running ..."
else
if [ $cout -eq "59" ]
then
cout=0
continue
fi
fi
cout=`expr $cout + 1`
fi
sleep 1
done
#
# Shell Script for Watching the Simph323-client
#
# Usage function
usage()
{
echo "Usage: $0 classname listen-port log-number"
echo "Example:"
echo " $0 2004060400006 1755 6"
echo ""
}
# Check input parameters
args=$#
if [ $args -ne "3" ]
then
usage
exit 1
fi
# Watch the simph323-client status
app="./simph323 -g 61.159.46.62 -u $1 -u $1#1 -a -l -i 61.159.46.63:$2"
port=$2
cout=0;
echo "$app"
while [ 1 ]
do
# Inquire if the process exist or not
rs=`ps -x | grep "$app" | grep -v "grep"`
# echo "rs = $rs"
if [ -z "$rs" ]
then
echo "`date "+%Y-%m-%d %H:%M:%S"` The process is not found !"
# Change port, and restart ...
if [ $port -eq $2 ]
then
port=`expr $port + 10`
else
port=$2
fi
app="./simph323 -g 61.159.46.62 -u $1 -u $1#1 -a -l -i 61.159.46.63:$port"
echo $app
sleep 1
$app 1>>$3.out 2>>$3.out &
cout=0
else
if [ $cout -eq "0" ]
then
echo "`date "+%Y-%m-%d %H:%M:%S"` The process is running ..."
else
if [ $cout -eq "59" ]
then
cout=0
continue
fi
fi
cout=`expr $cout + 1`
fi
sleep 1
done
|
#! /usr/bin/perl
$app = shift;
while (1) {
$result = `ps aux|grep $app|grep -v grep`;
unless ($result) {
system ("$app&") == 0
or die "can not start $app: $!";
sleep 300;
}
}
$app = shift;
while (1) {
$result = `ps aux|grep $app|grep -v grep`;
unless ($result) {
system ("$app&") == 0
or die "can not start $app: $!";
sleep 300;
}
}