进程管理器Systemd进程管理工具实战教程System进程




进程管理器Systemd进程管理工具实战教程System进程

2022-07-21 2:11:31 网络知识 官方管理员

进程管理器(Systemd进程管理工具实战教程)(1)

1.systemd介绍

systemd是目前Linux系统上主要的系统守护进程管理工具,由于init一方面对于进程的管理是串行化的,容易出现阻塞情况,另一方面init也仅仅是执行启动脚本,并不能对服务本身进行更多的管理。所以从CentOS7开始也由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:启动指定的unitsystemctlstartnginx·stop:关闭指定的unitsystemctlstopnginx·restart:重启指定unitsystemctlrestartnginx·reload:重载指定unitsystemctlreloadnginx·enable:系统开机时自动启动指定unit,前提是配置文件中有相关配置systemctlenablenginx·disable:开机时不自动运行指定unitsystemctldisablenginx·status:查看指定unit当前运行状态systemctlstatusnginx

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控制的相关文件CentOS6CentOS7服务启动的脚本启动路径/etc/init.d/usr/lib/systemd/system开机自启服务存放路径/etc/rcN.d/etc/systemd/system/multi-user.target.wants/默认运行级别配置文件/etc/inittab/etc/systemd/system/default.target

实战一源码编译安装nginx实现systemd管理控制

安装nginx编译环境

yum-yinstallgccgcc-copenssl-develpcre-develgd-develiproutenet-toolstelnetwgetcurlwgethttp://nginx.org/download/nginx-1.15.5.tar.gztarzxfnginx-1.15.5.tar.gz&&cdnginx-1.15.5./configure--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_stub_status_modulemake-j4&&makeinstall

通用方式启动nginx

/usr/local/nginx/sbin/nginx#启动/usr/local/nginx/sbin/nginx-sreload#重启/usr/local/nginx/sbin/nginx-squit#关闭nginx

systemd管理控制启动模式

vim/usr/lib/systemd/system/nginx.service[Unit]Description=nginxAfter=network.target[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx-sreloadExecStop=/usr/local/nginx/sbin/nginx-squitPrivateTmp=true[Install]WantedBy=multi-user.target

参数详解

systemctlrestartnginxsystemctlenablenginxsystemctlstopnginx

进程管理器(Systemd进程管理工具实战教程)(2)

如图所示实现了systemd管理控制nginx服务

实战二二进制安装tomcat实现systemd管理控制

安装java环境,我已经将安装包打包到我得服务器上,也可以去官网下载

wget120.78.77.38/file/jdk-8u231-linux-x64.rpmwget120.78.77.38/file/apache-tomcat-9.0.27.tar.gz

进程管理器(Systemd进程管理工具实战教程)(3)

rpm-ivhjdk-8u231-linux-x64.rpm#rpm直接安装jdk

配置环境变量

vim/etc/profileexportJAVA_HOME=/usr/java/jdk1.8.0_231-amd64exportJRE_HOME=${JAVA_HOME}/jreexportCLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/libexportJAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/binexportPATH=${JAVA_HOME}/bin:$PATH
source/etc/profilejava-version#检测环境

进程管理器(Systemd进程管理工具实战教程)(4)

安装tomcat

tar-xfapache-tomcat-9.0.27mvapache-tomcat-9.0.27/usr/local/tomcat

启动tomcat

yum-yinstallgccgcc-copenssl-develpcre-develgd-develiproutenet-toolstelnetwgetcurlwgethttp://nginx.org/download/nginx-1.15.5.tar.gztarzxfnginx-1.15.5.tar.gz&&cdnginx-1.15.5./configure--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_stub_status_modulemake-j4&&makeinstall0

systemd管理控制启动

yum-yinstallgccgcc-copenssl-develpcre-develgd-develiproutenet-toolstelnetwgetcurlwgethttp://nginx.org/download/nginx-1.15.5.tar.gztarzxfnginx-1.15.5.tar.gz&&cdnginx-1.15.5./configure--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_stub_status_modulemake-j4&&makeinstall1
yum-yinstallgccgcc-copenssl-develpcre-develgd-develiproutenet-toolstelnetwgetcurlwgethttp://nginx.org/download/nginx-1.15.5.tar.gztarzxfnginx-1.15.5.tar.gz&&cdnginx-1.15.5./configure--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_stub_status_modulemake-j4&&makeinstall2

以上两个实战nginx和tomcat程序中自带了启动停止脚本,如果启动得程序没有自带脚本则需要自己编写一个类似得启动停止脚本

实战三部署jar程序实现systemd管理控制

实际得项目中会有一些jar程序需要启动如果手动启动则需要输入一大串命令停止则需要杀掉进程来停止,很麻烦

举一个实际启动得例子切换到jar目录下

yum-yinstallgccgcc-copenssl-develpcre-develgd-develiproutenet-toolstelnetwgetcurlwgethttp://nginx.org/download/nginx-1.15.5.tar.gztarzxfnginx-1.15.5.tar.gz&&cdnginx-1.15.5./configure--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_stub_status_modulemake-j4&&makeinstall3

编写一个启动脚本

yum-yinstallgccgcc-copenssl-develpcre-develgd-develiproutenet-toolstelnetwgetcurlwgethttp://nginx.org/download/nginx-1.15.5.tar.gztarzxfnginx-1.15.5.tar.gz&&cdnginx-1.15.5./configure--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_stub_status_modulemake-j4&&makeinstall4

#编写systemd配置文件

yum-yinstallgccgcc-copenssl-develpcre-develgd-develiproutenet-toolstelnetwgetcurlwgethttp://nginx.org/download/nginx-1.15.5.tar.gztarzxfnginx-1.15.5.tar.gz&&cdnginx-1.15.5./configure--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_stub_status_modulemake-j4&&makeinstall5

启动abc服务

yum-yinstallgccgcc-copenssl-develpcre-develgd-develiproutenet-toolstelnetwgetcurlwgethttp://nginx.org/download/nginx-1.15.5.tar.gztarzxfnginx-1.15.5.tar.gz&&cdnginx-1.15.5./configure--prefix=/usr/local/nginx\--with-http_ssl_module\--with-http_stub_status_modulemake-j4&&makeinstall6

发表评论:

最近发表
网站分类
标签列表