定时重启并记录时间的脚本

wen 实用脚本 1

本文目录导读:

定时重启并记录时间的脚本

  1. Linux 版本 (使用 bash + systemd 或 crontab)
  2. Windows 版本 (使用 PowerShell + 任务计划程序)
  3. 日志查看与分析示例
  4. 注意事项

一个适用于 Linux,另一个适用于 Windows


Linux 版本 (使用 bash + systemd 或 crontab)

推荐使用 systemd timer,因为它比 crontab 更可靠且易于管理日志。

方案 A: 使用 systemd timer (最推荐)

创建脚本文件 /usr/local/bin/reboot_with_log.sh

sudo nano /usr/local/bin/reboot_with_log.sh
#!/bin/bash
# 日志文件路径
LOG_FILE="/var/log/reboot_history.log"
# 获取当前时间
CURRENT_TIME=$(date '+%Y-%m-%d %H:%M:%S')
# 记录重启前的日志(带时间戳)
echo "[$CURRENT_TIME] System is about to reboot. Reason: Scheduled task." >> "$LOG_FILE"
# 执行重启命令
/sbin/reboot

赋予执行权限

sudo chmod +x /usr/local/bin/reboot_with_log.sh

创建 systemd service 文件

sudo nano /etc/systemd/system/reboot-with-log.service
[Unit]
Description=Reboot system with logging
[Service]
Type=oneshot
ExecStart=/usr/local/bin/reboot_with_log.sh

创建 systemd timer 文件

sudo nano /etc/systemd/system/reboot-with-log.timer

以每天早上 3:00 重启为例):

[Unit]
Description=Schedule reboot with log at 3:00 AM daily
[Timer]
OnCalendar=daily
# 指定具体时间:每天 3:00
# 格式:星期 时:分:秒
OnCalendar=*-*-* 03:00:00
# 防止丢失任务
Persistent=true
[Install]
WantedBy=timers.target

启用并启动定时器

sudo systemctl daemon-reload
sudo systemctl enable reboot-with-log.timer
sudo systemctl start reboot-with-log.timer

查看日志

cat /var/log/reboot_history.log

查看定时器状态

sudo systemctl list-timers --all | grep reboot

方案 B: 使用 crontab (更简单)

创建脚本文件 /usr/local/bin/reboot_with_log.sh

#!/bin/bash
LOG_FILE="/var/log/reboot_history.log"
CURRENT_TIME=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$CURRENT_TIME] System is about to reboot. Reason: Cron task." >> "$LOG_FILE"
/sbin/reboot

赋予执行权限

sudo chmod +x /usr/local/bin/reboot_with_log.sh

编辑 crontab

sudo crontab -e

添加以下行(每天凌晨 3:00 重启):

0 3 * * * /usr/local/bin/reboot_with_log.sh

查看日志

cat /var/log/reboot_history.log

Windows 版本 (使用 PowerShell + 任务计划程序)

方法:创建 PowerShell 脚本 + 任务计划程序

创建脚本文件 C:\Scripts\RebootWithLog.ps1

# 日志文件路径
$LogFile = "C:\Logs\RebootHistory.log"
# 获取当前时间
$CurrentTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# 写入日志
"[$CurrentTime] System is about to reboot. Reason: Scheduled task." | Out-File -FilePath $LogFile -Append
# 重启计算机 (立即重启,无延迟)
Restart-Computer -Force

创建日志目录

New-Item -ItemType Directory -Force -Path "C:\Logs"

设置脚本执行策略

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

创建计划任务 (每天凌晨 3:00 重启)

打开 PowerShell 管理员模式,执行:

# 创建触发器(每天 3:00)
$Trigger = New-ScheduledTaskTrigger -Daily -At "03:00"
# 定义操作(启动 PowerShell 执行脚本)
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File `"C:\Scripts\RebootWithLog.ps1`""
# 设置任务(以 SYSTEM 身份运行,最高权限)
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
# 注册任务
Register-ScheduledTask -TaskName "ScheduledRebootWithLog" -Trigger $Trigger -Action $Action -Settings $Settings -RunLevel Highest -User "NT AUTHORITY\SYSTEM"

查看日志

Get-Content "C:\Logs\RebootHistory.log"

日志查看与分析示例

Linux 查看日志(带时间戳反查)

# 实时查看最后 10 行
tail -f /var/log/reboot_history.log
# 搜索某次重启
grep "2025-03-23 03:00" /var/log/reboot_history.log
# 统计重启次数
wc -l /var/log/reboot_history.log

Windows 查看日志

# 显示所有日志
Get-Content "C:\Logs\RebootHistory.log"
# 实时监控(需要 PowerShell 7+)
Get-Content "C:\Logs\RebootHistory.log" -Wait

注意事项

  1. 确保脚本路径和日志目录的权限正确

    • Linux:脚本需 chmod +x,日志文件所在目录需有写入权限。
    • Windows:脚本需以管理员权限运行,日志目录需有写入权限。
  2. 测试脚本

    • Linux:手动执行 sudo /usr/local/bin/reboot_with_log.sh 测试。
    • Windows:手动执行 C:\Scripts\RebootWithLog.ps1 测试(注意会立刻重启)。
  3. 修改重启时间

    • Linux systemd timer:编辑 OnCalendar 行,sudo systemctl daemon-reload
    • Linux crontab:直接修改 sudo crontab -e
    • Windows:在任务计划程序中修改触发器时间。
  4. 取消定时重启

    • Linux systemd:sudo systemctl disable reboot-with-log.timer
    • Linux crontab:sudo crontab -e 注释掉或删除该行。
    • Windows:Unregister-ScheduledTask -TaskName "ScheduledRebootWithLog"

选择适合你系统的方案即可,如果需要更复杂的日志记录(如记录重启原因、IP 地址等),可以进一步扩展脚本。

抱歉,评论功能暂时关闭!