linux系统查看服务运行,如何查看 Linux 中所有正在运行的服务

91264f99b0c4c787a328cc21f7cfe452.png

有许多方法和工具可以查看 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|lessabrt-ccpp hookisinstalled

abrtd(pid2131)isrunning...

abrt-dump-oopsisstopped

acpid(pid1958)isrunning...

atd(pid2164)isrunning...

auditd(pid1731)isrunning...

Frequencyscaling enabledusingondemand governor

crond(pid2153)isrunning...

hald(pid1967)isrunning...

htcachecleanisstopped

httpdisstopped

Table:filter

ChainINPUT(policy ACCEPT)

num target prot opt source destination

1ACCEPT all::/0 ::/0state RELATED,ESTABLISHED

2ACCEPT icmpv6::/0 ::/0

3ACCEPT all::/0 ::/0

4ACCEPT tcp::/0 ::/0state NEW tcp dpt:80

5ACCEPT tcp::/0 ::/0state NEW tcp dpt:21

6ACCEPT tcp::/0 ::/0state NEW tcp dpt:22

7ACCEPT tcp::/0 ::/0state NEW tcp dpt:25

8ACCEPT tcp::/0 ::/0state NEW tcp dpt:2082

9ACCEPT tcp::/0 ::/0state NEW tcp dpt:2086

10ACCEPT tcp::/0 ::/0state NEW tcp dpt:2083

11ACCEPT tcp::/0 ::/0state NEW tcp dpt:2087

12ACCEPT tcp::/0 ::/0state NEW tcp dpt:10000

13REJECT all::/0 ::/0reject-withicmp6-adm-prohibited

ChainFORWARD(policy ACCEPT)

num target prot opt source destination

1REJECT all::/0 ::/0reject-withicmp6-adm-prohibited

ChainOUTPUT(policy ACCEPT)

num target prot opt source destination

iptables:Firewallisnotrunning.

irqbalance(pid1826)isrunning...

Kdumpisoperational

lvmetadisstopped

mdmonitorisstopped

messagebus(pid1929)isrunning...

SUCCESS!MySQLrunning(24376)

rndc:neither/etc/rndc.conf nor/etc/rndc.key was found

namedisstopped

netconsolemodulenotloaded

Usage:startup.sh{start|stop}

Configureddevices:

lo eth0 eth1

Currentlyactive devices:

lo eth0

ntpdisstopped

portreserve(pid1749)isrunning...

master(pid2107)isrunning...

Processaccountingisdisabled.

quota_nldisstopped

rdiscisstopped

rngdisstopped

rpcbind(pid1840)isrunning...

rsyslogd(pid1756)isrunning...

sandboxisstopped

saslauthdisstopped

smartdisstopped

openssh-daemon(pid9859)isrunning...

svnserveisstopped

vsftpd(pid4008)isrunning...

xinetd(pid2031)isrunning...

zabbix_agentd(pid215021492148214721462140)isrunning...

执行以下命令,可以只查看正在运行的服务:

#service--status-all|greprunningcrond(pid535)isrunning...

httpd(pid627)isrunning...

mysqld(pid911)isrunning...

rndc:neither/etc/rndc.conf nor/etc/rndc.key was found

rsyslogd(pid449)isrunning...

saslauthd(pid492)isrunning...

sendmail(pid509)isrunning...

sm-client(pid519)isrunning...

openssh-daemon(pid478)isrunning...

xinetd(pid485)isrunning...

运行以下命令以查看指定服务的状态:

#service--status-all|grephttpd

httpd(pid627)isrunning...

或者,使用以下命令也可以查看指定服务的状态:

#service httpd status

httpd(pid627)isrunning...

使用以下命令查看系统启动时哪些服务会被启用:

#chkconfig--listcrond0:off1:off2:on3:on4:on5:on6:off

htcacheclean0:off1:off2:off3:off4:off5:off6:off

httpd0:off1:off2:off3:on4:off5:off6:off

ip6tables0:off1:off2:on3:off4:on5:on6:off

iptables0:off1:off2:on3:on4:on5:on6:off

modules_dep0:off1:off2:on3:on4:on5:on6:off

mysqld0:off1:off2:on3:on4:on5:on6:off

named0:off1:off2:off3:off4:off5:off6:off

netconsole0:off1:off2:off3:off4:off5:off6:off

netfs0:off1:off2:off3:off4:on5:on6:off

network0:off1:off2:on3:on4:on5:on6:off

nmb0:off1:off2:off3:off4:off5:off6:off

nscd0:off1:off2:off3:off4:off5:off6:off

portreserve0:off1:off2:on3:off4:on5:on6:off

quota_nld0:off1:off2:off3:off4:off5:off6:off

rdisc0:off1:off2:off3:off4:off5:off6:off

restorecond0:off1:off2:off3:off4:off5:off6:off

rpcbind0:off1:off2:on3:off4:on5:on6:off

rsyslog0:off1:off2:on3:on4:on5:on6:off

saslauthd0:off1:off2:off3:on4:off5:off6:off

sendmail0:off1:off2:on3:on4:on5:on6:off

smb0:off1:off2:off3:off4:off5:off6:off

snmpd0:off1:off2:off3:off4:off5:off6:off

snmptrapd0:off1:off2:off3:off4:off5:off6:off

sshd0:off1:off2:on3:on4:on5:on6:off

udev-post0:off1:on2:on3:off4:on5:on6:off

winbind0:off1:off2:off3:off4:off5:off6:off

xinetd0:off1:off2:off3:on4:on5:on6: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 命令:

#initctllist

rc stop/waiting

tty(/dev/tty3)start/running,process1740

tty(/dev/tty2)start/running,process1738

tty(/dev/tty1)start/running,process1736

tty(/dev/tty6)start/running,process1746

tty(/dev/tty5)start/running,process1744

tty(/dev/tty4)start/running,process1742

plymouth-shutdownstop/waiting

control-alt-deletestop/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 plugged82540EMGigabitEthernetController(PRO/1000MTDesktopAdapter)

-.mountloaded active mountedRootMount

dev-hugepages.mountloaded active mountedHugePagesFileSystem

dev-mqueue.mountloaded active mounted POSIXMessageQueueFileSystem

run-user-1000-gvfs.mountloaded active mounted/run/user/1000/gvfs

run-user-1000.mountloaded active mounted/run/user/1000

snap-core-3887.mountloaded active mountedMountunitforcore

snap-core-4017.mountloaded active mountedMountunitforcore

snap-core-4110.mountloaded active mountedMountunitforcore

snap-gping-13.mountloaded active mountedMountunitforgping

snap-termius\x2dapp-8.mountloaded active mountedMountunitfortermius-app

sys-fs-fuse-connections.mountloaded active mounted FUSEControlFileSystem

sys-kernel-debug.mountloaded active mountedDebugFileSystem

acpid.path loaded active running ACPIEventsCheck

cups.path loaded active running CUPSScheduler

systemd-ask-password-plymouth.path loaded active waitingForwardPasswordRequeststoPlymouthDirectoryWatch

systemd-ask-password-wall.path loaded active waitingForwardPasswordRequeststoWallDirectoryWatch

init.scope loaded active runningSystemandServiceManager

session-c2.scope loaded active runningSessionc2 of user magi

accounts-daemon.service loaded active runningAccountsService

acpid.service loaded active running ACPI event daemon

anacron.service loaded active runningRunanacron jobs

apache2.service loaded active runningTheApacheHTTPServer

apparmor.service loaded active exitedAppArmorinitialization

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 runningAtopadvanced performance monitor

atopacct.service loaded active runningAtopprocess accounting daemon

avahi-daemon.service loaded active runningAvahimDNS/DNS-SDStack

colord.service loaded active runningManage,InstallandGenerateColorProfiles

console-setup.service loaded active exitedSetconsole fontandkeymap

cron.service loaded active runningRegularbackground program processing daemon

cups-browsed.service loaded active runningMakeremote CUPS printers available locally

cups.service loaded active running CUPSScheduler

dbus.service loaded active running D-BusSystemMessageBus

postfix.service loaded active exitedPostfixMailTransportAgent

UNIT 相应的 systemd 单元名称

LOAD 相应的单元是否被加载到内存中

ACTIVE 该单元是否处于活动状态

SUB 该单元是否处于运行状态(LCTT 译注:是较于 ACTIVE 更加详细的状态描述,不同的单元类型有不同的状态。)

DESCRIPTION 关于该单元的简短描述

以下选项可根据类型列出单元:

#systemctllist-units--type service

UNIT LOAD ACTIVE SUB DESCRIPTION

accounts-daemon.service loaded active runningAccountsService

acpid.service loaded active running ACPI event daemon

anacron.service loaded active runningRunanacron jobs

apache2.service loaded active runningTheApacheHTTPServer

apparmor.service loaded active exitedAppArmorinitialization

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 runningAtopadvanced performance monitor

atopacct.service loaded active runningAtopprocess accounting daemon

avahi-daemon.service loaded active runningAvahimDNS/DNS-SDStack

colord.service loaded active runningManage,InstallandGenerateColorProfiles

console-setup.service loaded active exitedSetconsole fontandkeymap

cron.service loaded active runningRegularbackground program processing daemon

cups-browsed.service loaded active runningMakeremote CUPS printers available locally

cups.service loaded active running CUPSScheduler

dbus.service loaded active running D-BusSystemMessageBus

fwupd.service loaded active runningFirmwareupdate daemon

getty@tty1.service loaded active runningGettyon tty1

grub-common.service loaded active exited LSB:Recordsuccessful bootforGRUB

irqbalance.service loaded active running LSB:daemon to balance interruptsforSMP systems

keyboard-setup.service loaded active exitedSetthe console keyboard layout

kmod-static-nodes.service loaded active exitedCreatelistof requiredstaticdevice nodesforthe current kernel

以下选项可帮助您根据状态列出单位,输出与前例类似但更直截了当:

#systemctllist-unit-files--type service

UNIT FILE STATE

accounts-daemon.service enabled

acpid.service disabled

alsa-restore.servicestatic

alsa-state.servicestatic

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@.servicestatic

apport.service generated

apt-daily-upgrade.servicestatic

apt-daily.servicestatic

aptik-battery-monitor.service generated

atop.service enabled

atopacct.service enabled

autovt@.service enabled

avahi-daemon.service enabled

bluetooth.service enabled

运行以下命令以查看指定服务的状态:

#systemctl|grepapache2

apache2.service loaded active runningTheApacheHTTPServer

或者,使用以下命令也可查看指定服务的状态:

#systemctlstatus apache2

●apache2.service-TheApacheHTTPServer

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)sinceTue2018-03-0612:34:09IST;8minago

Process:2786ExecReload=/usr/sbin/apachectl graceful(code=exited,status=0/SUCCESS)

MainPID: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:08magi-VirtualBoxsystemd[1]:StartingTheApacheHTTPServer...

Mar0612:34:09magi-VirtualBoxapachectl[1089]:AH00558:apache2:Couldnotreliably 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,usingfe80::7929:4ed1:279f:4d65.Setthe'ServerName'directive gl

Mar0612:39:10magi-VirtualBoxsystemd[1]:ReloadedTheApacheHTTPServer.

执行以下命令,只查看正在运行的服务:

#systemctl|greprunning

acpid.path loaded active running ACPIEventsCheck

cups.path loaded active running CUPSScheduler

init.scope loaded active runningSystemandServiceManager

session-c2.scope loaded active runningSessionc2 of user magi

accounts-daemon.service loaded active runningAccountsService

acpid.service loaded active running ACPI event daemon

apache2.service loaded active runningTheApacheHTTPServer

aptik-battery-monitor.service loaded active running LSB:start/stop the aptik battery monitor daemon

atop.service loaded active runningAtopadvanced performance monitor

atopacct.service loaded active runningAtopprocess accounting daemon

avahi-daemon.service loaded active runningAvahimDNS/DNS-SDStack

colord.service loaded active runningManage,InstallandGenerateColorProfiles

cron.service loaded active runningRegularbackground program processing daemon

cups-browsed.service loaded active runningMakeremote CUPS printers available locally

cups.service loaded active running CUPSScheduler

dbus.service loaded active running D-BusSystemMessageBus

fwupd.service loaded active runningFirmwareupdate daemon

getty@tty1.service loaded active runningGettyon tty1

irqbalance.service loaded active running LSB:daemon to balance interruptsforSMP systems

lightdm.service loaded active runningLightDisplayManager

ModemManager.service loaded active runningModemManager

NetworkManager.service loaded active runningNetworkManager

polkit.service loaded active runningAuthorizationManager

使用以下命令查看系统启动时会被启用的服务列表:

#systemctllist-unit-files|grepenabled

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%CPUMemoryInput/sOutput/s

/--1.5G--

/init.scope1----

/system.slice153----

/system.slice/ModemManager.service3----

/system.slice/NetworkManager.service4----

/system.slice/accounts-daemon.service3----

/system.slice/acpid.service1----

/system.slice/apache2.service55----

/system.slice/aptik-battery-monitor.service1----

/system.slice/atop.service1----

/system.slice/atopacct.service1----

/system.slice/avahi-daemon.service2----

/system.slice/colord.service3----

/system.slice/cron.service1----

/system.slice/cups-browsed.service3----

/system.slice/cups.service2----

/system.slice/dbus.service6----

/system.slice/fwupd.service5----

/system.slice/irqbalance.service1----

/system.slice/lightdm.service7----

/system.slice/polkit.service3----

/system.slice/repowerd.service14----

/system.slice/rsyslog.service4----

/system.slice/rtkit-daemon.service3----

/system.slice/snapd.service8----

/system.slice/system-getty.slice1----

同时,我们可以使用 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

6a785948d12c33a79431450b7b6af214.png

要查看帮助页面,请按下 ? ,它将显示管理 systemd 服务的可用选项。

3136b0981c277d479c6515dc3ae39ff9.png

本文由 LCTT 原创编译,Linux中国 荣誉推出

0b1331709591d260c1c78e86d0c51c18.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值