有许多方法和工具可以查看 Linux 中所有正在运行的服务。大多数管理员会在 System V(SysV)初始化系统中使用 service service-name status 或 /etc/init.d/service-name status,而在 systemd 初始化系统中使用 systemctl status service-name。
以上命令可以清楚地显示该服务是否在服务器上运行,这也是每个 Linux 管理员都该知道的非常简单和基础的命令。
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: Firewall is not running. irqbalance (pid 1826) is running... Kdump is 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 module not 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...
执行以下命令,可以只查看正在运行的服务:
1 2
# service --status-all | grep running
1 2 3 4 5 6 7 8 9 10 11
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...
运行以下命令以查看指定服务的状态:
1 2 3
# service --status-all | grep httpd httpd (pid 627) isrunning...
或者,使用以下命令也可以查看指定服务的状态:
1 2 3
# service httpd status httpd (pid 627) isrunning...
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
# 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 82540EM Gigabit Ethernet Controller (PRO/1000 MT Desktop Adapter) -.mount loaded active mounted Root Mount dev-hugepages.mount loaded active mounted Huge Pages File System dev-mqueue.mount loaded active mounted POSIX Message Queue File System 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-termius\x2dapp-8.mount loaded active mounted Mount unit for termius-app sys-fs-fuse-connections.mount loaded active mounted FUSE Control File System sys-kernel-debug.mount loaded active mounted Debug File System acpid.path loaded active running ACPI Events Check cups.path loaded active running CUPS Scheduler systemd-ask-password-plymouth.path loaded active waiting Forward Password Requests to Plymouth Directory Watch systemd-ask-password-wall.path loaded active waiting Forward Password Requests to Wall Directory Watch init.scope loaded active running System and Service Manager session-c2.scope loaded active running Session c2 of user magi accounts-daemon.service loaded active running Accounts Service acpid.service loaded active running ACPI event daemon anacron.service loaded active running Run anacron jobs apache2.service loaded active running The Apache 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, Install and Generate Color Profiles 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-Bus System Message Bus postfix.service loaded active exited Postfix Mail Transport Agent
UNIT 相应的 systemd 单元名称
LOAD 相应的单元是否被加载到内存中
ACTIVE 该单元是否处于活动状态
SUB 该单元是否处于运行状态(LCTT 译注:是较于 ACTIVE 更加详细的状态描述,不同的单元类型有不同的状态。)
# 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 anacron.service loaded active running Run anacron jobs apache2.service loaded active running The Apache 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, Install and Generate Color Profiles 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-Bus System Message Bus 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 Create list of required static device nodes for the current kernel
# systemctl | grep running acpid.path loaded active running ACPI Events Check cups.path loaded active running CUPS Scheduler init.scope loaded active running System and Service Manager session-c2.scope loaded active running Session c2 of user magi accounts-daemon.service loaded active running Accounts Service acpid.service loaded active running ACPI event daemon apache2.service loaded active running The Apache 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, Install and Generate Color Profiles 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-Bus System Message Bus 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 Light Display Manager ModemManager.service loaded active running Modem Manager NetworkManager.service loaded active running Network Manager polkit.service loaded active running Authorization Manager