File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -27,23 +27,23 @@ h2::before {
2727h3 ::before {
2828 counter-increment : h3;
2929 content : counter (h2 ) " ." counter (h3 );
30- margin-right : 0.8 rem ;
30+ margin-right : 0.5 rem ;
3131}
3232
3333h4 ::before {
3434 counter-increment : h4;
3535 content : counter (h2 ) " ." counter (h3 ) " ." counter (h4 );
36- margin-right : 0.8 rem ;
36+ margin-right : 0.5 rem ;
3737}
3838
3939h5 ::before {
4040 counter-increment : h5;
4141 content : counter (h2 ) " ." counter (h3 ) " ." counter (h4 ) " ." counter (h5 );
42- margin-right : 0.8 rem ;
42+ margin-right : 0.3 rem ;
4343}
4444
4545h6 ::before {
4646 counter-increment : h6;
4747 content : counter (h2 ) " ." counter (h3 ) " ." counter (h4 ) " ." counter (h5 ) " ." counter (h6 );
48- margin-right : 0.8 rem ;
48+ margin-right : 0.3 rem ;
4949}
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments