第99节 Systemd Configuration, Installation, and Startup


❤️💕💕记录sealosopen in new window开源项目的学习过程。k8s,docker和云原生的学习open in new window。Myblog:http://nsddd.topopen in new window

1. Introduction

Systemd is the default service management form for the latest Linux distributions, replacing the original init.

Format introduction:

[Unit] : Unit of the service

Description: Brief description of the service

[Service]: Configuration of the service's runtime behavior

ExecStart: Complete path of the program

Restart: Restart configurations like no, always, on-success, on-failure, on-abnormal, on-abort, on-watchdog

[Install]: Installation configuration

WantedBy: Multi-user, etc.

For more details, refer to: Systemd Service Manualopen in new window

Starting command:

systemctl daemon-reload && systemctl enable openim-api && systemctl restart openim-api

Service status:

systemctl status openim-api

Stop command:

systemctl stop openim-api

Why choose systemd?

Advanced requirements:

  • Convenient service runtime log recording for problem analysis
  • Service management logs
  • Option to restart upon abnormal exit

The daemon does not meet these advanced requirements.

nohup only logs the service's runtime outputs and errors.

Only systemd can fulfill all of the above requirements.

The default logs are enhanced with timestamps, usernames, service names, PIDs, etc., making them user-friendly. You can view logs of abnormal service exits. Advanced customization is possible through the configuration files in /lib/systemd/system/.

In short, systemd is the current mainstream way to manage backend services on Linux, so I've abandoned nohup in my new versions of bash scripts, opting instead for systemd.

2. Prerequisites (Requires root permissions)

  1. Configure environment.sh based on the comments.
  2. Create a data directory:
mkdir -p ${OPENIM_DATA_DIR}/{openim-api,openim-crontask}
  1. Create a bin directory and copy openim-api and openim-crontask executable files:
source ./environment.sh
mkdir -p ${OPENIM_INSTALL_DIR}/bin
cp openim-api openim-crontask ${OPENIM_INSTALL_DIR}/bin
  1. Copy the configuration files of openim-api and openim-crontask to the ${OPENIM_CONFIG_DIR} directory:
mkdir -p ${OPENIM_CONFIG_DIR}
cp openim-api.yaml openim-crontask.yaml ${OPENIM_CONFIG_DIR}

3. Create openim-api systemd unit template file

For each OpenIM service, we will create a systemd unit template. Follow the steps below for each service:

Run the following shell script to generate the openim-api.service.template:

source ./environment.sh
cat > openim-api.service.template <<EOF
[Unit]
Description=OpenIM Server API
Documentation=https://github.com/marmotedu/iam/blob/master/init/README.md

[Service]
WorkingDirectory=${OPENIM_DATA_DIR}/openim-api
ExecStart=${OPENIM_INSTALL_DIR}/bin/openim-api --config=${OPENIM_CONFIG_DIR}/openim-api.yaml
Restart=always
RestartSec=5
StartLimitInterval=0

[Install]
WantedBy=multi-user.target
EOF

Following the above style, create the respective template files or generate them in bulk:

First, make sure you've sourced the environment variables:

source ./environment.sh

Use the shell script to generate the systemd unit template for each service:

declare -a services=(
"openim-api"
... [other services]
)

for service in "${services[@]}"
do
   cat > $service.service.template <<EOF
[Unit]
Description=OpenIM Server - $service
Documentation=https://github.com/marmotedu/iam/blob/master/init/README.md

[Service]
WorkingDirectory=${OPENIM_DATA_DIR}/$service
ExecStart=${OPENIM_INSTALL_DIR}/bin/$service --config=${OPENIM_CONFIG_DIR}/$service.yaml
Restart=always
RestartSec=5
StartLimitInterval=0

[Install]
WantedBy=multi-user.target
EOF
done

4. Copy systemd unit template file to systemd config directory (Requires root permissions)

Ensure you have root permissions to perform this operation:

for service in "${services[@]}"
do
   sudo cp $service.service.template /etc/systemd/system/$service.service
done
...

5. Start systemd service

To start the OpenIM services:

for service in "${services[@]}"
do
   sudo systemctl daemon-reload 
   sudo systemctl enable $service 
   sudo systemctl restart $service
done

END 链接