2021-05-12 14:32:11
如何檢視 Linux 中所有正在執行的服務
有許多方法和工具可以檢視 Linux 中所有正在執行的服務。大多數管理員會在 System V(SysV)初始化系統中使用 service service-name status
或 /etc/init.d/service-name status
,而在 systemd 初始化系統中使用 systemctl status service-name
。
以上命令可以清楚地顯示該服務是否在伺服器上執行,這也是每個 Linux 管理員都該知道的非常簡單和基礎的命令。
如果你對系統環境並不熟悉,也不清楚系統在執行哪些服務,你會如何檢查?
是的,我們的確有必要這樣檢查一下。這將有助於我們了解系統上執行了什麼服務,以及哪些是必要的、哪些需要被禁用。
init(初始化initialization的簡稱)是在系統啟動期間執行的第一個進程。init
是一個守護行程,它將持續執行直至關機。
大多數 Linux 發行版都使用如下的初始化系統之一:
- System V 是更老的初始化系統
- Upstart 是一個基於事件的傳統的初始化系統的替代品
- systemd 是新的初始化系統,它已經被大多數最新的 Linux 發行版所採用
什麼是 System V(SysV)
SysV(意即 System V) 初始化系統是早期傳統的初始化系統和系統管理器。由於 sysVinit 系統上一些長期懸而未決的問題,大多數最新的發行版都適用於 systemd 系統。
什麼是 Upstart 初始化系統
Upstart 是一個基於事件的 /sbin/init 的替代品,它控制在啟動時的任務和服務的開始,在關機時停止它們,並在系統執行時監控它們。
它最初是為 Ubuntu 發行版開發的,但其是以適合所有 Linux 發行版的開發為目標的,以替換過時的 System-V 初始化系統。
什麼是 systemd
systemd 是一個新的初始化系統以及系統管理器,它已成為大多數 Linux 發行版中非常流行且廣泛適應的新的標準初始化系統。systemctl
是一個 systemd 管理工具,它可以幫助我們管理 systemd 系統。
方法一:如何在 System V(SysV)系統中檢視執行的服務
以下命令可以幫助我們列出 System V(SysV) 系統中所有正在執行的服務。
如果服務很多,我建議使用檔案檢視命令,如 less
、more
等,以便得到清晰的結果。
# service --status-all
或
# service --status-all |more
或
# service --status-all |less
abrt-ccpp hook is installed
abrtd (pid 2131)is running...
abrt-dump-oops is stopped
acpid (pid 1958)is running...
atd (pid 2164)is running...
auditd (pid 1731)is running...
Frequency scaling enabled using ondemand governor
crond (pid 2153)is running...
hald (pid 1967)is running...
htcacheclean is stopped
httpd is stopped
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all ::/0 ::/0 state RELATED,ESTABLISHED
2 ACCEPT icmpv6 ::/0 ::/0
3 ACCEPT all ::/0 ::/0
4 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:80
5 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:21
6 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:22
7 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:25
8 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:2082
9 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:2086
10 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:2083
11 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:2087
12 ACCEPT tcp ::/0 ::/0 state NEW tcp dpt:10000
13 REJECT all ::/0 ::/0 reject-with icmp6-adm-prohibited
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all ::/0 ::/0 reject-with icmp6-adm-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
iptables:Firewallisnot running.
irqbalance (pid 1826)is running...
Kdumpis operational
lvmetad is stopped
mdmonitor is stopped
messagebus (pid 1929)is running...
SUCCESS!MySQL running (24376)
rndc: neither /etc/rndc.conf nor /etc/rndc.key was found
named is stopped
netconsole modulenot loaded
Usage: startup.sh { start | stop }
Configured devices:
lo eth0 eth1
Currently active devices:
lo eth0
ntpd is stopped
portreserve (pid 1749)is running...
master (pid 2107)is running...
Process accounting is disabled.
quota_nld is stopped
rdisc is stopped
rngd is stopped
rpcbind (pid 1840)is running...
rsyslogd(pid 1756)is running...
sandbox is stopped
saslauthd is stopped
smartd is stopped
openssh-daemon (pid 9859)is running...
svnserve is stopped
vsftpd (pid 4008)is running...
xinetd (pid 2031)is running...
zabbix_agentd (pid 215021492148214721462140)is running...
執行以下命令,可以只檢視正在執行的服務:
# service --status-all |grep running
crond (pid 535)is running...
httpd (pid 627)is running...
mysqld (pid 911)is running...
rndc: neither /etc/rndc.conf nor /etc/rndc.key was found
rsyslogd(pid 449)is running...
saslauthd (pid 492)is running...
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
ip6tables 0:off 1:off 2:on 3:off 4:on 5:on 6:off
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
modules_dep 0:off 1:off 2:on 3:on 4:on 5:on 6:off
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
named 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
netfs 0:off 1:off 2:off 3:off 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nmb 0:off 1:off 2:off 3:off 4:off 5:off 6:off
nscd0:off 1:off 2:off 3:off 4:off 5:off 6:off
portreserve 0:off 1:off 2:on 3:off 4:on 5:on 6:off
quota_nld 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off
restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rpcbind 0:off 1:off 2:on 3:off 4:on 5:on 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
saslauthd 0:off 1:off 2:off 3:on 4:off 5:off 6:off
sendmail 0:off 1:off 2:on 3:on 4:on 5:on 6:off
smb 0:off 1:off 2:off 3:off 4:off 5:off 6:off
snmpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
snmptrapd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sshd0:off 1:off 2:on 3:on 4:on 5:on 6:off
udev-post 0:off 1:on 2:on 3:off 4:on 5:on 6:off
winbind 0:off 1:off 2:off 3:off 4:off 5:off 6:off
xinetd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
xinetd based services:
chargen-dgram: off
chargen-stream: off
daytime-dgram: off
daytime-stream: off
discard-dgram: off
discard-stream: off
echo-dgram: off
echo-stream: off
finger: off
ntalk: off
rsync: off
talk: off
tcpmux-server: off
time-dgram: off
time-stream: off
方法二:如何在 System V(SysV)系統中檢視執行的服務
另外一種在 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 系統中所有服務:
#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
sys-devices-virtual-block-loop2.device loaded active plugged /sys/devices/virtual/block/loop2
sys-devices-virtual-block-loop3.device loaded active plugged /sys/devices/virtual/block/loop3
sys-devices-virtual-block-loop4.device loaded active plugged /sys/devices/virtual/block/loop4
sys-devices-virtual-misc-rfkill.device loaded active plugged /sys/devices/virtual/misc/rfkill
sys-devices-virtual-tty-ttyprintk.device loaded active plugged /sys/devices/virtual/tty/ttyprintk
sys-module-fuse.device loaded active plugged /sys/module/fuse
sys-subsystem-net-devices-enp0s3.device loaded active plugged 82540EMGigabitEthernetController(PRO/1000 MT DesktopAdapter)
-.mount loaded active mounted RootMount
dev-hugepages.mount loaded active mounted HugePagesFileSystem
dev-mqueue.mount loaded active mounted POSIX MessageQueueFileSystem
run-user-1000-gvfs.mount loaded active mounted /run/user/1000/gvfs
run-user-1000.mount loaded active mounted /run/user/1000
snap-core-3887.mount loaded active mounted Mount unit for core
snap-core-4017.mount loaded active mounted Mount unit for core
snap-core-4110.mount loaded active mounted Mount unit for core
snap-gping-13.mount loaded active mounted Mount unit for gping
snap-termiusx2dapp-8.mount loaded active mounted Mount unit for termius-app
sys-fs-fuse-connections.mount loaded active mounted FUSE ControlFileSystem
sys-kernel-debug.mount loaded active mounted DebugFileSystem
acpid.path loaded active running ACPI EventsCheck
cups.path loaded active running CUPS Scheduler
systemd-ask-password-plymouth.path loaded active waiting ForwardPasswordRequests to PlymouthDirectoryWatch
systemd-ask-password-wall.path loaded active waiting ForwardPasswordRequests to WallDirectoryWatch
init.scope loaded active running SystemandServiceManager
session-c2.scope loaded active running Session c2 of user magi
accounts-daemon.service loaded active running AccountsService
acpid.service loaded active running ACPI event daemon
anacron.service loaded active running Run anacron jobs
apache2.service loaded active running TheApache HTTP Server
apparmor.service loaded active exited AppArmor initialization
apport.service loaded active exited LSB: automatic crash report generation
aptik-battery-monitor.service loaded active running LSB: start/stop the aptik battery monitor daemon
atop.service loaded active running Atop advanced performance monitor
atopacct.service loaded active running Atop process accounting daemon
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
colord.service loaded active running Manage,InstallandGenerateColorProfiles
console-setup.service loaded active exited Set console font and keymap
cron.service loaded active running Regular background program processing daemon
cups-browsed.service loaded active running Make remote CUPS printers available locally
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-BusSystemMessageBus
postfix.service loaded active exited PostfixMailTransportAgent
UNIT
相應的 systemd 單元名稱LOAD
相應的單元是否被載入到記憶體中ACTIVE
該單元是否處於活動狀態SUB
該單元是否處於執行狀態(LCTT 譯註:是較於 ACTIVE 更加詳細的狀態描述,不同的單元型別有不同的狀態。)DESCRIPTION
關於該單元的簡短描述
以下選項可根據型別列出單元:
#systemctllist-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
accounts-daemon.service loaded active running AccountsService
acpid.service loaded active running ACPI event daemon
anacron.service loaded active running Run anacron jobs
apache2.service loaded active running TheApache HTTP Server
apparmor.service loaded active exited AppArmor initialization
apport.service loaded active exited LSB: automatic crash report generation
aptik-battery-monitor.service loaded active running LSB: start/stop the aptik battery monitor daemon
atop.service loaded active running Atop advanced performance monitor
atopacct.service loaded active running Atop process accounting daemon
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
colord.service loaded active running Manage,InstallandGenerateColorProfiles
console-setup.service loaded active exited Set console font and keymap
cron.service loaded active running Regular background program processing daemon
cups-browsed.service loaded active running Make remote CUPS printers available locally
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-BusSystemMessageBus
fwupd.service loaded active running Firmware update daemon
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 Createlist of required static device nodes for the current kernel
以下選項可幫助您根據狀態列出單位,輸出與前例類似但更直截了當:
#systemctllist-unit-files --type service
UNIT FILE STATE
accounts-daemon.service enabled
acpid.service disabled
alsa-restore.service static
alsa-state.service static
alsa-utils.service masked
anacron-resume.service enabled
anacron.service enabled
apache-htcacheclean.service disabled
apache-htcacheclean@.service disabled
apache2.service enabled
apache2@.service disabled
apparmor.service enabled
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
autovt@.service enabled
avahi-daemon.service enabled
bluetooth.service enabled
執行以下命令以檢視指定服務的狀態:
#systemctl|grep apache2
apache2.service loaded active running TheApache HTTP Server
或者,使用以下命令也可檢視指定服務的狀態:
#systemctl status apache2
● apache2.service -TheApache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Drop-In:/lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Tue2018-03-0612:34:09 IST;8min ago
Process:2786ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
Main PID:1171(apache2)
Tasks:55(limit:4915)
CGroup:/system.slice/apache2.service
├─1171/usr/sbin/apache2 -k start
├─2790/usr/sbin/apache2 -k start
└─2791/usr/sbin/apache2 -k start
Mar0612:34:08 magi-VirtualBoxsystemd[1]:StartingTheApache HTTP Server...
Mar0612:34:09 magi-VirtualBox apachectl[1089]: AH00558: apache2:Couldnot reliably determine the server's fully qualified domain name, using 10.0.2.15. Set the 'ServerName' directive globally to suppre
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
Mar0612:39:10 magi-VirtualBoxsystemd[1]:ReloadedTheApache HTTP Server.
執行以下命令,只檢視正在執行的服務:
#systemctl|grep running
acpid.path loaded active running ACPI EventsCheck
cups.path loaded active running CUPS Scheduler
init.scope loaded active running SystemandServiceManager
session-c2.scope loaded active running Session c2 of user magi
accounts-daemon.service loaded active running AccountsService
acpid.service loaded active running ACPI event daemon
apache2.service loaded active running TheApache HTTP Server
aptik-battery-monitor.service loaded active running LSB: start/stop the aptik battery monitor daemon
atop.service loaded active running Atop advanced performance monitor
atopacct.service loaded active running Atop process accounting daemon
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
colord.service loaded active running Manage,InstallandGenerateColorProfiles
cron.service loaded active running Regular background program processing daemon
cups-browsed.service loaded active running Make remote CUPS printers available locally
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-BusSystemMessageBus
fwupd.service loaded active running Firmware update daemon
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 LightDisplayManager
ModemManager.service loaded active running ModemManager
NetworkManager.service loaded active running NetworkManager
polkit.service loaded active running AuthorizationManager
使用以下命令檢視系統啟動時會被啟用的服務列表:
#systemctllist-unit-files |grep enabled
acpid.path enabled
cups.path enabled
accounts-daemon.service enabled
anacron-resume.service enabled
anacron.service enabled
apache2.service enabled
apparmor.service enabled
atop.service enabled
atopacct.service enabled
autovt@.service enabled
avahi-daemon.service enabled
bluetooth.service enabled
console-setup.service enabled
cron.service enabled
cups-browsed.service enabled
cups.service enabled
display-manager.service enabled
dns-clean.service enabled
friendly-recovery.service enabled
getty@.service enabled
gpu-manager.service enabled
keyboard-setup.service enabled
lightdm.service enabled
ModemManager.service enabled
network-manager.service enabled
networking.service enabled
NetworkManager-dispatcher.service enabled
NetworkManager-wait-online.service enabled
NetworkManager.service enabled
systemd-cgtop
按資源使用情況(任務、CPU、記憶體、輸入和輸出)列出控制組:
#systemd-cgtop
ControlGroupTasks%CPU MemoryInput/s Output/s
/--1.5G--
/init.scope 1----
/system.slice 153----
/system.slice/ModemManager.service 3----
/system.slice/NetworkManager.service 4----
/system.slice/accounts-daemon.service 3----
/system.slice/acpid.service 1----
/system.slice/apache2.service 55----
/system.slice/aptik-battery-monitor.service 1----
/system.slice/atop.service 1----
/system.slice/atopacct.service 1----
/system.slice/avahi-daemon.service 2----
/system.slice/colord.service 3----
/system.slice/cron.service 1----
/system.slice/cups-browsed.service 3----
/system.slice/cups.service 2----
/system.slice/dbus.service 6----
/system.slice/fwupd.service 5----
/system.slice/irqbalance.service 1----
/system.slice/lightdm.service 7----
/system.slice/polkit.service 3----
/system.slice/repowerd.service 14----
/system.slice/rsyslog.service 4----
/system.slice/rtkit-daemon.service 3----
/system.slice/snapd.service 8----
/system.slice/system-getty.slice 1----
同時,我們可以使用 pstree
命令(輸出來自 SysVinit 系統)檢視正在執行的服務:
#pstree
init-+-crond
|-httpd---2*[httpd]
|-kthreadd/99149---khelper/99149
|-2*[mingetty]
|-mysqld_safe---mysqld---9*[{mysqld}]
|-rsyslogd---3*[{rsyslogd}]
|-saslauthd---saslauthd
|-2*[sendmail]
|-sshd---sshd---bash---pstree
|-udevd
`-xinetd
我們還可以使用 pstree
命令(輸出來自 systemd 系統)檢視正在執行的服務:
#pstree
systemd─┬─ModemManager─┬─{gdbus}
│└─{gmain}
├─NetworkManager─┬─dhclient
│├─{gdbus}
│└??{gmain}
├─accounts-daemon─┬─{gdbus}
│└─{gmain}
├─acpid
├─agetty
├─anacron
├─apache2───2*[apache2───26*[{apache2}]]
├─aptd───{gmain}
├─aptik-battery-m
├─atop
├─atopacctd
├─avahi-daemon───avahi-daemon
├─colord─┬─{gdbus}
│└─{gmain}
├─cron
├─cups-browsed─┬─{gdbus}
│└─{gmain}
├─cupsd
├─dbus-daemon
├─fwupd─┬─{GUsbEventThread}
│├─{fwupd}
│├─{gdbus}
│└─{gmain}
├─gnome-keyring-d─┬─{gdbus}
│├─{gmain}
│└─{timer}
方法四:如何使用 chkservice 在 systemd 系統中檢視正在執行的服務
chkservice
是一個管理系統單元的終端工具,需要超級使用者許可權。
# chkservice
要檢視幫助頁面,請按下 ?
,它將顯示管理 systemd 服務的可用選項。
via: https://www.2daygeek.com/how-to-check-all-running-services-in-linux/
作者:Magesh Maruthamuthu 譯者:jessie-pang 校對:wxy
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx
本文永久更新連結地址:https://www.linuxidc.com/Linux/2018-08/153512.htm
相關文章