阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

systemd进程管理工具实战教程

28次阅读
没有评论

共计 2287 个字符,预计需要花费 6 分钟才能阅读完成。

导读 systemd 是目前 Linux 系统上主要的系统守护进程管理工具,由于 init 一方面对于进程的管理是串行化的,容易出现阻塞情况,另一方面 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 配置文件说明:
  1. 每一个 Unit 都需要有一个配置文件用于告知 systemd 对于服务的管理方式
  2. 配置文件存放于 /usr/lib/systemd/system/,设置开机启动后会在/etc/systemd/system 目录建立软链接文件
  3. 每个 Unit 的配置文件配置默认后缀名为.service
  4. /usr/lib/systemd/system/ 目录中分为 system 和 user 两个目录,一般将开机不登陆就能运行的程序存在系统服务里,也就是/usr/lib/systemd/system
  5. 配置文件使用方括号分成了多个部分,并且区分大小写
5. 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 服务

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2287字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中