您的位置:首页 > 运维架构 > Linux

centos 7 systemctl 命令总结

2017-06-27 16:33 351 查看
Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。

Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

案例:

1. 首先检查你的系统中是否安装有systemd并确定当前安装的版本

[root@web-01-110 ~]# systemctl --version

systemd 219

+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

2. 检查systemd和systemctl的二进制文件和库文件的安装位置

[root@web-01-110 ~]# whereis systemctl

systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

[root@web-01-110 ~]# whereis systemd

systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz

3. 检查systemd是否运行

[root@web-01-110 ~]# ps -ef|grep systemd

root 1 0 0 03:06 ? 00:00:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 21

root 353 1 0 03:06 ? 00:00:00 /usr/lib/systemd/systemd-journald

root 386 1 0 03:06 ? 00:00:00 /usr/lib/systemd/systemd-udevd

root 924 1 0 03:06 ? 00:00:00 /usr/lib/systemd/systemd-logind

dbus 935 1 0 03:06 ? 00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation

root 4008 2666 0 04:07 pts/0 00:00:00 grep --color=auto systemd

注意:systemd是作为父进程(PID=1)运行的

4. 分析systemd启动进程

[root@web-01-110 ~]# systemd-analyze

Startup finished in 615ms (kernel) + 3.729s (initrd) + 26.040s (userspace) = 30.385s

5. 分析启动时各个进程花费的时间

[root@web-01-110 ~]# systemd-analyze blame

5.657s kdump.service

5.366s NetworkManager-wait-online.service

5.350s abrt-ccpp.service

5.043s sysstat.service

4.191s systemd-logind.service

3.905s rhel-dmesg.service

3.875s tuned.service

6. 分析启动时的关键链

[root@web-01-110 ~]# 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.

multi-user.target @26.029s

`-tuned.service @20.345s +3.875s

`-network.target @20.314s

`-network.service @19.689s +622ms

`-NetworkManager-wait-online.service @14.309s +5.366s

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

7. 列出所有可用单元

[root@web-01-110 ~]# systemctl list-unit-files

UNIT FILE STATE

proc-sys-fs-binfmt_misc.automount static

dev-hugepages.mount static

dev-mqueue.mount static

proc-fs-nfsd.mount static

proc-sys-fs-binfmt_misc.mount static

sys-fs-fuse-connections.mount static

sys-kernel-config.mount static

sys-kernel-debug.mount static

tmp.mount disabled

var-lib-nfs-rpc_pipefs.mount static

brandbot.path disabled

说明: static 它是指对应的 Unit 文件中没有定义[Install]区域,因此无法配置为开机启动服务;disabled关闭开机启动;enable开启开机启动

8. 列出所有运行中单元

[root@web-01-110 ~]# systemctl list-units

UNIT LOAD ACTIVE SUB DESCRIPTION

proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File Syste

sys-devices-pci0000:00-0000:00:07.1-ata2-host2-target2:0:0-2:0:0:0-block-sr0.device loaded active plugged VMwar

sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged VMwar

sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged VMwa

9. 列出所有失败单元

[root@web-01-110 ~]# systemctl --failed

UNIT LOAD ACTIVE SUB DESCRIPTION

* kdump.service loaded failed failed Crash recovery kernel arming

LOAD = Reflects whether the unit definition was properly loaded.

ACTIVE = The high-level unit activation state, i.e. generalization of SUB.

SUB = The low-level unit activation state, values depend on unit ty

10. 检查某个单元(如 cron.service)是否启用

查看crond定时任务是否启动服务

[root@web-01-110 ~]# systemctl is-enabled crond

enabled

11. 检查某个单元或服务是否运行

查看firewalld防火墙是否运行,Active: inactive (dead) 说明关闭状态

[root@web-01-110 ~]# systemctl status firewalld

* firewalld.service - firewalld - dynamic firewall daemon

Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)

Active: inactive (dead) 活动:无效(死亡)

Docs: man:firewalld(1)

12. 列出所有服务(包括启用的和禁用的)

[root@web-01-110 ~]# systemctl list-unit-files --type=service

UNIT FILE STATE

abrt-ccpp.service enabled

abrt-oops.service enabled

abrt-pstoreoops.service disabled

abrt-vmcore.service enabled

abrt-xorg.service enabled

abrtd.service enabled

arp-ethers.service disabled

atd.service enabled

auditd.service enabled

说明:enabled 开启,disabled 关闭

13. Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

[root@web-01-110 ~]# systemctl start httpd.service

[root@web-01-110 ~]# systemctl restart httpd.service

[root@web-01-110 ~]# systemctl stop httpd.service

[root@web-01-110 ~]# systemctl reload httpd.service

[root@web-01-110 ~]# systemctl status httpd.service

httpd.service - The Apache HTTP Server

Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)

Active: active (running) since Tue 2015-04-28 17:21:30 IST; 6s ago

Process: 2876 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)

Main PID: 2881 (httpd)

Status: "Processing requests..."

CGroup: /system.slice/httpd.service

├─2881 /usr/sbin/httpd -DFOREGROUND

├─2884 /usr/sbin/httpd -DFOREGROUND

├─2885 /usr/sbin/httpd -DFOREGROUND

├─2886 /usr/sbin/httpd -DFOREGROUND

├─2887 /usr/sbin/httpd -DFOREGROUND

└─2888 /usr/sbin/httpd -DFOREGROUND

Apr 28 17:21:30 tecmint systemd[1]: Starting The Apache HTTP Server...

Apr 28 17:21:30 tecmint httpd[2881]: AH00558: httpd: Could not reliably determine the server's fully q...ssage

Apr 28 17:21:30 tecmint systemd[1]: Started The Apache HTTP Server.

Hint: Some lines were ellipsized, use -l to show in full.

注意:当我们使用systemctl的start,restart,stop和reload命令时,我们不会从终端获取到任何输出内容,只有status命令可以打印输出。

14. 如何激活服务并在启动时启用或禁用服务(即系统启动时自动启动服务)

[root@web-01-110 ~]# systemctl is-active httpd.service

[root@web-01-110 ~]# systemctl enable httpd.service

[root@web-01-110 ~]# systemctl disable httpd.service

15. 如何屏蔽(让它不能启动)或显示服务(如 httpd.service)

[root@web-01-110 ~]# systemctl mask crond.service

Created symlink from /etc/systemd/system/crond.service to /dev/null.

[root@web-01-110 ~]# systemctl unmask crond.service

Removed symlink /etc/systemd/system/crond.service.

16. 在启动时激活、启用或禁用挂载点(系统启动时自动挂载)

[root@web-01-110 ~]# systemctl is-active tmp.mount

[root@web-01-110 ~]# systemctl enable tmp.mount

[root@web-01-110 ~]# systemctl disable tmp.mount

17. 获取当前某个服务的CPU分配额(如httpd)

[root@web-01-110 ~]# systemctl show -p CPUShares httpd.service

CPUShares=1024

注意:各个服务的默认CPU分配份额=1024,你可以增加/减少某个进程的CPU分配份额。

18. 检查某个服务的所有配置细节

[root@web-01-110 ~]# systemctl show httpd

Id=httpd.service

Names=httpd.service

Requires=basic.target

Wants=system.slice

WantedBy=multi-user.target

Conflicts=shutdown.target

Before=shutdown.target multi-user.target

After=network.target remote-fs.target nss-lookup.target systemd-journald.socket basic.target system.slice

Description=The Apache HTTP Server

LoadState=loaded

ActiveState=active

SubState=running

FragmentPath=/usr/lib/systemd/system/httpd.service

....

参考资料:https://www.tecmint.com/manage-services-using-systemd-and-systemctl-in-linux/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: