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
tar zxf nginx-1.15.5.tar.gz &&
cd nginx-1.15.5
./configure --prefix=/usr/local/nginx \
    --with-http_ssl_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


ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit


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

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/   #start-up
sh   /usr/local/tomcat/bin/ #close

systemd management control startup

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

Description=tomcat server


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/

Write a startup script



source /etc/profile

    cd ${workDir} && java -jar ${jarName} --server.port=9630 >uams.log 2>&1 &

    ps -ef | grep -qP "(?<=-jar)\s+${jarName}" && kill $(ps -ef | grep -P "(?<=-jar)\s+${jarName}" | awk '{print $2}')

case $1 in

Write systemd configuration file

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

Description=uams server

ExecStart=/bin/bash start
ExecStop=/bin/bash stop
ExecReload=/bin/bash restart


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