systemctl命令的使用及服务状态的查看

systemctl命令的使用及服务状态的查看
参考文档:https://blog.csdn.net/sky__man/article/details/78178821
    https://linux.cn/article-5926-1.html

根据参考文档整理

1、systemctl 是系统初始化程序,系统开始的第一个进程,pid为1
 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                        
   1 root      20   0  194636   7792   4064 S   0.0  0.0   3:39.18 systemd     

systemctl --version    #查看版本

#检查systemd和systemctl的二进制文件和库文件的安装位置
whereis system    
system: /usr/share/man/man3p/system.3p.gz /usr/share/man/man3/system.3.gz
whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

ps -eaf |grep [s]ystemd        #检查systemd是否运行
注意:systemd是作为父进程(PID=1)运行的。在上面带(-e)参数的ps命令输出中,选择所有进程,(-a)选择除会话前导外的所有进程,并使用(-f)参数输出完整格式列表(即 -eaf)。
也请注意上例中后随的方括号和例子中剩余部分。方括号表达式是grep的字符类表达式的一部分。    

#分析systemd启动进程
systemd-analyze
Startup finished in 10.499s (kernel) + 964ms (initrd) + 13.621s (userspace) = 25.085s

systemd-analyze blame    #分析启动时各个进程花费的时间

systemd-analyze critical-chain    #分析启动时的关键链
The time after the unit is active or started is printed after the "@" character.
The time the unit takes to start is printed after the "+" character.

graphical.target @13.616s
└─multi-user.target @13.616s
  └─libvirtd.service @6.636s +729ms
    └─remote-fs.target @6.635s
      └─remote-fs-pre.target @6.635s
        └─iscsi-shutdown.service @6.614s +19ms
          └─network.target @6.607s
            └─wpa_supplicant.service @12.465s +26ms
              └─basic.target @2.647s
                └─sockets.target @2.647s
                  └─rpcbind.socket @2.647s
                    └─sysinit.target @2.605s
                      └─systemd-update-utmp.service @2.598s +6ms
                        └─auditd.service @2.012s +584ms
                          └─systemd-tmpfiles-setup.service @1.848s +163ms
                            └─rhel-import-state.service @1.706s +140ms
                              └─local-fs.target @1.705s
                                └─run-user-42.mount @10.101s
                                  └─local-fs-pre.target @1.401s
                                    └─lvm2-monitor.service @498ms +855ms
                                      └─lvm2-lvmetad.service @536ms
                                        └─lvm2-lvmetad.socket @497ms
                                          └─-.slice

扫描二维码关注公众号,回复: 4849318 查看本文章

重要:Systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元。

systemd-analyze critical-chain sshd.service    #分析某个服务(sshd)的关键链
The time after the unit is active or started is printed after the "@" character.
The time the unit takes to start is printed after the "+" character.

sshd.service +223ms
└─network.target @6.607s
  └─wpa_supplicant.service @12.465s +26ms
    └─basic.target @2.647s
      └─sockets.target @2.647s
        └─rpcbind.socket @2.647s
          └─sysinit.target @2.605s
            └─systemd-update-utmp.service @2.598s +6ms
              └─auditd.service @2.012s +584ms
                └─systemd-tmpfiles-setup.service @1.848s +163ms
                  └─rhel-import-state.service @1.706s +140ms
                    └─local-fs.target @1.705s
                      └─run-user-42.mount @10.101s
                        └─local-fs-pre.target @1.401s
                          └─lvm2-monitor.service @498ms +855ms
                            └─lvm2-lvmetad.service @536ms
                              └─lvm2-lvmetad.socket @497ms
                                └─-.slice

2、systemctl 命令:
systemctl list-units            ##列出当前系统服务的状态(列出所有运行中单元)
systemctl list-unit-files       ##列出服务的开机状态(列出所有可用单元)
systemctl --failed        ##列出所有失败单元
systemctl is-enabled crond.service     #检查某个单元(如 cron.service)是否启用
enabled

systemd-cgls        #按等级列出控制组
systemd-cgtop        #按CPU、内存、输入和输出列出控制组

systemctl list-dependencies sshd    ##查看指定服务的倚赖关系

systemctl mask  sshd            ##冻结指定服务(示例:)
    [root@erp-ora ~]# systemctl mask tnslsnr
    Created symlink from /etc/systemd/system/tnslsnr.service to /dev/null.

    [root@erp-ora ~]# systemctl status tnslsnr
    ● tnslsnr.service
       Loaded: masked (/dev/null; bad)
       Active: inactive (dead)


systemctl unmask sshd           ##启用服务

systemctl set-default multi-user.target ##开机不开启图形

systemctl set-default graphical.target  ##开机启动图形

setterm                 ##文本界面设定color

systemctl list-unit-files --type=service    ##列出所有服务(包括启用的和禁用的)
systemctl status sshd           ##查看指定服务的状态

systemctl stop sshd             ##关闭指定服务

systemctl start sshd            ##开启指定服务

systemctl restart sshd          ##从新启动服务

systemctl enable sshd           ##设定指定服务开机开启

systemctl disable sshd          ##设定指定服务开机关闭

systemctl reload sshd           ##使指定服务从新加载配置

systemctl kill httpd        ##使用systemctl命令杀死服务

3、服务状态:
systemctl   status     服务名称

loaded             ##系统服务已经初始化完成,加载过配置

active(running)       ##正有一个或多个程序正在系统中执行, vsftpd就是这种模式

atcive(exited)        ##僅執行一次就正常結束的服務, 目前並沒有任何程序在系統中執行

atcive(waiting)       ##正在執行當中,不過還再等待其他的事件才能继续处理

inactive            ##服务关闭

enbaled           ##服务开机启动

disabled          ##服务开机不自启

static                ##服务开机启动项不可被管理

failed                ##系统配置错误

4、使用Systemctl控制并管理挂载点
systemctl list-unit-files --type=mount    # 列出所有系统挂载点

#挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态
# systemctl start tmp.mount
# systemctl stop tmp.mount
# systemctl restart tmp.mount
# systemctl reload tmp.mount
# systemctl status tmp.mount

5、使用Systemctl控制并管理套接口
systemctl list-unit-files --type=socket        #列出所有可用系统套接口

#在Linux中启动、重启、停止、重载套接口并检查其状态
# systemctl start cups.socket
# systemctl restart cups.socket
# systemctl stop cups.socket
# systemctl reload cups.socket
# systemctl status cups.socket

6、服务的CPU利用率(分配额)
systemctl show -p CPUShares sshd.service #获取当前某个服务的CPU分配额(如sshd)
CPUShares=18446744073709551615
注意:各个服务的默认CPU分配份额=1024,你可以增加/减少某个进程的CPU分配份额。

systemctl set-property sshd.service CPUShares=2000    #将某个服务(sshd.service)的CPU分配份额限制为2000 CPUShares/(有点延时,需要等一下起作用)
systemctl show -p CPUShares sshd.service
CPUShares=2000

#当你为某个服务设置CPUShares,会自动创建一个以服务名命名的目录,该文件含有CPUShare限制信息,你可以通过以下方式查看该文件:
cat /etc/systemd/system/sshd.service.d/50-CPUShares.conf 
[Service]
CPUShares=2000

systemctl show sshd    #检查某个服务的所有配置细节

7、控制系统运行等级
 systemctl rescue    #启动系统救援模式(此时的 runlevel 运行级别是 5 1)
PolicyKit daemon disconnected from the bus.
We are no longer a registered authentication agent.

Broadcast message from root@erp-ora on pts/0 (Mon 2019-01-07 11:14:41 CST):

The system is going down to rescue mode NOW!

systemctl emergency    #进入紧急模式

systemctl get-default     #列出当前使用的运行等级
graphical.target

#启动运行等级5,即图形模式
# systemctl isolate runlevel5.target

# systemctl isolate graphical.target

#启动运行等级3,即多用户模式(命令行)
# systemctl isolate runlevel3.target

# systemctl isolate multiuser.target

#设置多用户模式或图形模式为默认运行等级
# systemctl set-default runlevel3.target
# systemctl set-default runlevel5.target

#重启、停止、挂起、休眠系统或使系统进入混合睡眠
# systemctl reboot
# systemctl halt
# systemctl suspend
# systemctl hibernate
# systemctl hybrid-sleep


对于不知运行等级为何物的人,说明如下。

Runlevel 0 : 关闭系统
Runlevel 1 : 救援?维护模式
Runlevel 3 : 多用户,无图形系统
Runlevel 4 : 多用户,无图形系统
Runlevel 5 : 多用户,图形化系统
Runlevel 6 : 关闭并重启机器
 

猜你喜欢

转载自blog.csdn.net/yaoshixian/article/details/85998114