sh Bash脚本,用于检查网站是启动还是关闭,并通过电子邮件发送通知

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Bash脚本,用于检查网站是启动还是关闭,并通过电子邮件发送通知相关的知识,希望对你有一定的参考价值。

#!/bin/bash

################################################################################

# This script will check to see if a website is up/down by pinging the url
# If there is no response an email wil be send via an external smtp mail server
# If the site status is down an email will be send when the site is up again

# set your check interval here :-) #############################################
interval=3600 # hour

# begin status ; DO NOT CHANGE #################################################
stat=0 # this is the status UP; status 1 is DOWN

# your url #####################################################################
url="localhost"

# email settings ###############################################################

send(){ # subject message
	toemail="you@gmail.com"
	fromemail="you@gmail.com"
	smtpserver="smtp.gmail.com"
	port=587
	username="you@gmail.com"
	passw="YourVerySecretPassword"
	sendemail -f "$fromemail" -t "$toemail" -u "$1" -m "$2" -s \
	"$smtpserver:$port" -xu "$username" -xp "$passw" -o tls=yes -q
}

# check url ####################################################################

while :
do
	wget --server-response --spider $url # checking the actual response is
        # better, because server me be running and returning pings while our
        # site could be down. This wget will only return exit 0 if response is
        # 200 ok
        #ping -c 1 $url | grep "0%" > /dev/null
	# if we get zero percent lost in the ping, all is well and we should get
	# exit status 0 on this last program run / exited
	if [ "$?" -eq 0 ] # so if we have exit status of zero then server is UP
	then
		# only if the current status is DOWN (1) then we want to notify
		# the new status up and change the stat variable accordingly
		if [ "$stat" -eq 1 ]
		then
		echo "UP | `date`" >> "$url-status.log"
		send "$url is UP" "UP | `date`"
		stat=0
		fi
	else
		# same as above but the other way around
		if [ "$stat" -eq 0 ]
		then
		echo "DOWN | `date`" >> `echo "$url-status.log"`
      		send "$url is DOWN" "DOWN | `date`"
		stat=1
		fi
	fi

	sleep $interval

done

exit

# Ciao #########################################################################

以上是关于sh Bash脚本,用于检查网站是启动还是关闭,并通过电子邮件发送通知的主要内容,如果未能解决你的问题,请参考以下文章

sh Bash shell脚本,用于检查本地Vault服务器的密封状态,并尝试使用保管库保密库中的密钥进行密封。支持HA Va

sh 用于检查MySQL是否正在运行的简单bash脚本。

sh 用于检查当前git branch =“x”的bash脚本

用于检查连接的串行端口的Bash脚本

sh 用于构建和启动Google Fuchsia的BASH脚本

linux 中,自定义启动 和 关闭脚本