最近在做一个监控的项目。需要批量把一堆RTSP流放在浏览器上播放。后来精挑细选选择了开源的h5sstream。

在使用过程中发现每过一个小时服务会终止,API也无法访问。索性写了个脚本轮询,服务异常时自动重启h5ss。目前稳定运行!

检测脚本

#!/bin/bash

step=10 #10秒检查一次,间隔不能大于60
check_url(){
	echo "正在检测端口。。。"
	curl -s -m 3 http://192.168.1.118:8080/api/v1/GetSystemInfo
}

for (( i = 0; i < 60; i=(i+step) )); do
	if check_url
	then
		echo "服务正常"
	else
		echo "服务死亡"
		DATE_N=`date "+%Y-%m-%d %H:%M:%S"`
		echo "时间 > :$DATE_N" >> /root/check_url.log #记录死亡日志
		systemctl restart h5ss.service #重启服务
	fi
	sleep $step
done

exit 0

crontab中的定时任务配置


# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
* * * * * /root/checkUrl.sh #这里是上面那个脚本的路径

脚本作用

每10秒检测一次API能否访问。可以自行更改脚本内的时间。延长或缩短检测时间。当服务挂了之后脚本会记录日志。

PS:宕机时间很准时啊,1个小时零10秒准时宕机

时间 > :2019-09-09 08:05:44
时间 > :2019-09-09 09:05:54
时间 > :2019-09-09 10:06:04
时间 > :2019-09-09 11:06:14
时间 > :2019-09-09 12:06:24
时间 > :2019-09-09 13:06:34
时间 > :2019-09-09 14:06:44
时间 > :2019-09-09 15:06:54
时间 > :2019-09-09 16:07:04
时间 > :2019-09-09 17:07:14
时间 > :2019-09-09 18:07:24
时间 > :2019-09-09 19:07:34
时间 > :2019-09-09 20:07:44
时间 > :2019-09-09 21:07:54
时间 > :2019-09-09 22:08:04
时间 > :2019-09-09 23:08:14
时间 > :2019-09-10 00:08:24
时间 > :2019-09-10 01:08:34
时间 > :2019-09-10 02:08:44
时间 > :2019-09-10 03:08:54
时间 > :2019-09-10 04:09:04
时间 > :2019-09-10 05:09:14
时间 > :2019-09-10 06:09:24
时间 > :2019-09-10 07:09:34
时间 > :2019-09-10 08:09:44
时间 > :2019-09-10 09:09:54
时间 > :2019-09-10 10:10:04
时间 > :2019-09-10 11:10:14
时间 > :2019-09-10 12:10:24
时间 > :2019-09-10 13:10:34
时间 > :2019-09-10 14:10:44
时间 > :2019-09-10 15:10:54

发表回复