Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Systemd process management tools

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to use Systemd process management tools. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

1. Systemd introduction

At present, systemd is the main system daemon management tool on Linux system. On the one hand, init is serialized and prone to blocking, on the other hand, init only executes startup scripts, and can not manage the service itself. So since CentOS 7, systemd has replaced init as the default system process management tool.

All the system resources managed by systemd are called Unit, and these Unit can be easily managed through the systemd command set. For example, systemctl, hostnamectl, timedatectl, localctl and other commands, although these commands rewrite the command habits of users in the init era (no longer using chkconfig, service and other commands), but they do provide a lot of convenience.

2. Characteristics of systemd:

1. The latest systems all adopt systemd management (RedHat7,CentOS7,Ubuntu15...).

2.CentOS7 supports boot parallel startup service, which significantly improves boot efficiency.

3.CentOS7 shutdown only shuts down running services, while CentOS6 shuts down all once.

The start and stop of 4.CentOS7 services are no longer managed by scripts, that is, there are no scripts under / etc/init.d.

5.CentOS7 uses systemd to solve the shortcomings of the original mode, such as the original service does not shut down the child processes generated by the program.

3. Systemd syntax:

Systemctl [command] [unit] (configured Application name) command optional start: start specified unit systemctl start nginx stop: close specified unit systemctl stop nginx restart: restart specified unit systemctl restart nginx reload: reload specified unit systemctl reload nginx enable: automatically start specified unit when the system boots The premise is that there is a relevant configuration systemctl enable nginx disable in the configuration file: do not automatically run the specified unit systemctl disable nginx status when powered on: view the current running status of the specified unit systemctl status nginx

4. Systemd configuration file description:

Each Unit needs a configuration file to tell systemd how the service is managed.

The configuration file is stored in the / usr/lib/systemd/system/, setting and the soft link file will be established in the / etc/systemd/system directory after boot

The default suffix for the profile configuration of each Unit is .service

The / usr/lib/systemd/system/ directory is divided into two directories: system and user. Generally, the programs that can be run without login are stored in the system service, that is, / usr/lib/systemd/system.

The configuration file is divided into multiple parts using square brackets and is case-sensitive

5. Systemd related documents:

Related files controlled by systemd script startup path for CentOS6CentOS7 service startup / etc/init.d/usr/lib/systemd/system boot service storage path / etc/rcN.d/etc/systemd/system/multi-user.target.wants/ default run-level configuration file / etc/inittab/etc/systemd/system/default.target

Practical one source code compiling and installing nginx to realize systemd management and control

Install the nginx compilation environment

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

General way to start nginx

/ usr/local/nginx/sbin/nginx # start / usr/local/nginx/sbin/nginx-s reload # restart / usr/local/nginx/sbin/nginx-s quit # shut down nginx

Systemd management control startup mode

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

Detailed explanation of parameters

Systemctl restart nginx systemctl enable nginx systemctl stop nginx

The systemd management control nginx service is implemented as shown in the figure.

Practical binary installation tomcat to realize systemd Management and Control

To install the java environment, I have packed the installation package to my server, or you can download it from the official website.

Wget 120.78.77.38/file/jdk-8u231-linux-x64.rpm wget 120.78.77.38/file/apache-tomcat-9.0.27.tar.gz

Rpm-ivh jdk-8u231-linux-x64.rpm # rpm install jdk directly

Configure environment variables

Vim / etc/profile export JAVA_HOME=/usr/java/jdk1.8.0_231-amd64 export JRE_HOME=$ {JAVA_HOME} / jre export CLASSPATH=.:$ {JAVA_HOME} / lib:$ {JRE_HOME} / lib export JAVA_PATH=$ {JAVA_HOME} / bin:$ {JRE_HOME} / bin export PATH=$ {JAVA_HOME} / bin:$PATHsource / etc/profile java-version # testing environment

Install tomcat

Tar-xf apache-tomcat-9.0.27 mv apache-tomcat-9.0.27 / usr/local/tomcat

Start tomcat

Sh / usr/local/tomcat/bin/startup.sh # starts sh / usr/local/tomcat/bin/shutdown.sh # shuts down

Systemd Management Control start

Vim / usr/lib/systemd/system/tomcat.service [Unit] Description=tomcat server Wants=network-online.target After=network.target [Service] Type=forking Environment= "JAVA_HOME=/usr/java/jdk1.8.0_231-amd64" Environment= "PATH=$JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin" Environment= "CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar "ExecStart=/usr/local/tomcat/bin/startup.sh ExecStop=/usr/local/tomcat/bin/shutdown.sh Restart=on-failure [Install] WantedBy=multi-user.targetsystemctl restart tomcat # start systemctl enable tomcat # configure self-starting systemctl stop tomcat # stop Service systemctl status tomcat # check status

The above two practical nginx and tomcat programs come with a start-stop script. If the started program does not have its own script, you need to write a similar start-stop script.

Practice 3 deploy jar program to realize systemd management and control

In the actual project, there will be some jar programs that need to be started. If you start manually, you need to enter a long list of commands to stop, then you need to kill the process to stop. It is very troublesome.

Take an example of actual startup and switch to the jar directory.

Java-jar decode.jar-Dconfig=/usr/local/abc/application.properties

Write a startup script

Vim demo.sh #! / bin/bash # source / etc/profile jarName= "abc-web.jar" workDir= "/ usr/local/abc" start () {cd ${workDir} & & java-jar ${jarName}-spring.profiles.active=prod-- server.port=9630 > uams.log 2 > & 1 &} stop () {ps-ef | grep-qP "(?

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report