#!/bin/sh# 服务名称serviceName="TongWeb"# 日志地址log="/opt/monitor/log.log"# 执行路径commandPath="/opt/TongWeb/TW_7/bin/startserver.sh"# 间隔时间(单位:s)intervalTime=10whiletrue;donow=$(date +"%Y-%m-%d %H:%M:%S")# 进程数量pid=$(pgrep -f $serviceName |wc-l)# 若进程不存在, 则启动服务if["$pid"-eq0];thennohup>/dev/null 2>&1$commandPath&# 日志输出echo"$now$serviceName has been restarted!">>$logelseecho"$now$serviceName already started!">>$logfisleep$intervalTimedone
# ps: 添加该声明后可通过脚本文件名搜索到进程信息#!/bin/sh
2.设置脚本开机自启
vi /etc/rc.d/rc.local
#!/bin/bash# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES## It is highly advisable to create own systemd services or udev rules# to run scripts during boot instead of using this file.## In contrast to previous versions due to parallel execution during boot# this script will NOT be run after all other services.## Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure# that this script will be executed during boot.touch /var/lock/subsys/local
/opt/shell/restart.sh &