创建脚本
1 2
| vi restart_and_log_trojan.sh chmod +x restart_trojan_service.sh
|
内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #!/bin/bash
LOG_FILE="$HOME/trojan_restart.log"
if [ ! -f "$LOG_FILE" ]; then touch "$LOG_FILE" fi
echo "Restarting trojan.service at $(date)" >> "$LOG_FILE"
sudo systemctl restart trojan.service
sleep 10
echo "Status of trojan.service after restart:" >> "$LOG_FILE" sudo systemctl status trojan.service >> "$LOG_FILE" 2>&1 echo "--------------------------------------------------" >> "$LOG_FILE"
|
定时任务
在 crontab
文件中添加以下行以安排任务(假设在每周日和周四的晚上 11 点执行):
1
| 0 23 * * 0,4 ~/restart_trojan_service.sh
|