1 简介

systemd是目前Linux系统上主要的系统守护进程管理工具,由于init一方面对于进程的管理是串行化的,容易出现阻塞情况,另一方面init也仅仅是执行启动脚本,并不能对服务本身进行更多的管理。所以从CentOS 7开始也由systemd取代了init作为默认的系统进程管理工具。
systemd所管理的所有系统资源都称作Unit,通过systemd命令集可以方便的对这些Unit进行管理。比如systemctl、hostnamectl、timedatectl、localctl等命令,这些命令虽然改写了init时代用户的命令使用习惯(不再使用chkconfig、service等命令),但确实也提供了很大的便捷性。
Linux系统启动流程
POST—>Boot Sequeue(Bios)—>Bootloader(MBR)—>Kernel(ramdisk)—>rootfs—>/sbin/init
不同系统启动方式:
CentOS 5 : SysV init
CentOS 6 : Upstart
CentOS 7 : Systemd

2 Systemd特性

系统引导时实现服务并行启动,此方式显著提升了启动效率
按需激活进程,不会一次性将所有服务启动,而是启动需要运行的程序
基于依赖关系定义服务控制逻辑;
基于socket的激活机制:socket与程序分离;
基于bus的激活机制;
基于device的激活机制;
基于Path的激活机制;
系统状态快照:保存各unit的当前状态信息于持久存储设备中;
向后兼容sysv init脚本,即/etc/init.d/下的启动脚本文件

2.1 核心概念:unit

unit由其相关的配置文件进行标识、识别和配置;文件中主要包含了系统服务、监听的socket、保存的快照以及其他与init相关的信息;这些配置文件主要保存在/usr/lib/systemd/system/,/run/systemd/system, /etc/systemd/system
unit的常见类型:
Service unit:文件扩展名为.service,用于定义系统服务;
Target unit:文件扩展为.target,用于模拟实现”运行级别”;
Device unit:.device,用于定义内核识别的设备;
Mount unit: .mount,定义文件系统挂载点;
Socket unit:.socket,用于标识进程间通信用到的socket文件;
Snapshut:.snapshot,管理系统快照;
Swap unit:.swap,用与标识swap设备;
Automount unit:.automount,文件系统自动挂载点设备;
Path unit:.path,用于定义文件系统中的文件或目录;

2.2 管理系统服务

CentOS 7 :service类型的unit文件;
systemctl命令:Control the systemd system and service manager
语法格式:systemd [Options…] Command [NAME….]
启动: service NAME start ==> systemctl start NAME.service
停止: service NAME stop ==> systemctl stop NAME.service
重启: service NAME restart ==> systemctl restart NAME.service
状态: service NAME status ==> systemctl status NAME.service
条件式重启: service NAME condrestart ==> systemctl try-restart NAME.service
重载或重启服务: systemctl reload-or-restart NAME.service
重载或条件式重启服务: systemctl reload-or-try-restart NAME.service
查看某服务是否激活与否的状态: systemctl is-active NAME.service
查看所有已激活的服务: systemctl list-units —type service
查看所有服务 (已激活及未激活): chkconfig —list ==> systemctl list-units -t service —all
设置服务开机自启: chkconfig NAME on ==>systemctl enable NAME.service
禁止服务开机自启: chkconfig NAME off ==>systemctl disable NAME.service
查看某服务是否能开机自启: chkconfig —list NAME ==>systemctl is-enabled NAME.service
禁止某服务设定为开机自启: systemctl mask NAME.service
取消此禁止: systemctl unmask NAME.service
查看服务的依赖关系: systemctl list-dependencies NAME.service
管理Target units:
运行级别:在CentOS6 中有运行级别的概念,CentOS7之后没有运行级别的概念,但是通过不同的target文件来模拟运行级别
0==>runlevel0.target, poweroff.target
1==>runlevel1.target,rescure.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
相关命令:
级别切换:init N ==>systemctl isolate NAME.target
查看级别:runlevel ==>systemctl list-units —type target
查看所有级别:systemctl list-units -t target -a
获取默认运行级别:systemctl get-default
修改默认运行级别:systemctl set-default NAME.target
切换至紧急救援模式:systemctl rescue
切换至emergency模式:systemctl emergency

其他常用命令:
关机:systemctl halt,systemctl poweroff
重启:systemctl reboot
挂起:systemctl suspend
休眠:systemctl hibernate
混合休眠模式(同时休眠到硬盘并待机):systemctl hybrid-sleep

2.3 service unit file文件编写

文件通常由三部分组成:[Unit]、[Service]、[Install]
[Unit]:定于与unit类型无关的通用选项,用于提供unit的描述信息,unit行为以及依赖关系等
[Service]:与特定类型相关的专用选项,此处为service类型;
[Install]:定义由”systemctl enable” 以及”systemctl disable”命令在实现服务启用或禁用时用到的一些选项
Unit段的常用选项:
Description:描述信息,意义性描述;
After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反;
Requies:依赖到其他的units;强依赖,被依赖的units无法激活时,当前unit即无法激活;
Wants: 依赖到其他units;弱依赖;
Confilicts:定义units间的冲突关系;
Service段常用的选项:
Type:用于定义影响ExecStart及相关参数的功能的unit进程启动类型;
类型:
simple:
forking:
oneshot:
dbus:
notify:
idle:
EnvironmentFIle:环境配置文件;
ExecStart:指明启动unit要运行的命令或脚本;ExecStartPre,ExecStartPost
ExecStop:指明停止unit要运行的命令或脚本;
Restart:
Install段的常用选项:
Alias:
RequiredBy:被哪些units所依赖;
WantedBy:被哪些units所依赖
注意:对于新创建的unit文件或修改了的unit文件,要通知systemd重载此配置文件;
~]# systemctl daemon-reload
示例:

  1. ~]# vim /usr/lib/systemd/system/nginx.service
  2. [Unit]
  3. Description=nginx
  4. After=network.target
  5. [Service]
  6. Type=forking
  7. ExecStart=/usr/local/nginx/sbin/nginx
  8. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  9. ExecStop=/usr/local/nginx/sbin/nginx -s quit
  10. PrivateTmp=true
  11. [Install]
  12. WantedBy=multi-user.target