Skip to content

Commit 4285b19

Browse files
committed
service: Add systemd-run
1 parent 0b3ea44 commit 4285b19

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

docs/css/counter.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ h2::before {
2727
h3::before {
2828
counter-increment: h3;
2929
content: counter(h2) "." counter(h3);
30-
margin-right: 0.8rem;
30+
margin-right: 0.5rem;
3131
}
3232

3333
h4::before {
3434
counter-increment: h4;
3535
content: counter(h2) "." counter(h3) "." counter(h4);
36-
margin-right: 0.8rem;
36+
margin-right: 0.5rem;
3737
}
3838

3939
h5::before {
4040
counter-increment: h5;
4141
content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5);
42-
margin-right: 0.8rem;
42+
margin-right: 0.3rem;
4343
}
4444

4545
h6::before {
4646
counter-increment: h6;
4747
content: counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6);
48-
margin-right: 0.8rem;
48+
margin-right: 0.3rem;
4949
}

docs/ops/service.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ Timers 的主要缺点是:
287287

288288
[systemd-cron]: https://github.com/systemd-cron/systemd-cron
289289

290-
### 创建一个定时任务 {#create-timer}
290+
#### 创建一个定时任务 {#create-timer}
291291

292292
如上所述,一个定时任务包含两个文件,一个是 timer unit,一个是对应的 service unit。下面以 certbot 的配置文件为例,说明如何创建一个定时任务。
293293

@@ -324,6 +324,29 @@ WantedBy=timers.target
324324

325325
在编辑完两个文件之后,需要运行 `systemctl daemon-reload` 使 systemd 重新加载配置文件,然后可以使用 `systemctl start certbot.timer` 启动定时器,或者使用 `systemctl enable certbot.timer` 让其开机启动。
326326

327+
### 临时服务 {#transient-service}
328+
329+
systemd 提供了临时服务的支持,可以在需要时动态创建和启动服务,而不需要事先编写 `.service` 文件,这对于一些临时任务或一次性操作非常有用。
330+
331+
`systemd-run` 命令可以创建临时服务。例如,以下命令会创建一个临时服务并立即启动:
332+
333+
```shell
334+
systemd-run --unit=my-sleep sleep 600
335+
```
336+
337+
此后,你就可以通过 `systemctl status my-sleep` 查看临时服务的状态,或者使用诸如 `systemctl stop` / `systemctl restart` 等命令管理其状态了。同时,你也可以使用 `journalctl` 命令查看进程的输出([见下](#log))。
338+
339+
默认情况下,如果临时服务的命令正常退出了,那么对应的服务会被回收,即 `systemctl status my-sleep` 将会显示 service not found。此时你仍然可以使用 `journalctl` 命令查看日志,回收服务并不会清除其运行日志。`systemd-run` 有两个参数可以改变此默认行为:
340+
341+
- `-r` 可以使 systemd 在进程正常退出后仍然保留服务;
342+
- `-G` 可以使 systemd 在进程退出后立刻回收服务,即使不是正常退出(如非零的 exit code 或被信号杀死)。
343+
344+
同时,`systemd-run` 还有更多的参数可以用于指定服务进程的运行环境,例如工作目录和输入输出文件描述符等,具体可参考 [`systemd-run(1)`][systemd-run.1]
345+
346+
基于这些讨论,本文认为 `systemd-run``nohup` 命令的全面替代品,也鼓励读者尽量使用 `systemd-run` 来运行各种一次性的后台命令,而不是使用 `nohup``&`
347+
348+
作为一个常见的使用场景,普通用户需要使用 `systemd-run --user` 以运行临时的用户服务,而非系统服务。
349+
327350
## 日志管理 {#log}
328351

329352
### Systemd-journald

includes/man.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
[perf-stat.1]: https://man7.org/linux/man-pages/man1/perf-stat.1.html
1414
[rrsync.1]: https://man7.org/linux/man-pages/man1/rrsync.1.html
1515
[rsync.1]: https://man7.org/linux/man-pages/man1/rsync.1.html
16+
[systemd-run.1]: https://www.freedesktop.org/software/systemd/man/latest/systemd-run.html
1617

1718
<!-- man 2 -->
1819

0 commit comments

Comments
 (0)