Linux systemd及systemctl实战 Linux systemd及systemctl实战

声明

原文:Linux systemd及systemctl实战

systemd简介

 systemd是Linux下的中央系统及设定管理程式(init),包括有守护进程,程序库跟应用程序。开发目标是提供更优秀的框架以表示系统服务间的依赖关系,并以此实现系统初始化时服务的并行启动,同时达到降低shell的系统开销,最终替代现在常用的system v与BSD风格init程序。

Systemd的新特性:

  • 系统引导时实现服务并行启动;

  • 按需激活进程;

  • 系统状态快照;

  • 基于依赖关系定义服务控制逻辑;

syscemctl命令:

systemctl [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.servcie

  • 重载或条件式重启服务:systemctl reload-or-try-restart NAME.service

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

  • 查看所有已激活的服务:systemctl list-units --type service

  • 查看所有服务(已激活及未激活): chkconfig --lsit ==> 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.servcie

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

其它常用命令:

  • 关机: systemctl halt, systemctl poweroff

  • 重启: systemctl reboot

  • 挂起: systemctl suspend

  • 快照: systemctl hibernate

  • 快照并挂起: systemctl hybrid-sleep

核心概念: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文件;

  • Snapshot unit: .snapshot, 管理系统快照;

  • Swap unit: .swap, 用于标识swap设备;

  • Automount unit: .automount,文件系统自动点设备;

  • Path unit: .path, 用于定义文件系统中的一文件或目录;

service unit file 文件通常由三部分组成:

  • [Unit]:定义与Unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等;

  • [Service]:与特定类型相关的专用选项;此处为Service类型;

  • [Install]:定义由“systemctl enable”以及"systemctl disable“命令在实现服务启用或禁用时用到的一些选项;

Unit段的常用选项:

  • Description:描述信息; 意义性描述;

  • After:定义unit的启动次序;表示当前unit应该晚于哪些unit启动;其功能与Before相反;

  • Requies:依赖到的其它units;强依赖,被依赖的units无法激活时,当前unit即无法激活;

  • Wants:依赖到的其它units;弱依赖;

  • Conflicts:定义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所依赖;

service的type详细说明

  • simple:默认值,这个daemon主要由execstart后面所写的字符串来启动,启动后常驻内存。

  • forking:由 ExecStart 启动的程序通过 spawns 延伸出其他子程序来作为此 daemon 的主要服务。原生的父程序在启动结束后就会终止运行

  • oneshot:与simple类似的启动方式类似,但是工作完就结束,不常驻内存

  • dbus:与simple类似的启动方式类似,但要设置DbusName=XX,daemon程序才会运行。

  • idle:与simple类似,要执行这个daemon 必须要所有的工作都顺利执行完毕后才会执行。这类的daemon 通常是开机到最后才执行即可的服务

注意:对于新创建的unit文件或,修改了的unit文件,要通知systemd重载此配置文件;

1
systemctl daemon-reload

练习:为当前系统的httpd服务提供一个unit文件

在Centos7上使用源码安装httpd服务,然后自己写一个unit,实现可以用systemd对服务进行管理  

源码安装 http

1
2
3
4
5
wget http: //mirrors.hust.edu.cn/apache//httpd/httpd-2.4.33.tar.gz
tar xvf httpd-2.4.33.tar.gz    //下载源码包并解压
cd httpd-2.4.33
./configure --prefix=/usr/local/apache/ --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/
make && make install #安装完成

为httpd编写unit文件  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=The Apache HTTP Server
 
[Service]
Type=forking
EnvironmentFile=/usr/local/apache/conf/httpd.conf
ExecStart=/usr/local/apache/bin/apachectl start
ExecReload=/usr/local/apache/bin/apachectl graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
KillSignal=SIGCONT
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

经我测试,如果type设置为:simple,启动程序直接由execstart后面的命令直接运行,并且直接运行在后台中,命令执行起来非常快,但是httpd服务没那么快起来,简单来说,type设置为simple,命令执行虽然快,但

因为主进程需要启动子程序,所以直接使用浏览器浏览是看不到效果的;如果设置为forking,命令执行起来很慢,会中断一段时间,但是执行完成后,可以马上浏览看到效果,并且使用simple有个弊端,因为httpd开辟了很多子程序提供服务,主进程的作用就是启动子进程,当启动完成以后,主进程会结束自身,所以再次使用systemctl status httpd查看,就会显示inactive状态,但其实已经启动了。如果把type设置为:forking,

则不会出现这个问题。

猜你喜欢

转载自www.cnblogs.com/yadongliang/p/12561563.html
今日推荐