
Systemd 是一个专用于 Linux 操作系统的系统与服务管理器,其目的是要取代Unix时代以来一直在使用的init系统。
1. systemd介绍
systemd是目前Linux系统上主要的系统守护进程管理工具,由于init一方面对于进程的管理是串行化的,容易出现阻塞情况,另一方面init也仅仅是执行启动脚本,并不能对服务本身进行更多的管理。所以从CentOS 7开始也由systemd取代了init作为默认的系统进程管理工具。
systemd所管理的所有系统资源都称作Unit,通过systemd命令集可以方便的对这些Unit进行管理。比如systemctl、hostnamectl、timedatectl、localctl等命令,这些命令虽然改写了init时代用户的命令使用习惯(不再使用chkconfig、service等命令),但确实也提供了很大的便捷性。
2. systemd特点:
1.最新系统都采用systemd管理(RedHat7,CentOS7,Ubuntu15…)
2.CentOS7 支持开机并行启动服务,显著提高开机启动效率
3.CentOS7关机只关闭正在运行的服务,而CentOS6,全部都关闭一次。
4.CentOS7服务的启动与停止不再使用脚本进行管理,也就是/etc/init.d下不在有脚本。
5.CentOS7使用systemd解决原有模式缺陷,比如原有service不会关闭程序产生的子进程。
3. systemd语法:
systemctl [command] [unit](配置的应用名称)
command可选项
· start:启动指定的unit systemctl start nginx
· stop:关闭指定的unit systemctl stop nginx
· restart:重启指定unit systemctl restart nginx
· reload:重载指定unit systemctl reload nginx
· enable:系统开机时自动启动指定unit,前提是配置文件中有相关配置 systemctl enable nginx
· disable:开机时不自动运行指定unit systemctl disable nginx
· status:查看指定unit当前运行状态 systemctl status nginx
4. systemd配置文件说明:
每一个Unit都需要有一个配置文件用于告知systemd对于服务的管理方式 配置文件存放于/usr/lib/systemd/system/,设置开机启动后会在/etc/systemd/system目录建立软链接文件 每个Unit的配置文件配置默认后缀名为.service 在/usr/lib/systemd/system/目录中分为system和user两个目录,一般将开机不登陆就能运行的程序存在系统服务里,也就是/usr/lib/systemd/system 配置文件使用方括号分成了多个部分,并且区分大小写
5. systemd相关文件:
systemd进程管理工具实战教程systemd进程管理工具实战教程
实战 源码编译安装nginx 实现systemd管理控制
安装nginx编译环境
yum -y install gcc gcc-c++ openssl-devel pcre-devel gd-devel iproute net-tools telnet wget curl
wget http://nginx.org/download/nginx-1.15.5.tar.gz
tar zxf nginx-1.15.5.tar.gz &&
cd nginx-1.15.5
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module
make -j 4 && make install
通用方式启动nginx
/usr/local/nginx/sbin/nginx #启动
/usr/local/nginx/sbin/nginx -s reload #重启
/usr/local/nginx/sbin/nginx -s quit #关闭nginx
systemd 管理控制启动模式
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
参数详解
systemctl restart nginx
systemctl enable nginx
systemctl stop nginx
如图所示实现了systemd 管理控制nginx服务
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/221511.html<