PRF:20180306 How To Check All Running Services In Linux.md

@jessie-pang
This commit is contained in:
Xingyu.Wang 2018-08-14 10:06:23 +08:00
parent 8604ec337e
commit db27e7ddfb

View File

@ -1,8 +1,7 @@
如何查看 Linux 中所有正在运行的服务
======
有许多方法和工具可以查看 Linux 中所有正在运行的服务。大多数管理员会在 sysVinit 系统中使用 `service service-name status``/etc/init.d/service-name status`,而在 systemd 系统中使用 `systemctl status service-name`
有许多方法和工具可以查看 Linux 中所有正在运行的服务。大多数管理员会在 System VSysV初始化系统中使用 `service service-name status``/etc/init.d/service-name status`,而在 systemd 初始化系统中使用 `systemctl status service-name`
以上命令可以清楚地显示该服务是否在服务器上运行,这也是每个 Linux 管理员都该知道的非常简单和基础的命令。
@ -10,28 +9,43 @@
是的,我们的确有必要这样检查一下。这将有助于我们了解系统上运行了什么服务,以及哪些是必要的、哪些需要被禁用。
### 什么是 SysVinit
init<ruby>初始化<rt>initialization</rt></ruby>的简称)是在系统启动期间运行的第一个进程。`init` 是一个守护进程,它将持续运行直至关机。
init初始化 initialization 的简称是在系统启动期间运行的第一个进程。Init 是一个守护进程,它将持续运行直至关机。
大多数 Linux 发行版都使用如下的初始化系统之一:
SysVinit 是早期传统的 init 系统和系统管理器。由于 sysVinit 系统上一些长期悬而未决的问题,大多数最新的发行版都适用于 systemd 系统。
- System V 是更老的初始化系统
- Upstart 是一个基于事件的传统的初始化系统的替代品
- systemd 是新的初始化系统,它已经被大多数最新的 Linux 发行版所采用
### 什么是 System VSysV
SysV意即 System V 初始化系统是早期传统的初始化系统和系统管理器。由于 sysVinit 系统上一些长期悬而未决的问题,大多数最新的发行版都适用于 systemd 系统。
### 什么是 Upstart 初始化系统
Upstart 是一个基于事件的 /sbin/init 的替代品,它控制在启动时的任务和服务的开始,在关机时停止它们,并在系统运行时监控它们。
它最初是为 Ubuntu 发行版开发的,但其是以适合所有 Linux 发行版的开发为目标的,以替换过时的 System-V 初始化系统。
### 什么是 systemd
systemd 是一个新的 init 系统以及系统管理器,它已成为大多数 Linux 发行版中非常流行且广泛适应的新的标准 init 系统。Systemctl 是一个 systemd 管理工具,它可以帮助我们管理 systemd 系统。
systemd 是一个新的初始化系统以及系统管理器,它已成为大多数 Linux 发行版中非常流行且广泛适应的新的标准初始化系统。`systemctl` 是一个 systemd 管理工具,它可以帮助我们管理 systemd 系统。
### 方法一:如何在 sysVinit 系统中查看运行的服务
### 方法一:如何在 System VSysV系统中查看运行的服务
以下命令可以帮助我们列出 sysVinit 系统中所有正在运行的服务。
以下命令可以帮助我们列出 System VSysV 系统中所有正在运行的服务。
如果服务很多,我建议使用文件查看命令,如 `less`、`more` 等,以便得到清晰的结果。
如果服务很多,我建议使用文件视图命令,如 less、more 等,以便得到清晰的结果。
```
# service --status-all
or
# service --status-all | more
or
# service --status-all | less
```
```
abrt-ccpp hook is installed
abrtd (pid 2131) is running...
abrt-dump-oops is stopped
@ -99,13 +113,15 @@ svnserve is stopped
vsftpd (pid 4008) is running...
xinetd (pid 2031) is running...
zabbix_agentd (pid 2150 2149 2148 2147 2146 2140) is running...
```
执行以下命令,可以只查看正在运行的服务。
执行以下命令,可以只查看正在运行的服务:
```
# service --status-all | grep running
```
```
crond (pid 535) is running...
httpd (pid 627) is running...
mysqld (pid 911) is running...
@ -116,27 +132,29 @@ sendmail (pid 509) is running...
sm-client (pid 519) is running...
openssh-daemon (pid 478) is running...
xinetd (pid 485) is running...
```
运行以下命令以查看指定服务的状态。
运行以下命令以查看指定服务的状态:
```
# service --status-all | grep httpd
httpd (pid 627) is running...
```
或者,使用以下命令也可以查看指定服务的状态。
或者,使用以下命令也可以查看指定服务的状态:
```
# service httpd status
httpd (pid 627) is running...
```
使用以下命令查看系统启动时哪些服务会被启用。
使用以下命令查看系统启动时哪些服务会被启用:
```
# chkconfig --list
```
```
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
htcacheclean 0:off 1:off 2:off 3:off 4:off 5:off 6:off
httpd 0:off 1:off 2:off 3:on 4:off 5:off 6:off
@ -182,15 +200,47 @@ xinetd based services:
tcpmux-server: off
time-dgram: off
time-stream: off
```
### 方法二:如何在 systemd 系统中查看运行的服务
### 方法二:如何在 System VSysV系统中查看运行的服务
另外一种在 Linux 系统上列出运行的服务的方法是使用 initctl 命令:
```
# initctl list
rc stop/waiting
tty (/dev/tty3) start/running, process 1740
tty (/dev/tty2) start/running, process 1738
tty (/dev/tty1) start/running, process 1736
tty (/dev/tty6) start/running, process 1746
tty (/dev/tty5) start/running, process 1744
tty (/dev/tty4) start/running, process 1742
plymouth-shutdown stop/waiting
control-alt-delete stop/waiting
rcS-emergency stop/waiting
readahead-collector stop/waiting
kexec-disable stop/waiting
quit-plymouth stop/waiting
rcS stop/waiting
prefdm stop/waiting
init-system-dbus stop/waiting
ck-log-system-restart stop/waiting
readahead stop/waiting
ck-log-system-start stop/waiting
splash-manager stop/waiting
start-ttys stop/waiting
readahead-disable-services stop/waiting
ck-log-system-stop stop/waiting
rcS-sulogin stop/waiting
serial stop/waiting
```
### 方法三:如何在 systemd 系统中查看运行的服务
以下命令帮助我们列出 systemd 系统中所有服务:
以下命令帮助我们列出 systemd 系统中所有服务。
```
# systemctl
UNIT LOAD ACTIVE SUB DESCRIPTION
sys-devices-virtual-block-loop0.device loaded active plugged /sys/devices/virtual/block/loop0
sys-devices-virtual-block-loop1.device loaded active plugged /sys/devices/virtual/block/loop1
@ -236,17 +286,15 @@ xinetd based services:
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-Bus System Message Bus
postfix.service loaded active exited Postfix Mail Transport Agent
```
* `UNIT` 相应的 systemd 单元名称
* `LOAD` 相应的单元是否被加载到内存中
* `ACTIVE` 该单元是否处于活动状态
* `SUB` 该单元是否处于运行状态LCTT 译注:是较于 ACTIVE 更加详细的状态描述,不同的单元类型有不同的状态。)
* `DESCRIPTION` 关于该单元的简短描述
* **`UNIT`** 相应的 systemd 单元名称
* **`LOAD`** 相应的单元是否被加载到内存中
* **`ACTIVE`** 该单元是否处于活动状态
* **`SUB`** 该单元是否处于运行状态LCTT 译者注:是较于 ACTIVE 更加详细的状态描述,不同的单元类型有不同的状态。)
* **`DESCRIPTION`** 关于该单元的简短描述
以下选项可根据类型列出单元:
以下选项可根据类型列出单元。
```
# systemctl list-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
@ -267,15 +315,15 @@ xinetd based services:
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-Bus System Message Bus
fwupd.service loaded active running Firmware update daemon
[email protected] loaded active running Getty on tty1
getty@tty1.service loaded active running Getty on tty1
grub-common.service loaded active exited LSB: Record successful boot for GRUB
irqbalance.service loaded active running LSB: daemon to balance interrupts for SMP systems
keyboard-setup.service loaded active exited Set the console keyboard layout
kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel
```
以下选项可帮助您根据状态列出单位,输出与前例类似但更直截了当。
以下选项可帮助您根据状态列出单位,输出与前例类似但更直截了当:
```
# systemctl list-unit-files --type service
@ -288,31 +336,31 @@ alsa-utils.service masked
anacron-resume.service enabled
anacron.service enabled
apache-htcacheclean.service disabled
[email protected] disabled
apache-htcacheclean@.service disabled
apache2.service enabled
[email protected] disabled
apache2@.service disabled
apparmor.service enabled
[email protected] static
apport-forward@.service static
apport.service generated
apt-daily-upgrade.service static
apt-daily.service static
aptik-battery-monitor.service generated
atop.service enabled
atopacct.service enabled
[email protected] enabled
autovt@.service enabled
avahi-daemon.service enabled
bluetooth.service enabled
```
运行以下命令以查看指定服务的状态。
运行以下命令以查看指定服务的状态:
```
# systemctl | grep apache2
apache2.service loaded active running The Apache HTTP Server
```
或者,使用以下命令也可查看指定服务的状态。
或者,使用以下命令也可查看指定服务的状态:
```
# systemctl status apache2
● apache2.service - The Apache HTTP Server
@ -334,10 +382,9 @@ Mar 06 12:34:09 magi-VirtualBox systemd[1]: Started The Apache HTTP Server.
Mar 06 12:39:10 magi-VirtualBox systemd[1]: Reloading The Apache HTTP Server.
Mar 06 12:39:10 magi-VirtualBox apachectl[2786]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using fe80::7929:4ed1:279f:4d65. Set the 'ServerName' directive gl
Mar 06 12:39:10 magi-VirtualBox systemd[1]: Reloaded The Apache HTTP Server.
```
执行以下命令,只查看正在运行的服务
执行以下命令,只查看正在运行的服务
```
# systemctl | grep running
acpid.path loaded active running ACPI Events Check
@ -357,16 +404,16 @@ Mar 06 12:39:10 magi-VirtualBox systemd[1]: Reloaded The Apache HTTP Server.
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-Bus System Message Bus
fwupd.service loaded active running Firmware update daemon
[email protected] loaded active running Getty on tty1
getty@tty1.service loaded active running Getty on tty1
irqbalance.service loaded active running LSB: daemon to balance interrupts for SMP systems
lightdm.service loaded active running Light Display Manager
ModemManager.service loaded active running Modem Manager
NetworkManager.service loaded active running Network Manager
polkit.service loaded active running Authorization Manager
```
使用以下命令查看系统启动时会被启用的服务列表。
使用以下命令查看系统启动时会被启用的服务列表:
```
# systemctl list-unit-files | grep enabled
acpid.path enabled
@ -378,7 +425,7 @@ apache2.service enabled
apparmor.service enabled
atop.service enabled
atopacct.service enabled
[email protected] enabled
autovt@.service enabled
avahi-daemon.service enabled
bluetooth.service enabled
console-setup.service enabled
@ -388,7 +435,7 @@ cups.service enabled
display-manager.service enabled
dns-clean.service enabled
friendly-recovery.service enabled
[email protected] enabled
getty@.service enabled
gpu-manager.service enabled
keyboard-setup.service enabled
lightdm.service enabled
@ -398,10 +445,10 @@ networking.service enabled
NetworkManager-dispatcher.service enabled
NetworkManager-wait-online.service enabled
NetworkManager.service enabled
```
systemd-cgtop 按资源使用情况任务、CPU、内存、输入和输出列出控制组。
`systemd-cgtop` 按资源使用情况任务、CPU、内存、输入和输出列出控制组
```
# systemd-cgtop
@ -432,13 +479,13 @@ Control Group Tasks %CPU Memory Input/s Output/s
/system.slice/rtkit-daemon.service 3 - - - -
/system.slice/snapd.service 8 - - - -
/system.slice/system-getty.slice 1 - - - -
```
同时,我们可以使用 pstree 命令(输出来自 SysVinit 系统)查看正在运行的服务。
同时,我们可以使用 `pstree` 命令(输出来自 SysVinit 系统)查看正在运行的服务:
```
# pstree
init-|-crond
init-+-crond
|-httpd---2*[httpd]
|-kthreadd/99149---khelper/99149
|-2*[mingetty]
@ -449,10 +496,10 @@ init-|-crond
|-sshd---sshd---bash---pstree
|-udevd
`-xinetd
```
我们还可以使用 pstree 命令(输出来自 systemd 系统)查看正在运行的服务。
我们还可以使用 `pstree` 命令(输出来自 systemd 系统)查看正在运行的服务:
```
# pstree
systemd─┬─ModemManager─┬─{gdbus}
@ -485,20 +532,20 @@ systemd─┬─ModemManager─┬─{gdbus}
├─gnome-keyring-d─┬─{gdbus}
│ ├─{gmain}
│ └─{timer}
```
### 方法三:如何使用 chkservice 在 systemd 系统中查看正在运行的服务
### 方法四:如何使用 chkservice 在 systemd 系统中查看正在运行的服务
`chkservice` 是一个管理系统单元的终端工具,需要超级用户权限。
chkservice 是一个管理系统单元的终端工具,需要超级用户权限。
```
# chkservice
```
![][1]
要查看帮助页面,请单击 `?` 按钮,它将显示管理 systemd 服务的可用选项。
要查看帮助页面,请按下 `?` ,它将显示管理 systemd 服务的可用选项。
![][2]
--------------------------------------------------------------------------------
@ -507,7 +554,7 @@ via: https://www.2daygeek.com/how-to-check-all-running-services-in-linux/
作者:[Magesh Maruthamuthu][a]
译者:[jessie-pang](https://github.com/jessie-pang)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出