You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux中reboot与poweroff映射至systemctl的执行逻辑问询

systemctl如何识别reboot/poweroff并执行关机重启操作

问题

在Linux系统中,reboot与poweroff均是指向/bin/systemctl的符号链接,请问:

  1. systemctl是如何控制关机与重启操作的?
  2. 输入reboot或poweroff命令时,systemctl是如何识别并执行对应命令的?

验证信息

通过以下命令可确认reboot和poweroff的符号链接指向:

➜  /usr/bin file /usr/sbin/reboot
/usr/sbin/reboot: symbolic link to /bin/systemctl
➜  /usr/bin file /usr/sbin/poweroff 
/usr/sbin/poweroff: symbolic link to /bin/systemctl
➜  /usr/bin uname -a
Linux mi-OptiPlex-7080 5.15.0-76-generic #83~20.04.1-Ubuntu SMP Wed Jun 21 20:23:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

原理说明

1. systemctl如何识别对应命令

当通过符号链接(比如reboot)调用/bin/systemctl时,程序会读取进程的argv[0]参数——也就是你在终端输入的命令名。systemctl内部会根据这个名称匹配预设的操作:

  • 如果argv[0]是reboot,就执行重启流程
  • 如果argv[0]是poweroff,就执行关机流程

简单来说,systemctl会“看自己被叫做什么名字”,然后做对应的事。

2. systemctl控制关机/重启的流程

systemctl是systemd系统管理器的前端工具,它通过触发systemd的**目标单元(target)**来完成操作:

  • 执行reboot时,systemctl会激活reboot.target,systemd会按预设顺序:停止所有正在运行的服务、卸载挂载的文件系统、向内核发送重启指令,最终完成系统重启
  • 执行poweroff时,systemctl会激活poweroff.target,systemd会完成停止服务、卸载文件系统后,向硬件发送断电指令,实现系统关机

整个过程由systemd统一调度,确保系统安全、有序地完成状态切换。

内容的提问来源于stack exchange,提问作者Junbo Zheng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:22:42