用 systemd-inhibit 阻止系统休眠

给长时间任务加上“防休眠锁”,下载或备份再久也不会被打断。

Linux睡眠电源与睡眠systemd低风险
在命令前加 systemd-inhibit --why='Downloading large file' --mode=block,任务运行期间系统不会自动休眠。

01命令

命令
$ systemd-inhibit --why='Downloading large file' --mode=block curl -LO https://example.com/big.iso

02作用说明

桌面空闲一段时间就自动挂起,长下载、rsync 或性能测试经常被中途掐断。systemd-inhibit 会在被包裹命令运行期间持有和播放器一样的电源管理锁,--why 里的说明文字会显示在锁列表中。不带参数运行 systemd-inhibit 可以查看当前谁在阻止休眠。

03风险

风险低风险
使用条件systemd-based distro; no root needed. Also affects lid close handling when used with delay/block modes.
!注意: block 模式只能阻止自动挂起——合上笔记本盖子仍可能休眠,取决于 logind 的 HandleLidSwitch 设置。被包裹的命令若卡住,机器会一直不睡,建议只用于有明确结束时间的任务。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • systemd-inhibit(1) - Ubuntu manpage
    Ubuntu Manpages · community · 2026-09-17
    依据: Same synopsis; explains that --mode=block takes the lock for the duration of the wrapped command and --why= sets a human-readable reason.
  • systemd-inhibit(1) — Arch Manual Pages
    Arch Linux · community · 2026-09-17
    依据: Documents systemd-inhibit [OPTIONS...] [COMMAND] with --what=, --who=, --why= and --mode= (block/delay) options.

最后验证: 2026-09-19 · 分类: 系统与设置

06相关方案