脚本内容jiaobenname.sh
#!/bin/bash
# 设置环境变量
export JAVA_HOME=/usr/local/java/jdk-17.0.10
export CLASSPATH=.:$JAVA_HOME/lib/
export PATH=.:$JAVA_HOME/bin:$PATH
while true; do
if ps aux | grep -v grep | grep "tomcat" > /dev/null; then
echo "tomcat started..."
else
echo "tomcat is not running. Restarting..."
nohup tomcat/bin/startup.sh > /dev/null 2>&1 &
fi
sleep 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:$PATH
RUOYI_PATH="/xx/ruoyi-admin.jar"
RUOYI_LOG_FILE="/xx/rouyimonitor.log"
while true; do
if ps aux | grep -v grep | grep "redis-server" > /dev/null; then
echo "redis-server started..."
else
echo "redis-server is not running. Restarting..."
redis-server /xxx/redis.conf
fi
if ps aux | grep -v grep | grep "ruoyi-admin.jar" > /dev/null; then
echo "ruoyi-admin.jar started. Check the log at $RUOYI_LOG_FILE"
else
echo "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 &
fi
if ps aux | grep -v grep | grep "tomcat" > /dev/null; then
echo "tomcat started..."
else
echo "tomcat is not running. Restarting..."
nohup /xx/startup.sh > /dev/null 2>&1 &
fi
sleep 30
done