-1

我是 Linux 脚本的新手,我需要帮助来创建一个脚本,检查服务器上的一些已安装进程,如果其中一项服务没有运行,请重新启动它,然后再次检查这些服务,如果有任何错误,请使用 echo 打印如下:

dsisrv        (DSI service)                                  (7384)   Running
midaemon      (measurement interface)                        (1412)   Running
misrv         (measurement interface service)                (1384)   Running
perfalarm     (Alarm generator)                                       Stopped
perfalarmsrv  (Alarm generator service)                               Stopped
scopent       (data collector)                                        Stopped
scopesrv      (collector service)                                     Stopped
perfd         (Real Time Metric Access Daemon)               (7888)   Running
perfdsrv      (Real Time Metric Access Service)              (9020)   Running
ttd           (transaction tracking)                         (1808)   Running

如果上述任何服务停止,则运行重新启动命令的脚本。

感谢有人帮助我从这个脚本开始

问候,

4

2 回答 2

1
#!/bin/sh
 SERVICE='httpd'
 if ps ax | grep -v grep | grep $SERVICE > /dev/null 
then
 echo "$SERVICE service running, everything is fine"
 else 
echo "$SERVICE is not running" echo "$SERVICE is not running!" | mail -s "$SERVICE down" root 
fi

只需添加您正在寻找的服务,如果服务出现故障,这将向您发送邮件。我假设您使用 bash,所以很享受。

于 2015-05-26T14:32:25.400 回答
1

我做了简单的脚本我希望这会有所帮助请以root身份运行此脚本并在声明数组中添加您的服务或守护程序

declare -a service=(vsftpd sshd) 

完整脚本

#!/bin/bash
declare -a service=(vsftpd sshd) ##declaration array
for x in ${service[@]} ##array with 
do
process=` ps -A | grep $x | awk '{print $4}' ` ### all process output
all_services=`echo $x`
line_no=` ps -A | sed -n '/'$all_services'/=' `
if ` ps -A | grep ${process[@]} > 0 ` ## condition to check if service available or not
then
echo "status running", " `ps -A | sed -n ''$line_no''p | awk ' {print $1 $4}'` "  ## service up running
else
service $all_services start ### start the daemon again
fi
done
于 2015-05-27T13:08:31.777 回答