centos

sys.18.1 systemd

 

系统启动后,启动的第一个进程是’init’,对于centos系统版本不同,init进程的工作机制也不同;
centos 5 使用的是 SysV init;
centos 6 使用的是 UPstart;
centos 7 使用的是 Systemd;

1、Systemd的新特性:
系统引导启动时实现服务并行启动;
按需激活进程;
系统状态快照;
基于依赖关系定义服务控制逻辑;

2、Systemd引入的核心概念:unit;

unit的作用
由其相关的配置文件进行标识、识别和配置;

unit包含的类别:
系统服务、监听的socket、保存的快照以及其它与init相关的信息;

unit相关的配置文件:
/usr/lib/systemd/system/*
/run/systemd/system/*
/etc/systemd/system/*

[root@KOU system]#
[root@KOU system]# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
[root@KOU system]#
[root@KOU system]#
[root@KOU system]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@KOU system]#
[root@KOU system]# ll /etc/systemd/system/multi-user.target.wants/httpd.service
lrwxrwxrwx. 1 root root 37 May 24 04:12 /etc/systemd/system/multi-user.target.wants/httpd.service -> /usr/lib/systemd/system/httpd.service
[root@KOU system]#

‘/run/systemd/system/’目录内容:

[root@KOU system]# ll /run/systemd/system/
total 36
-rw-r--r--. 1 root root  17 May 21 22:01 session-1.scope
drwxr-xr-x. 2 root root 160 May 21 22:01 session-1.scope.d
-rw-r--r--. 1 root root  17 May 24 04:00 session-387.scope
drwxr-xr-x. 2 root root 160 May 24 04:00 session-387.scope.d
-rw-r--r--. 1 root root  17 May 24 04:05 session-389.scope
drwxr-xr-x. 2 root root 160 May 24 04:05 session-389.scope.d
-rw-r--r--. 1 root root  17 May 24 04:05 session-390.scope
drwxr-xr-x. 2 root root 160 May 24 04:05 session-390.scope.d
-rw-r--r--. 1 root root  17 May 22 06:35 session-54.scope
-rw-r--r--. 1 root root  17 May 22 09:25 session-85.scope
-rw-r--r--. 1 root root  17 May 21 22:01 session-c1.scope
drwxr-xr-x. 2 root root 160 May 21 22:01 session-c1.scope.d
-rw-r--r--. 1 root root  17 May 21 22:01 user-0.slice
drwxr-xr-x. 2 root root 120 May 21 22:01 user-0.slice.d
-rw-r--r--. 1 root root  17 May 21 22:01 user-42.slice
drwxr-xr-x. 2 root root 120 May 21 22:01 user-42.slice.d
[root@KOU system]#

‘/etc/systemd/system’文件内容:

[root@KOU system]# ll /etc/systemd/system | head -10
total 4
drwxr-xr-x. 2 root root   31 May 21 20:20 basic.target.wants
drwxr-xr-x. 2 root root   31 May 21 21:35 bluetooth.target.wants
lrwxrwxrwx. 1 root root   41 May 21 21:35 dbus-org.bluez.service -> /usr/lib/systemd/system/bluetooth.service
lrwxrwxrwx. 1 root root   41 May 21 20:20 dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx. 1 root root   44 May 22 09:46 dbus-org.freedesktop.Avahi.service -> /usr/lib/systemd/system/avahi-daemon.service
lrwxrwxrwx. 1 root root   44 May 21 21:40 dbus-org.freedesktop.ModemManager1.service -> /usr/lib/systemd/system/ModemManager.service
lrwxrwxrwx. 1 root root   46 May 21 20:20 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root   57 May 21 20:20 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
lrwxrwxrwx. 1 root root   40 May 21 22:00 default.target -> /usr/lib/systemd/system/graphical.target
[root@KOU system]#

‘/usr/lib/systemd/system’文件内容:

[root@KOU system]# ll /usr/lib/systemd/system | head -10
total 1460
-rw-r--r--. 1 root root  275 Apr 27 22:53 abrt-ccpp.service
-rw-r--r--. 1 root root  380 Apr 27 22:53 abrtd.service
-rw-r--r--. 1 root root  361 Apr 27 22:53 abrt-oops.service
-rw-r--r--. 1 root root  266 Apr 27 22:53 abrt-pstoreoops.service
-rw-r--r--. 1 root root  262 Apr 27 22:53 abrt-vmcore.service
-rw-r--r--. 1 root root  311 Apr 27 22:53 abrt-xorg.service
-rw-r--r--. 1 root root  751 Apr 11 15:18 accounts-daemon.service
-rw-r--r--. 1 root root  527 Mar 25  2017 alsa-restore.service
-rw-r--r--. 1 root root  486 Mar 25  2017 alsa-state.service
[root@KOU system]#

 

unit的常见类别:
Service  unit: 其文件扩展名为’.service’,用于定义系统服务;
Target  unit: 其文件扩展名为’.target’,用于模拟实现”运行级别”;
Device  unit: 其文件扩展名为’.device’,用于定义内核识别的设备;
Mount  unit: 其文件扩展名为’.mount’,用于定义文件系统挂载点;
Socket  unit: 其文件扩展名为’.socket’,用于标识进程间通信用到的socket文件;
Snapshot  unit: 其文件扩展名为’.snapshot’,用于管理系统快照;
Swap  unit: 其文件扩展名为’.swap’,用于标识swap设备;
Automount  unit: 其文件扩展名为’.automount’,文件系统自动挂载点设置;
Path  unit: 其文件扩展名为’.path’,用于定义文件系统中的

3、Systemd的关键特性

基于socket的激活机制:socket与程序分离;
基于bus(总线)的激活机制;
基于device的激活机制;
基于path的激活机制;
系统快照:保存各unit的当前状态信息于持久存储设备中;
向后兼容sysv init脚本;

4、Systemd与SysV init、UPstart的不同之处(不兼容之处):

systemctl命令是固定不变的;
非由systemd启动的服务,systemctl无法与之通信;

*********************************************************************************************

5、Systemd管理系统服务(.service)

centos 7:systemd通过对service类型的unit文件的管理来实现的;

systemctl命令说明:
Control  the  systemd  system  and  service  manager;

命令格式:
systemctl  [OPTIONS…]  COMMAND  [NAME…]

1> 启动、停止、重启、状态查看、条件式重启;
centos 6: service  SERVICE_NAME  {start|stop|restart|status|condrestart}
centos 7: systemctl  {start|stop|restart|status|try-restart}  SERVICE_NAME.service

2> 设置服务开机自启动、禁止服务开机自启动
centos 6:chkcofnig  SERVICE_NAME  on
                   chkconfig  SERVICE_NAME  off
centos 7:systemctl  enable  SERVICE_NAME.service
                   systemctl  disable  SERVICE_NAME.service

3> 查看某服务是否能开机自启动
centos 6:chkconfig  –list  SERVICE_NAME
centos 7:systemctl  is-active  SERVICE_NAME.service

4> centos 7 独有的命令:
重载或重启服务: systemctl  reload-or-restart  SERVICE_NAME.service
重载或条件式重启服务: systemctl  reload-or-try-restart  SERVICE_NAME.service

查看某服务当前激活与否的状态: systemctl  is-active  SERVICE_NAME.service

查看所有已激活的服务: systemctl  list-units  -t  SERVICE_TYPE
查看所有已激活的服务以及已经装载但未激活的服务:systemctl  list-units  -t  SERVICE_TYPE  -a
备注:’SERVICE_TYPE’指的是’unit’的类别,比如,service,target, socket,等等,此处特指’service’;

禁止某服务设定为开机自启动: systemctl  mask  SERVICE_NAME.service
取消此禁止: systemctl  unmask  SERVICE_NAME.service

查看服务的依赖关系: systemctl  list-dependencies  SERVICE_NAME.service

6、service-unit-file

以’httpd.service’文件为例:

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

 

1> service-unit-file文件内容组成部分:
[Unit]:定义与unit类型无关的通用选项;用于向当前文件对应的服务提供unit的描述信息、unit行为及依赖关系等;
[Service]:与特定类型相关的选项;此处为service类型;
[Install]:定义由’systemctl enable’以及’systemctl disable’命令实现服务开机自启动或禁用开机自启动时用到的一些选项;

2> [Unit]段常用选项:
Description:描述信息,做意义性描述;
After:定义unit的启动次序;表示当前unit应该晚于哪些unit启动;其相反的功能叫’Before’;
Wants:依赖到的其他unit;弱依赖,被依赖的unit无法激活时,不影响当前unit的激活动作;
Requies:依赖到的其他unit;强依赖,被依赖的unit无法激活时,当前unit也无法激活;
Conflicts:定义unit间的冲突关系;

3> [Service]段常用选项:
Type:unit进程启动类型,用于定义影响ExecStart及相关参数的功能;
EnvironmentFile:环境配置文件;在ExecStart之前读取此文件,并为ExecStart执行时提供一些变量等自定义功能;
ExecStart:指明启动unit要运行的命令或脚本;
ExecReload:
ExecStop:指明停止unit要运行的命令或脚本;

4> [Install]段常用选项:
Alias:
RequiredBy:被哪些unit所依赖;强类型;
WantedBy:被哪些unit所依赖;弱类型;

NOTE:
新创建的unit文件或对unit文件做了修改,需要对配置文件做重载操作;
systemctl  daemon-reload

7、systemd管理target units(.target)

centos 7 运行级别及对应的target名称:
0  —>  runlevel0.target , poweroff.target
1  —>  runlevel1.target , rescue.target
2  —>  runlevel2.target , multi-user.target
3  —>  runlevel3.target , multi-user.target
4  —>  runlevel4.target , multi-user.target
5  —>  runlevel5.target , graphical.target
6  —>  runlevel6.target , reboot.target

1> 级别切换操作:
centos 6:init  N
centos 7:systemctl  isolate  NAME.target

2> 查看运行级别(针对大类的target unit,不单单是运行级别):
centos 6:runlevel
centos 7:systemctl  list-units  -t  target                          查看所有已激活的target;
systemctl  list-units  -t  target  -a                                       查看所有已激活以及未激活的target;

3> 查看、修改默认运行级别(centos 7):
systemctl  get-default
systemctl  set-default  NAME.target

4> 切换至紧急救援模式(centos 7):
systemctl  rescue

5> 切换至紧急模式(emergency)(centos 7):
systemctl  emergency

备注,紧急救援模式与紧急模式的区别:
救援模式:启动的运行级别为’1’,会加载系统能探测到的各种设备的驱动程序;
紧急模式:不会加载系统的各种驱动程序;

8、其他常用命令

关机:systemctl  {halt|poweroff}
重启:systemctl  reboot

挂起:systemctl  suspend

快照:systemctl  hibernate
快照并挂起:systemctl  hybrid-sleep

Leave a Reply

Your email address will not be published. Required fields are marked *