脚本内容jiaobenname.sh
#!/bin/bash
# 设置环境变量
export JAVA_HOME=/usr/local/java/jdk-17.0.10
export CLASSPATH=.:$JAVA_HOME/lib/
export PATH=.:$JAVA_HOME/bin:$PATHwhile true; doif ps aux | grep -v grep | grep "tomcat" > /dev/null; thenecho "tomcat started..."elseecho "tomcat is not running. Restarting..."nohup tomcat/bin/startup.sh > /dev/null 2>&1 &fisleep 30
done
标红目录改成你的
执行命令
sudo vim /etc/systemd/system/ziqidong.service
i
加入
[Unit]
Description=Process Watchdog Service
After=network.target[Service]
Type=simple
ExecStart=/nidemulu/jiaobenname.sh
Restart=on-failure[Install]
WantedBy=multi-user.target
标红目录改成你的
esc
:wq
记得给目录赋权
chmod -R 777 /nidemulu/
chmod -R 777 /etc/systemd/system/
加载 systemd 配置:
sudo systemctl daemon-reload
开机自启动:
sudo systemctl enable ziqidong.service
启动服务:
sudo systemctl start ziqidong.service
检查服务状态:
sudo systemctl status ziqidong.service
# 关闭后台服务
sudo systemctl stop ziqidong.service
# 重启服务
sudo systemctl restart ziqidong.service
我的脚本如下参考
#!/bin/bash
# 设置环境变量
export JAVA_HOME=/usr/local/java/jdk-17.0.10
export CLASSPATH=.:$JAVA_HOME/lib/
export PATH=.:$JAVA_HOME/bin:$PATHRUOYI_PATH="/xx/ruoyi-admin.jar"RUOYI_LOG_FILE="/xx/rouyimonitor.log"while true; doif ps aux | grep -v grep | grep "redis-server" > /dev/null; thenecho "redis-server started..."elseecho "redis-server is not running. Restarting..."redis-server /xxx/redis.conffiif ps aux | grep -v grep | grep "ruoyi-admin.jar" > /dev/null; thenecho "ruoyi-admin.jar started. Check the log at $RUOYI_LOG_FILE"elseecho "ruoyi-admin.jar is not running. Restarting..."nohup java --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/sun.net.util=ALL-UNNAMED -jar -Xms1024M -Xmx2048M -XX:MetaspaceSize=1024M -XX:MaxMetaspaceSize=2048M $RUOYI_PATH > $RUOYI_LOG_FILE 2>&1 &fiif ps aux | grep -v grep | grep "tomcat" > /dev/null; thenecho "tomcat started..."elseecho "tomcat is not running. Restarting..."nohup /xx/startup.sh > /dev/null 2>&1 &fisleep 30
done