[Linux]常用命令之【systemctl/service/chkconfig/pstree】

1 systemctl

1-0 systemctl 基本使用

systemctl start/stop/restart/status sshd

systemctl enable/disable sshd

1-1 systemctl

# 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 更加详细的状态描述,不同的单元类型有不同的状态。)
  • DESCRIPTION 关于该单元的简短描述
systemctl | grep running

1-2 systemctl list-unit-files

systemctl list-unit-files | grep enabled

可根据类型列出单元

# systemctl list-units --type=service
# systemctl --type=service
# systemctl list-units --type=service --state=active
  (列出所有【active】状态(运行或退出)的服务)
# systemctl list-unit-files --state=enabled
  (列出所有【enabled】状态的服务)
# systemctl list-units --type=service --state=running
  (列出所有【正在运行】的服务)
# systemctl list-units --type service --state running,failed
  (列出所有【正在运行】或【failed】状态的服务)

# 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

2 service

service --status-all
service --status-all | grep running
service --status-all | grep httpd

service sshd/httpd start/stop/restart/status

3 chkconfig

  • 查看开机系统自启的服务的启停情况
# chkconfig --list
auditd         	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
blk-availability	0:关闭	1:启用	2:启用	3:启用	4:启用	5:启用	6:关闭
crond          	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
ip6tables      	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
iptables       	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
iscsi          	0:关闭	1:关闭	2:关闭	3:启用	4:启用	5:启用	6:关闭
iscsid         	0:关闭	1:关闭	2:关闭	3:启用	4:启用	5:启用	6:关闭
jexec          	0:关闭	1:启用	2:启用	3:启用	4:启用	5:启用	6:关闭
lvm2-monitor   	0:关闭	1:启用	2:启用	3:启用	4:启用	5:启用	6:关闭
mdmonitor      	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
multipathd     	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
mysql          	0:关闭	1:关闭	2:关闭	3:启用	4:启用	5:启用	6:关闭
netconsole     	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
netfs          	0:关闭	1:关闭	2:关闭	3:启用	4:启用	5:启用	6:关闭
network        	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
ntpdate        	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
postfix        	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
rabbitmq-server	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
rdisc          	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
restorecond    	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
rsyslog        	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
saslauthd      	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
sshd           	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭
svnserve       	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
udev-post      	0:关闭	1:启用	2:启用	3:启用	4:启用	5:启用	6:关闭
vsftpd         	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭
xinetd         	0:关闭	1:关闭	2:关闭	3:关闭	4:关闭	5:关闭	6:关闭

4 pstree

使用 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

5 systemd-cgtop

按资源使用情况(任务、CPU、内存、输入和输出)列出控制组

# systemd-cgtop
Control Group                                              Tasks   %CPU   Memory  Input/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      -        -        -        -

参考文献

posted @ 2020-06-15 23:25  千千寰宇  阅读(643)  评论(0编辑  收藏  举报