You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/deployments/templates/init
Xinwei Xiong 58f591e5f6
Large refactoring projects: OpenIM automation, scripting, and openimctl refactoring (#825)
1 year ago
..
README.md Large refactoring projects: OpenIM automation, scripting, and openimctl refactoring (#825) 1 year ago
openim-api.service Large refactoring projects: OpenIM automation, scripting, and openimctl refactoring (#825) 1 year ago
openim-cmdutils.service Large refactoring projects: OpenIM automation, scripting, and openimctl refactoring (#825) 1 year ago
openim-crontask.service Large refactoring projects: OpenIM automation, scripting, and openimctl refactoring (#825) 1 year ago
openim-msggateway.service Large refactoring projects: OpenIM automation, scripting, and openimctl refactoring (#825) 1 year ago
openim-msgtransfer.service Large refactoring projects: OpenIM automation, scripting, and openimctl refactoring (#825) 1 year ago
openim-push.service Large refactoring projects: OpenIM automation, scripting, and openimctl refactoring (#825) 1 year ago
openim-rpc.service Large refactoring projects: OpenIM automation, scripting, and openimctl refactoring (#825) 1 year ago

README.md

Systemd Configuration, Installation, and Startup

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 Manual

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

More command:

# 列出正在运行的Unit,可以直接使用systemctl
systemctl list-units

# 列出所有Unit包括没有找到配置文件的或者启动失败的
systemctl list-units --all

# 列出所有没有运行的 Unit
systemctl list-units --all --state=inactive

# 列出所有加载失败的 Unit
systemctl list-units --failed

# 列出所有正在运行的、类型为service的Unit
systemctl list-units --type=service

# 显示某个 Unit 是否正在运行
systemctl is-active application.service

# 显示某个 Unit 是否处于启动失败状态
systemctl is-failed application.service

# 显示某个 Unit 服务是否建立了启动链接
systemctl is-enabled application.service

# 立即启动一个服务
sudo systemctl start apache.service

# 立即停止一个服务
sudo systemctl stop apache.service

# 重启一个服务
sudo systemctl restart apache.service

# 重新加载一个服务的配置文件
sudo systemctl reload apache.service

# 重载所有修改过的配置文件
sudo systemctl daemon-reload

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