简体   繁体   English

Linux自我修复脚本检查一些过程

[英]Linux self-healing script to check some process

Im new with Linux scripts, I need help to create one script with check some installed processes on a server and if one of these services is not running restart it, and recheck again those services and if there any error print it with echo as below : 我是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

in case any of above services is stopped, the script to run restart command. 如果以上任何服务停止,请运行脚本以重新启动命令。

Appreciate if any one help me to start with this script 赞赏是否有人帮助我开始使用此脚本

Regards, 问候,

#!/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

Just add the service your looking for, this will mail you if the service goes down. 只需添加您要查找的服务,如果该服务出现故障,它将通过邮件发送给您。 Im assuming your using bash so enjoy. 我假设您正在使用bash,请尽情享受。

i did simple script i hope this will be helpful please run this script as a root and add your services or daemons inside declaration array 我做了简单的脚本,希望对您有所帮助,请以根用户身份运行此脚本,并在声明数组中添加您的服务或守护程序

declare -a service=(vsftpd sshd) 

full script 完整脚本

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM