
用 systemd-run 替代 screen 运行后台任务
来源标题:Use systemd-run as an alternative for screen
systemd-run 可将临时命令作为瞬态服务运行,SSH 断开后任务继续执行,并可用 systemctl 和 journalctl 监控和查看日志。它可作为 screen 或 tmux 的替代方案,每个命令都会成为一个 systemd 服务单元。
以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。
systemd-run 会把临时命令作为 transient service 启动,因此作业在 SSH 断开后仍会继续运行,并且可以用 systemctl 和 journalctl 进行监控。你可能用过 screen 或 tmux 在服务器上运行临时命令,让它在 SSH 会话关闭后继续运行。可以考虑用 systemd-run 作为替代方案。它会把每条命令都变成一个 systemd service unit:
以 unit run-benchmark.service 运行 openssl speed
$ sudo systemd-run --unit=run-benchmark openssl speed
查询当前状态
$ systemctl status run-benchmark.service ● run-benchmark.service - /usr/bin/openssl speed Loaded: loaded (/run/systemd/transient/run-benchmark.service; transient) Transient: yes Active: active (running) since Wed 2022-11-23 09:25:09 CET; 7min ago Main PID: 18344 (openssl) Tasks: 1 (limit: 4915) CGroup: /system.slice/run-benchmark.service └─18344 /usr/bin/openssl speed
获取日志
$ sudo journalctl -u run-benchmark.service -- Logs begin at Mon 2022-03-07 13:45:18 CET, end at Wed 2022-11-23 09:31:33 CET. -- Nov 23 09:25:09 server systemd[1]: Started /usr/bin/openssl speed. Nov 23 09:25:12 server openssl[18344]: Doing md4 for 3s on 16 size blocks: 12952588 md4's in 3.00s Nov 23 09:25:15 server openssl[18344]: Doing md4 for 3s on 64 size blocks: 9936296 md4's in 3.00s Nov 23 09:25:18 server openssl[18344]: Doing md4 for 3s on 256 size blocks: 5475381 md4's in 3.00s Nov 23 09:25:21 server openssl[18344]: Doing md4 for 3s on 1024 size blocks: 2126597 md4's in 3.00s Nov 23 09:25:24 server openssl[18344]: Doing md4 for 3s on 8192 size blocks: 311796 md4's in 3.00s Nov 23 09:25:27 server openssl[18344]: Doing md4 for 3s on 16384 size blocks: 156100 md4's in 3.00s Nov 23 09:25:30 server openssl[18344]: Doing md5 for 3s on 16 size blocks: 17646219 md5's in 3.00s Nov 23 09:25:33 server openssl[18344]: Doing md5 for 3s on 64 size blocks: 10281058 md5's in 3.00s
停止命令
$ sudo systemctl stop run-benchmark
Rails 长期支持 Rails LTS 在你的 Ruby on Rails 版本(2.3、3.2、4.2、5.2 和 6.1)官方生命周期结束很久之后,仍能通过快速、可靠的更新让你的应用保持安全。
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。