###
浅谈systemd
一、system的简要介绍
二、Systemd的Unit类型
三、Systemd的新特性
语法: systemctl COMMAND name.service
一、管理服务
启动:systemctl start name.service
,相当于Centos6的service name.service start
停止:sysytemctl stop name.service
,相当于Centos6的service name.service stop
重启:systemctl restart name.service
,相当于Centos6的service name.service restart
状态:systemctl status name.service
,相当于Centos6的service name.service status
条件式重启:已启动才重启,否则不做操作
sysytmectl try-restart name.service
,相当于Centos6的service name condrestart
重载或重启服务:先加载,再启动
systmenctl reload-or-restart name.service
重载或条件式重启服务:
sysytemctl reload-or-try-restart name.service
禁止自启动和手动启动
systemctl mask name.service
取消禁止自启动和手动启动
systmectl unmask name.service
[root@Centos8 ~]#systemctl mask httpd
Created symlink /etc/systemd/system/httpd.service → /dev/null.
[root@Centos8 ~]#systemctl unmask httpd
Removed /etc/systemd/system/httpd.service.
#结论:mask和umask命令实际为建立/删除指向/dev/null的软链接
实验:修改unit
修改 /usr/lib/systemd/system/httpd.service
文件,将Description 改为The Apache HTTP
,此时查看systemctl status httpd
状态,发现服务正常运行,但是描述没有改变
Warning: The unit file, source configuration file or drop-ins of httpd.service changed on disk. Run 'systemctl daemon-reload' to reload units.
#已经给出提示了
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sun 2019-11-03 17:14:36 CST; 11min ago
#据状态信息的提示,执行`sysytemctl daemon-reload`,再查看状态发现名称改变了
[root@Centos8 /usr/lib/systemd/system]#systemctl daemon-reload
[root@Centos8 /usr/lib/systemd/system]#systemctl status httpd
● httpd.service - The Apache HTTP
当服务正在运行的时候,对服务文件的unit进行改动,必须要重载才能更新
二、查看服务
sytemctl is-active name.service
systemctl list-units -type service
systemctl list-units -t service
systemctl list-units -t service -a or -all
三、设置服务开机状态
设定某服务开机自启动
systemctl enable name.service
,相当于Centos6的chkconfig name on
设定某服务开机禁止自启动
systemctl disable name.service
,相当于Centos6的chkconfig name off
查看所有服务的开机自启状态
systemctl list-unit-files
,相当于Centos6的 chkconfig --list
用来列出该服务在哪些运行级别下启用和禁用:
ls /etc/systemd/system/*wants/sshd.service
,相当于Centos6的 chkconfig sshd --list
实验:设置服务自启和禁止自启
[root@Centos8 ~]#systemctl list-unit-files | grep httpd
httpd.service disabled
#查看httpd服务原来状态是禁止自启
[root@Centos8 ~]#systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
#让httpd服务开机自启后,提示创建了软链接
[root@Centos8 ~]rm -f /etc/systemd/system/multi-user.target.wants/httpd.service
#删除这个软链接文件
[root@Centos8 ~]#systemctl list-unit-files | grep httpd
httpd.service disabled
#查看httpd状态,发现仍然是禁止自启动
[root@Centos8 ~]#ln -s /usr/lib/systemd/system/httpd.service /etc/systemd/system/multi-user.target.wants/httpd.service
#创建原来的软链接文件
[root@Centos8 ~]#systemctl list-unit-files | grep httpd
httpd.service enabled
#此时查看httpd状态,已经是开机自启动
[root@Centos8 ~]#systemctl disable httpd
Removed /etc/systemd/system/multi-user.target.wants/httpd.service
#设置httpd开机禁止自启,提示删除了软链接文件
[root@Centos8 ~]#systemctl is-enabled httpd
disabled
#查看状态,已经变为禁止开机自启动
总结:将某个服务设为自启或者禁止自启,实际上就是在相应的文件夹建立软链接文件的过程,systemd通过查询软链接文件的存在与否判断启用/禁止自启的状态。
四、其他命令
查看服务是否开机自启:
systemctl is-enabled name.service
systemctl list-unit-files | grep name.service
查看服务的依赖关系:
`systemctl list-dependencies httpd.service
[root@Centos8 ~]#systemctl list-dependencies httpd.service
httpd.service
● ├─-.mount
● ├─httpd-init.service
● ├─system.slice
● └─sysinit.target
● ├─dev-hugepages.mount
● ├─dev-mqueue.mount
● ├─dracut-shutdown.service
● ├─import-state.service
● ├─iscsi.service
● ├─kmod-static-nodes.service
● ├─ldconfig.service
● ├─loadmodules.service
● ├─lvm2-lvmpolld.socket
● ├─lvm2-monitor.service
● ├─multipathd.service
● ├─nis-domainname.service
● ├─plymouth-read-write.service
● ├─plymouth-start.service
● ├─proc-sys-fs-binfmt_misc.
.
.
杀掉进程:
systemctl kii unitname
五、服务状态
systemctl list-unit-files -t service -a
一、target的概念
ls /etc/lib/systemd/system/*.target
systemctl list-unit-files
systemctl list-dependencies name.target
二、级别切换
systemctl isolate name.target
systemctl daemon-reload
才能生效)systemctl list-unit-files -t target -a
systemctl list-unit-files | grep target
who -r
runlevel
systemctl get-default
systemctl set-default name.target
三、Systemd其他命令
切换紧急救援模式: systemctl rescue
切换至emergency模式:systemctl emergency
其他常用命令
[root@Centos8 ~]#ls /sbin/poweroff -l
lrwxrwxrwx. 1 root root 16 May 23 22:48 /sbin/poweroff -> ../bin/systemctl
[root@Centos8 ~]#ls /sbin/reboot -l
lrwxrwxrwx. 1 root root 16 May 23 22:48 /sbin/reboot -> ../bin/systemctl
[root@Centos8 ~]#ls /sbin/shutdown -l
lrwxrwxrwx. 1 root root 16 May 23 22:48 /sbin/shutdown -> ../bin/systemctl
完
手机扫一扫
移动阅读更方便
你可能感兴趣的文章