Xinwei Xiong(cubxxw-openim) 92d8c65aa0
feat: save all file
Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com>
2023-08-17 14:54:41 +08:00
..
2023-08-17 14:54:41 +08:00
2023-08-17 14:54:41 +08:00
2023-08-17 14:54:41 +08:00

Systemd 配置、安装和启动

0. 介绍

systemd是最新linux发行版管理后台的服务的默认形式用以取代原有的init。

格式介绍:

[Unit] :服务单元

Description对该服务进行简单的描述

[Service]:服务运行时行为配置

ExecStart程序的完整路径

Restart:重启配置no、always、on-success、on-failure、on-abnormal、on-abort、on-watchdog

[Install]:安装配置

WantedBy多用户等

更多介绍阅读:https://www.freedesktop.org/software/systemd/man/systemd.service.html

启动命令:

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

服务状态:

systemctl status openim-api

停止命令:

systemctl stop openim-api

为什么选择 systemd

高级需求:

  • 方便分析问题的服务运行日志记录

  • 服务管理的日志

  • 异常退出时可以根据需要重新启动

daemon不能实现上面的高级需求。

nohup 只能记录服务运行时的输出和出错日志。

只有systemd能够实现上述所有需求。

默认的日志中增加了时间、用户名、服务名称、PID等非常人性化。还能看到服务运行异常退出的日志。还能通过/lib/systemd/system/下的配置文件定制各种需求。

总而言之systemd是目前linux管理后台服务的主流方式所以我新版本的 bash 抛弃 nohup改用 systemd 来管理服务。

1. 前置操作(需要 root 权限)

  1. 根据注释配置 environment.sh

  2. 创建 data 目录

mkdir -p ${OPENIM_DATA_DIR}/{openim-api,openim-crontask}
  1. 创建 bin 目录,并将 openim-apiopenim-crontask 可执行文件复制过去
source ./environment.sh
mkdir -p ${OPENIM_INSTALL_DIR}/bin
cp openim-api openim-crontask ${OPENIM_INSTALL_DIR}/bin
  1. openim-apiopenim-crontask 配置文件拷贝到 ${OPENIM_CONFIG_DIR} 目录下
mkdir -p ${OPENIM_CONFIG_DIR}
cp openim-api.yaml openim-crontask.yaml ${OPENIM_CONFIG_DIR}

2. 创建 openim-api systemd unit 模板文件

执行如下 shell 脚本生成 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 --apiconfig=${OPENIM_CONFIG_DIR}/openim-api.yaml
Restart=always
RestartSec=5
StartLimitInterval=0

[Install]
WantedBy=multi-user.target
EOF

3. 创建 openim-crontask systemd unit 模板文件

...

6. 复制 systemd unit 模板文件到 sysmted 配置目录(需要有root权限)

cp openim-api.service.template /etc/systemd/system/openim-api.service
cp openim-crontask.service.template /etc/systemd/system/openim-crontask.service
...

7. 启动 systemd 服务

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