Linux设置定时自动重启某个系统服务【测试成功】

艺帆风顺 发布于 2025-04-03 16 次阅读


一、需求背景

    系统中存在YLMS.service该服务需要定时执行重启,否则运行会出错。

二、具体实现

编写shell脚本:

    #!/bin/bash
    while true; do current_time=$(date '+%Y-%m-%d %H:%M:%S')
    echo "$current_time Restarting YLMS service"
    systemctl restart YLMS.service
    if [ $? -eq 0 ]; then echo "$current_time YLMS service restarted successfully" else echo "$current_time Failed to restart YLMS service" fi
    sleep 12hdone

    加入计划任务中:每天凌晨三点执行一次

      [root@YLMonitor sh]# crontab -l3 * * * /temp/sh/YLMS.sh