Systemd 使用
定时任务
创建一个定时器:
fdsk
bash
sudo vi /etc/systemd/system/reboot.timer内容如下:
ini
[Unit]
Description=Daily reboot at midnight
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true # 确保系统在下次启动时,如果之前有错过的定时任务,会立即执行那次任务
# Unit=reboot.service 不指定默认为 timer 同名 service 文件
[Install]
WantedBy=timers.target创建对应的服务:
bash
sudo vi /etc/systemd/system/reboot.service内容如下:
ini
[Unit]
Description=Reboot system
[Service]
Type=oneshot # 服务是一次性任务,执行完后将立即退出
ExecStart=/usr/bin/systemctl reboot启用定时器
bash
sudo systemctl enable --now reboot.timer查看服务是否存在:
bash
systemctl status reboot.service查看定时器是否启用:
bash
systemctl list-timers查看定时器的详细信息:
bash
systemctl cat reboot.timer如果没有手动指定 Unit=,它会默认查找 reboot.service。
移除定时任务
先停止相关的定时器和服务:
bash
sudo systemctl stop reboot.timer
sudo systemctl stop reboot.service禁用定时器以防止它开机启动:
bash
sudo systemctl disable reboot.timer删除定时器和服务文件
删除相关的 systemd 配置文件:
bash
sudo rm /etc/systemd/system/reboot.timer
sudo rm /etc/systemd/system/reboot.service重新加载 systemd 配置
删除配置后,重新加载 systemd 配置以生效:
bash
sudo systemctl daemon-reload验证是否已移除
运行以下命令,确认定时器和服务已移除:
bash
systemctl list-timers --all
systemctl list-units --all | grep <name>启动模式
检查当前的默认启动目标
bash
systemctl get-default设置图形界面为默认启动
bash
sudo systemctl set-default graphical.target设置多用户模式为默认启动
bash
sudo systemctl set-default multi-user.target手动启动图形界面
bash
sudo systemctl start graphical.targetTips
重新加载 unit 文件
bash
systemctl daemon-reload重启 systemd 进程
很少用,一般系统升级才需要)
bash
systemctl daemon-reexec列出激活的 target
bash
systemctl list-units --type=target列出系统中所有的target
bash
systemctl list-units --type=target --all所有已安装的 target
bash
systemctl list-unit-files --type=targetSystemd
当 systemd 以用户实例(--user)运行时,加载单元的先后顺序(较前的目录优先级较高): https://www.jinbuguo.com/systemd/systemd.unit.htmlhttps://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html
让 systemd 重新加载用户服务配置:
sh
systemctl --user daemon-reexec
systemctl --user daemon-reloadsh
loginctl show-user $USER
#如果输出中 Linger=no,那么你可能需要开启 linger 才能使用用户服务:
sudo loginctl enable-linger $USER
systemctl --user status
mkdir -p ~/.config/systemd/user
cp battery-monitor.service ~/.config/systemd/user/
systemctl --user daemon-reload
systemctl --user enable --now battery-monitor.service