neko
deepin
10 hours ago Ecological co-builder
Q&A Team
让ai写个systemd定时任务
Reply Like 0 View the author
让ai写个systemd定时任务
有定时关机功能,可以定时关机,早上在手动开
或者:AI 回答的
在 Linux 上创建定时重启任务,最常用的是 cron。下面按常见需求给你几种做法(适用于大多数发行版,如 Ubuntu / CentOS / Debian)。
crontab -e
如果是 root 权限重启(推荐),请使用 root 用户:
sudo crontab -e
语法:
分 时 日 月 星期 命令
0 3 * * * /sbin/reboot
0 4 * * 0 /sbin/reboot
0 */6 * * * /sbin/reboot
保存后退出即可生效。
crontab -l
编辑系统定时任务文件:
sudo vim /etc/crontab
格式(注意多一个 user 字段):
0 3 * * * root /sbin/reboot
sudo vim /etc/systemd/system/reboot-job.service
内容:
[Unit]
Description=Reboot Timer
[Service]
Type=oneshot
ExecStart=/sbin/reboot
sudo vim /etc/systemd/system/reboot-job.timer
每天 3:00 重启:
[Unit]
Description=Daily Reboot Timer
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable reboot-job.timer
sudo systemctl start reboot-job.timer
查看状态:
systemctl list-timers
/sbin/rebootgrep CRON /var/log/syslog
# 或
journalctl -u cron
| 场景 | 推荐方式 |
|---|---|
| 简单定时重启 | crontab |
| 服务器生产环境 | systemd timer |
| 多用户系统 | root crontab |
如果你能告诉我:
我可以帮你直接生成 可复制的配置。
也可以:一条命令搞定,设置每天凌晨3点重启
(sudo crontab -l 2>/dev/null; echo "0 3 * * * /sbin/reboot") | sudo crontab -
Popular Ranking
ChangePopular Events
More
比如让电脑在每天凌晨2点自动重启,deepin在哪里可以设置吗?我只看到有一个定时关机的功能,但没找到定时重启的选项...