Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

systemd: System Commands #159

Open
dushaoshuai opened this issue Dec 8, 2024 · 0 comments
Open

systemd: System Commands #159

dushaoshuai opened this issue Dec 8, 2024 · 0 comments
Labels
Linux Linux Operating system Operating system systemd System and Service Manager

Comments

@dushaoshuai
Copy link
Owner

dushaoshuai commented Dec 8, 2024

man systemctl

  • COMMANDS
    • System Commands

is-system-running

检查系统是否正常运行,打印系统状态。只有在系统完全启动并且运行,且没有任何失败的服务时,命令才返回成功(退出状态 0)。

$ systemctl is-system-running 
degraded
$ echo $?
1

这表示系统运行,但是有一个或多个服务失败了。查看失败的服务:

$ systemctl --failed
  UNIT                           LOAD   ACTIVE SUB    DESCRIPTION          
● systemd-vconsole-setup.service loaded failed failed Virtual Console Setup

Legend: LOAD   → Reflects whether the unit definition was properly loaded.
        ACTIVE → The high-level unit activation state, i.e. generalization of SUB.
        SUB    → The low-level unit activation state, values depend on unit type.

1 loaded units listed.

解决这个问题后,重启相应服务,再查看系统状态:

$ systemctl is-system-running 
running
$ echo $?
0
$ systemctl --failed
  UNIT LOAD ACTIVE SUB DESCRIPTION

0 loaded units listed.

default / rescue / emergency

Command systemd Target Notes
systemctl default systemctl get-default 系统默认目标,正常运行
systemctl rescue rescue.target single user mode
systemctl emergency emergency.target Emergency shell

执行不同的命令,系统会进入不同的模式/目标,它们的主要区别在于系统加载的服务和功能范围不同。

模式 目的 加载的服务 文件系统 用户环境
(此表由 chatgpt 生成,未经考证)
default 正常运行 全部服务 已挂载 多用户模式(图形或 CLI)
rescue 单用户模式,基本修复 基本服务 已挂载 单用户shell
emergency 最小修复环境 未挂载 单用户shell

查看系统默认的目标:

# 树莓派/aarch64/无图形界面
$ systemctl get-default 
multi-user.target
# 笔记本/x86_64/有图形界面
$ systemctl get-default
graphical.target

multi-user.target 表示系统当前的默认运行目标是命令行多用户模式。graphical.target 表示系统当前的默认运行目标是图形界面模式。

进入 rescue 模式:

sudo systemctl rescue

1733658093609

进入 emergency 模式:

sudo systemctl emergency

1733658093600

电源管理

halt

关闭并停止系统运行,硬件仍会保持通电状态。

在我的笔记本(Manjaro, x86_64)上测试,执行 sudo systemctl halt 命令后,笔记本呼吸灯亮,有线鼠标有电,手机依然可以充电。长按电源键后,笔记本呼吸灯灭,有线鼠标指示灯灭,手机不可以充电。再次按电源键开机。

在树莓派上测试,有线鼠标指示灯灭,手机不可以充电。好像树莓派上 halt 和 poweroff 是一样的

我查了一些资料,但还是没搞懂 halt 的实际用处是什么。

其他电源管理命令

Command 作用
systemctl reboot 关机并重启
systemctl poweroff 关机并断电
systemctl suspend 关闭大部分设备,仅内存供电保存状态
systemctl hibernate 将内存内容保存到硬盘并完全断电
systemctl hybrid-sleep 也称为 suspend-to-both,将内存内容保存到硬盘,然后 suspend
systemctl suspend-then-hibernate 先 suspend,在电池电量过低,或者指定的延迟后,hibernate
systemctl soft-reboot 仅重启用户空间,不涉及固件初始化、内核初始化、initramfs 等。

See also

@dushaoshuai dushaoshuai added Linux Linux Operating system Operating system systemd System and Service Manager labels Dec 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Linux Linux Operating system Operating system systemd System and Service Manager
Projects
None yet
Development

No branches or pull requests

1 participant