立即注册  找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 77|回复: 6

设置每天定时开机关机

[复制链接]

设置每天定时开机关机

[复制链接]

187

主题

103

回帖

1203

积分

管理员

积分
1203
admin

187

主题

103

回帖

1203

积分

管理员

积分
1203
2025-6-7 16:28:07 | 显示全部楼层 |阅读模式
1. 设置每天定时开机
方法 1:使用 RTC 闹钟(推荐,无需 BIOS 设置)
RTC(Real Time Clock)闹钟可以让系统在指定时间自动开机,适用于大多数 Linux 服务器和 PC。

步骤:
检查 RTC 是否支持 wakealarm

bash
ls /sys/class/rtc/rtc0/wakealarm
如果文件存在,说明支持。

设置明天的开机时间(如 07:00)

bash
# 计算明天的 07:00 的时间戳
sudo sh -c 'echo $(date -d "tomorrow 07:00" +%s) > /sys/class/rtc/rtc0/wakealarm'
tomorrow 07:00 可以换成 "today 22:00" 或 "2025-12-31 08:00" 等具体时间。

验证设置

bash
cat /sys/class/rtc/rtc0/wakealarm
应该返回一个 Unix 时间戳。

让系统关机后仍能生效

bash
sudo systemctl enable --now rtcwake.service  # 如果存在的话
测试

bash
sudo shutdown -h now
系统应在设定的时间自动开机。

自动化(每天 07:00 开机)
由于 RTC 闹钟是一次性的,我们需要在每次开机后重新设置。可以通过 cron 或 systemd 实现:

方法 A:使用 cron

bash
sudo crontab -e
添加:

bash
@reboot sh -c 'echo $(date -d "tomorrow 07:00" +%s) > /sys/class/rtc/rtc0/wakealarm'
这样每次开机后都会重新设置明天的开机时间。

方法 B:使用 systemd 服务(更可靠)

创建服务文件:

bash
sudo vim /etc/systemd/system/set-wakealarm.service
写入:

ini
[Unit]
Description=Set RTC wakealarm for next boot
After=syslog.target

[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo $(date -d "tomorrow 07:00" +%s) > /sys/class/rtc/rtc0/wakealarm'

[Install]
WantedBy=multi-user.target
启用服务:

bash
sudo systemctl enable --now set-wakealarm.service
方法 2:使用 BIOS Wake-on-LAN (WOL)
如果主板支持 WOL,可以配合 cron 或路由器定时发送魔术包唤醒机器。

步骤:
启用 WOL

bash
sudo ethtool -s eth0 wol g  # eth0 换成你的网卡名
设置 BIOS 启用 Wake-on-LAN

进入 BIOS,找到 Power Management → Wake-on-LAN,启用它。

用另一台机器/路由器定时发送 WOL 包

如果有一台 Linux 机器一直运行,可以设置 cron:

bash
0 6 * * * wakeonlan MAC_ADDRESS  # 每天早上 6:00 唤醒
部分路由器(如 OpenWRT、Tomato)支持定时发送 WOL 包。

2. 设置每天定时关机
方法 1:使用 cron(推荐)
bash
sudo crontab -e
添加一行,例如 每天 23:00 关机:

bash
0 23 * * * /sbin/shutdown -h now
0 23 * * * = 每天 23:00 执行

/sbin/shutdown -h now = 立即关机

方法 2:使用 systemd 定时器
创建关机服务:

bash
sudo vim /etc/systemd/system/auto-shutdown.service
ini
[Unit]
Description=Auto shutdown daily

[Service]
Type=oneshot
ExecStart=/sbin/shutdown -h now
创建定时器:

bash
sudo vim /etc/systemd/system/auto-shutdown.timer
ini
[Unit]
Description=Run auto-shutdown daily at 23:00

[Timer]
OnCalendar=*-*-* 23:00:00
Persistent=true

[Install]
WantedBy=timers.target
启用定时器:

bash
sudo systemctl enable --now auto-shutdown.timer
3. 最终方案
功能        方法
定时开机        RTC wakealarm + cron/systemd 自动重新设置
定时关机        cron 或 systemd 定时器
示例流程
开机时(07:00):

系统自动启动(RTC 闹钟)。

cron 或 systemd 重新设置明天的 wakealarm。

关机时(23:00):

cron 或 systemd 执行 shutdown -h now。

4. 验证
检查开机日志:

bash
journalctl -b | grep -i "wake\|rtc"
检查关机计划:

bash
sudo crontab -l
sudo systemctl list-timers
5. 注意事项
RTC 闹钟 需要主板支持,部分虚拟机可能不适用。

BIOS WOL 需要网卡和主板支持,关机后不能断电。

cron 需要确保时间同步(ntp/chronyd)。

这样,你的 CentOS 7 就可以实现 每天定时开机 + 定时关机 了! 🚀

187

主题

103

回帖

1203

积分

管理员

积分
1203
admin 楼主

187

主题

103

回帖

1203

积分

管理员

积分
1203
2025-6-7 17:30:14 | 显示全部楼层
定时关机服务文件
  1. sudo systemctl enable --now auto-shutdown.timer
复制代码

定时开机服务文件
  1. sudo systemctl enable --now set-wakealarm.service
复制代码

以上文件上传到/etc/systemd/system
sudo rtcwake -m no -l -t $(date -d "tomorrow 04:08" +%s)
cat /sys/class/rtc/rtc0/wakealarm

sudo systemctl daemon-reload

sudo systemctl list-timers --all

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

187

主题

103

回帖

1203

积分

管理员

积分
1203
admin 楼主

187

主题

103

回帖

1203

积分

管理员

积分
1203
2025-6-8 05:57:28 | 显示全部楼层
在 CentOS 7 中,`rtcwake.service` 并不是默认存在的服务,但你可以创建它来管理 **RTC 唤醒(定时开机)** 功能。以下是详细配置方法:

---

## **1. 检查 RTC 设备是否支持唤醒**
首先确认你的系统是否支持 RTC 唤醒:
```bash
ls /sys/class/rtc/rtc0/wakealarm
```
如果该文件存在,说明支持 RTC 唤醒。

---

## **2. 创建 `rtcwake.service` 服务**
### **(1) 创建服务文件**
```bash
sudo vim /etc/systemd/system/rtcwake.service
```
写入以下内容:
```ini
[Unit]
Description=Set RTC Wakeup Alarm for Automatic Boot
After=syslog.target

[Service]
Type=oneshot
# 设置明天的 07:00 开机(可自行调整时间)
ExecStart=/bin/sh -c 'echo $(date -d "tomorrow 07:00" +%s) > /sys/class/rtc/rtc0/wakealarm'
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
```
- `ExecStart` 设置明天的 07:00 自动开机(可修改为 `"today 22:00"` 或 `"2025-12-31 08:00"` 等)。
- `RemainAfterExit=yes` 表示服务运行后仍保持状态。

### **(2) 启用并启动服务**
```bash
sudo systemctl daemon-reload         # 重新加载 systemd 配置
sudo systemctl enable rtcwake.service  # 开机自启
sudo systemctl start rtcwake.service   # 立即运行一次
```

### **(3) 检查是否生效**
```bash
cat /sys/class/rtc/rtc0/wakealarm
```
应该返回一个 Unix 时间戳(如 `1767222000`)。

---

## **3. 测试定时开机**
```bash
sudo shutdown -h now
```
等待系统关机后,观察是否在设定的时间自动开机。

---

## **4. 进阶优化(可选)**
### **(1) 每天自动更新唤醒时间**
由于 `rtcwake.service` 只运行一次,我们可以用 `cron` 或 `systemd timer` 每天更新唤醒时间。

#### **方法 A:使用 `cron`**
```bash
sudo crontab -e
```
添加:
```bash
@reboot /bin/sh -c 'echo $(date -d "tomorrow 07:00" +%s) > /sys/class/rtc/rtc0/wakealarm'
```
这样每次开机后都会重新设置明天的唤醒时间。

#### **方法 B:使用 `systemd timer`**
1. 创建定时器:
   ```bash
   sudo vim /etc/systemd/system/rtcwake.timer
   ```
   ```ini
  1. [Unit]
  2. Description=Update RTC wakealarm daily at 04:10

  3. [Timer]
  4. OnCalendar=*-*-* 04:10:00
  5. Persistent=true
  6. Unit=rtcwake.server
  7. [Install]
  8.    WantedBy=timers.target
复制代码

   ```
2. 启用定时器:
   ```bash
   sudo systemctl enable --now rtcwake.timer
   ```

---

## **5. 常见问题**
### **Q1: `/sys/class/rtc/rtc0/wakealarm` 不存在?**
- 可能是内核或硬件不支持 RTC 唤醒,尝试:
  ```bash
  dmesg | grep rtc
  ```
  检查是否有相关错误。

### **Q2: 唤醒后时间不正确?**
- 检查时区:
  ```bash
  timedatectl
  ```
  如果需要修改:
  ```bash
  sudo timedatectl set-timezone Asia/Shanghai
  ```

### **Q3: 如何取消唤醒?**
清空 `wakealarm`:
```bash
echo 0 > /sys/class/rtc/rtc0/wakealarm
```

---

## **6. 总结**
| 操作                | 命令/配置 |
|---------------------|----------|
| **创建 `rtcwake.service`** | `sudo vim /etc/systemd/system/rtcwake.service` |
| **启用服务**         | `sudo systemctl enable --now rtcwake.service` |
| **手动设置唤醒时间** | `echo $(date -d "tomorrow 07:00" +%s) > /sys/class/rtc/rtc0/wakealarm` |
| **取消唤醒**         | `echo 0 > /sys/class/rtc/rtc0/wakealarm` |

这样你的 CentOS 7 就可以每天自动定时开机了!🚀

187

主题

103

回帖

1203

积分

管理员

积分
1203
admin 楼主

187

主题

103

回帖

1203

积分

管理员

积分
1203
2025-6-8 20:06:07 | 显示全部楼层
  1. sudo vim /etc/systemd/system/***.service
  2. #让系统关机后仍能生效
  3. sudo systemctl enable --now ***.service
  4. sudo systemctl daemon-reload         # 重新加载 systemd 配置
  5. sudo systemctl enable ***.service  # 开机自启
  6. sudo systemctl start ***.service   # 立即运行一次

  7. # 查看定时器状态</span>
  8. systemctl list-timers --all

  9. # 检查下次执行时间</span>
  10. systemctl status ***.timer


复制代码

187

主题

103

回帖

1203

积分

管理员

积分
1203
admin 楼主

187

主题

103

回帖

1203

积分

管理员

积分
1203
2025-12-15 18:30:18 | 显示全部楼层
sudo systemctl enable --now auto-shutdown.timer

187

主题

103

回帖

1203

积分

管理员

积分
1203
admin 楼主

187

主题

103

回帖

1203

积分

管理员

积分
1203
2025-12-18 16:57:38 | 显示全部楼层
sudo vim /etc/systemd/system/auto-shutdown.service

  1. [Unit]
  2. Description=Auto shutdown daily

  3. [Service]
  4. Type=oneshot
  5. ExecStart=/sbin/shutdown -h now
复制代码
sudo vim /etc/systemd/system/auto-shutdown.timer
  1. [Unit]
  2. Description=Run auto-shutdown daily at 23:58

  3. [Timer]
  4. OnCalendar=*-*-* 23:58:00
  5. Persistent=true

  6. [Install]
  7. WantedBy=timers.target
复制代码


sudo systemctl daemon-reload
sudo systemctl list-timers --all


187

主题

103

回帖

1203

积分

管理员

积分
1203
admin 楼主

187

主题

103

回帖

1203

积分

管理员

积分
1203
2025-12-18 17:10:40 | 显示全部楼层
定时关机脚本

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|小黑屋|学无止境

GMT+8, 2026-9-12 11:20 , Processed in 0.279366 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表