System D process management

systemd introduction

systemd is the main system daemon management tool on Linux system at present. On the one hand, init manages the process serially, which is prone to blocking. On the other hand, init only executes the startup script and cannot manage the service itself. Therefore, starting from CentOS 7, init has been replaced by systemd as the default system process management tool.

All system resources managed by systemd are called units. These units can be easily managed through the systemd command set. For example, systemctl, hostnamectl, timedatectl, localctl and other commands have rewritten the command usage habits of users in the init era (no longer using chkconfig, service and other commands), but they do provide great convenience.

systemd features

  • The latest systems are managed by systemd (RedHat7, CentOS7, Ubuntu 15...)
  • CentOS7 supports parallel startup service, which significantly improves startup efficiency
  • When CentOS7 is shut down, only running services are shut down, while CentOS6 is shut down once.
  • The start and stop of CentOS7 services are no longer managed by scripts, that is, / etc / init There is no script under D.
  • CentOS7 uses systemd to solve the defects of the original mode. For example, the original service will not close the subprocess generated by the program.

systemd syntax

systemctl [command]      [unit](Configured app name)

command Optional
· start: Start the specified unit          systemctl start nginx
· stop: Close the specified unit           systemctl stop nginx
· restart: Restart specified unit          systemctl restart nginx
· reload: Overload assignment unit           systemctl reload nginx
· enable: Automatically start the specified when the system is powered on unit,The premise is that there are relevant configurations in the configuration file systemctl enable nginx
· disable: Do not automatically run the specified at startup unit   systemctl disable nginx
· status: View assignments unit Current running status systemctl status nginx

systemd configuration file description

  • Each Unit needs a configuration file to tell systemd how to manage services
  • The configuration file is stored in / usr/lib/systemd/system /. After startup, a soft link file will be established in the / etc/systemd/system directory
  • The default suffix for the configuration file configuration of each Unit is service
  • The / usr/lib/systemd/system / directory is divided into two directories: system and user. Generally, programs that can run without login after startup 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

systemd related files

Actual combat I

Source code compilation and installation nginx to realize systemd management control

Install 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

Start nginx in general mode

/usr/local/nginx/sbin/nginx  #start-up
/usr/local/nginx/sbin/nginx  -s reload  #restart
/usr/local/nginx/sbin/nginx -s   quit   #Close 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

Actual combat II

Implementing system D management control by installing tomcat binary

Install the java environment. I have packaged the installation package on my server. You can also download it on 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 direct mount jdk

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:$PATH 
source   /etc/profile
java -version   #Detection environment

Install tomcat

tar  -xf  apache-tomcat-9.0.27  
mv  apache-tomcat-9.0.27  /usr/local/tomcat
 start-up tomcat
sh    /usr/local/tomcat/bin/startup.sh   #start-up
sh   /usr/local/tomcat/bin/shutdown.sh #close

systemd management control startup

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.target
systemctl restart tomcat  #start-up
systemctl enable tomcat   #Configure auto start
systemctl stop  tomcat   #Out of Service
systemctl status  tomcat  #Detection status

The above two actual nginx and tomcat programs have their own start and stop scripts. If the started program does not have its own script, you need to write a similar start and stop script yourself

Actual combat III

Deploy jar program to realize system D management control

In the actual project, some jar programs need to be started. If you start manually, you need to enter a large series of commands. If you stop, you need to kill the process to stop. It's very troublesome

Take an example of actual startup. 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 "(?<=-jar)\s+${jarName}" && kill $(ps -ef | grep -P "(?<=-jar)\s+${jarName}" | awk '{print $2}')
}

case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
esac

Write systemd configuration file

vim  /usr/lib/systemd/system/abc.service

[Unit]
Description=uams server
Wants=network-online.target
After=network.target

[Service]
Type=forking
WorkingDirectory=/usr/local/abc/
ExecStart=/bin/bash uams.sh start
ExecStop=/bin/bash uams.sh stop
ExecReload=/bin/bash uams.sh restart
Restart=on-failure

[Install]
WantedBy=multi-user.target

Start abc service

systemctl restart abc  #start-up
systemctl enable abc   #Configure auto start
systemctl stop  abc   #Out of Service
systemctl status  abc  #Detection status

Click“ Read the original text "Get a better reading experience!

Keywords: Linux Operation & Maintenance

Added by sdjensen on Sat, 25 Dec 2021 05:34:46 +0200