4

如何列出Linux Systemd服务

 1 year ago
source link: https://www.myfreax.com/systemd-list-services/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

如何列出Linux Systemd服务

尽管systemd解决了许多系统管理问题,但有时也令人困惑。即使是列出systemd服务之类的简单任务也会让您思考

Updated At 24 Aug 2022

5 min read

By myfreax
如何列出Linux Systemd服务

尽管systemd解决了许多系统管理问题,但有时也令人困惑。即使是列出systemd服务之类的简单任务也会让您思考。

在本教程中,我将向您展示如何列出systemd服务。我还将解释systemd服务的状态,以便您更容易理解服务的不同状态。

systemd服务由systemctl命令管理。如果你systemctl不带任何参数运行,它会调用默认list-units子命令,并列出各种类型的systemd单元unit,如服务service、套接字socket、目标target等。

如果你只是想列出指定类型信息,可以使用systemctl的--type选项,例如命令systemctl --type=service将会列出Linux系统的所有服务。

systemctl --type=service
systemctl list-units --type=service

在这两个命令输出是相同的。默认情况下,此命令仅按字母顺序显示已加载和正在运行的服务:

  UNIT                     LOAD   ACTIVE SUB     DESCRIPTION                                                       
  accounts-daemon.service  loaded active running Accounts Service                                                  
  acpid.service            loaded active running ACPI event daemon                                                 
  alsa-restore.service     loaded active exited  Save/Restore Sound Card State                                     
  apparmor.service         loaded active exited  Load AppArmor profiles                                            
  apport.service           loaded active exited  LSB: automatic crash report generation

UNIT是systemd单元的名称。LOAD是单元配置文件是否被systemd解析过。ACTIVE 单元处于活动状态。

SUB是单元的低电平状态。活动单元可以处于运行状态或退出状态。此值取决于服务类型。

如您所见,您可以列出Linux系统上已加载的服务。但是活动的systemd服务可能正在运行或可能退出状态。

列出systemd服务

默认情况下,systemctl命令只显示已加载的和活动的服务单元。服务,套接字等都有一个状态,如运行、退出等。

您可以使用systemctl的--state选项列出不同状态服务或者套接字。例如要列出正在运行的systemd服务,请使用--state=running标识。

命令sudo systemctl list-units --type=service --state=running将会列出正在运行的服务。

如果你需要列出所有已加载的服务,包括不活动的服务,请使用--all标识。你也可以像这样组合各种--all--state标识列出服务。

每个systemd单元,无论是服务、套接字还是任何其他单元,都有一个单元文件。即使它们没有加载。

你可以运行命令sudo systemctl list-unit-files --type=service 列出在Linux系统上已安装的服务。

sudo systemctl list-units --type=service --state=running
systemctl list-units --all --type=service
systemctl list-units --all --type=service --state=inactive
systemctl list-unit-files --type=service

列出自动启动的systemd服务

如需要查看系统启动时将自动运行那些systemd服务,你可以运行命令sudo systemctl list-unit-files --type=service --state=enabled

systemctl list-unit-files --type=service --state=enabled

许多新用户将启用的系统服务与正在运行的服务混淆。但是,启用systemd服务意味着该服务将在系统启动时自动启动。

UNIT FILE                                  STATE   VENDOR PRESET
mfreax.com.service                         enabled enabled      

你可能还会注意到字段VENDOR PRESET供应商预设。它也出现在前面的示例中。供应商预设定义了安装程序时systemd单元的行为。

假设您安装了一个新程序Nginx。在安装Nginx时,Nginx软件包将会被解压到指定的目录,其中nginx.service将会被解压到/lib/systemd/system/nginx.service

nginx.service将会被设置为自动启动,这就是供应商预设。这意味着在安装程序后,当你启动系统时,nginx.service将在启动时自动激活。

如果供应商预设是禁用disable的,您必须手动将其设置为启用。一旦启用,它会在每次启动时自动启动。

sudo systemd enable nginx.service

systemd服务状态

到目前为止,您已经看到了列出服务的各种方式。但没有什么专注于单一服务。

您可以使用systemctl的子命令status,获取systemd服务的详细信息。如果你忘记服务的全称你可以使用制表符tab补全sudo systemctl status命令。

在查看服务状态时,可以不输入服务的扩展名称,例如查看Nginx服务的状态,你可以只输入nginx,也可以是nginx.service。套接子也可以省略.socket

systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-08-24 08:02:17 CST; 8h ago
       Docs: man:nginx(8)
    Process: 795 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 829 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 830 (nginx)
      Tasks: 6 (limit: 19036)
     Memory: 12.2M
     CGroup: /system.slice/nginx.service
             ├─830 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─831 nginx: worker process
             ├─832 nginx: worker process
             ├─833 nginx: worker process
             ├─834 nginx: worker process
             └─835 nginx: cache manager process

8月 24 08:02:17 myfreax systemd[1]: Starting A high performance web server and a reverse proxy server...
8月 24 08:02:17 myfreax systemd[1]: Started A high performance web server and a reverse proxy server.

您可以看到它提供了大量有用的信息,例如服务的状态、服务的手册页、占用内存、进程ID等。它还显示了服务的最后几行日志

如果要在shell脚本中检查服务是否处于活动状态,可以使用is-active子命令。systemctl is-active nginx.service退出代码为0时表示激活。

同样,如果要检查服务是否启用,设置为开机自动启动,可以使用is-enabled子命令。systemctl is-enabled nginx.service退出代码为0时表示服务自动启动。

您现在对列出systemd服务有了更好的理解。如你有任何问题请在下面发表评论,提供您的反馈、问题或建议。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK