使用Systemd管理应用

· Read in about 2 min · (310 Words)
dev work

关于systemd

Systemd在新版Linux操作系统(Ubuntu 15.04+, RHEL 7+, Debian 8+)里基本都已成为内置软件

systemctl --version   # 查看systemd版本号

Unit

Systemd以Unit作为基本管理单元,用户级的 SERVICE.service 文件一般可存放于/etc/systemd/system/目录。

其支持类型有:SERVICE.service, SOCKET.socket, DEVICE.device, MOUNT.mount, AUTOMOUNT.automount, SWAP.swap, TARGET.target, PATH.path, TIMER.timer, SLICE.slice, SCOPE.scope

systemctl list-unit-files            # 查看所有已安装服务
systemctl list-units --type=service  # 列出所有正在运行的、类型为 service 的 Unit

管理自定义的应用一般只需要用到SERVICE.service配置文件。

服务配置文件

此处使用了一个简单的shell脚本,位于/usr/local/bin/hello 其内容为:

#!/bin/sh
echo “This is hello program!”

服务的配置文件一般格式如下:

[Unit]
# 应用描述
Description=A test program
# 应用文档地址
Documentation=https://docs.example.com
# 必须在某一个或多个单元之后启动
After=network.target ssh.service
# 依赖于某一个或多个单元
Requires=ssh.service

[Service]
# 进程启动方式,有:simple(默认), forking, oneshot, notify, dbus, idle这几种方式
Type=simple
# 启动服务之前执行的命令
ExecStartPre=/usr/bin/which hello
# 启动程序
ExecStart=/user/local/bin/hello
# 启动服务之后执行的命令
ExecStartPost=/bin/echo "start hello success."
# 停止服务时执行的命令
ExecStop=/bin/echo "stop hello"
# 重启服务时执行的命令
ExecReload=/bin/kill -s HUP $MAINPID
# 进程结束后重启方式,可选值有:always, on-success, on-failure, on-abnormal, on-abort, on-watchdog
Restart=on-failure
# 重启间隔秒数
RestartSec=30
# 标准输出到
StandardOutput=syslog
# 标准错误输出到
StandardError=syslog
# 设置syslog中log的程序名称
SyslogIdentifier=helloexample
# 设置syslog中log类型
SyslogFacility=local0
# 设置syslog中log级别,此处为info
SyslogLevel=info
# 程序运行时的用户
User=root
# 程序运行时分配的组
Group=root
# 程序的环境变量
Environment=NODE_ENV=production

# 依赖于
[Install]
WantedBy=multi-user.target

将上面配置写入到/etc/systemd/system/hello.service中,然后执行命令

systemctl daemon-reload
systemctl enable hello.service

这样即可加载服务到systemd中。

检查服务是否已加入到systemd管理

systemctl status hello.service
#● hello.service - A test program
#   Loaded: loaded (/lib/systemd/system/hello.service; enabled; vendor preset: enabled)
#   Active: inactive (dead)
#     Docs: https://docs.example.com

启动服务

systemctl start hello.service

查看hello服务输出

journalctl -u hello.service
# 1) 没有使用SyslogIdentifier时的输出
#Feb 07 14:19:40 i-rcaex55q systemd[1]: Starting A test program...
#Feb 07 14:19:40 i-rcaex55q which[27364]: /usr/local/bin/hello
#Feb 07 14:19:40 i-rcaex55q echo[27369]: start hello success.
#Feb 07 14:19:40 i-rcaex55q systemd[1]: Started A test program.
#Feb 07 14:19:40 i-rcaex55q hello[27368]: This is hello program!
#Feb 07 14:19:40 i-rcaex55q echo[27372]: stop hello
#Feb 07 14:22:30 i-rcaex55q systemd[1]: Stopped A test program.
# 2) 使用了SyslogIdentifier=helloexample时的输出
#Feb 07 14:22:39 i-rcaex55q systemd[1]: Starting A test program...
#Feb 07 14:22:39 i-rcaex55q helloexample[27419]: /usr/local/bin/hello
#Feb 07 14:22:39 i-rcaex55q helloexample[27423]: start hello success.
#Feb 07 14:22:39 i-rcaex55q helloexample[27422]: This is hello program!
#Feb 07 14:22:39 i-rcaex55q helloexample[27425]: stop hello
#Feb 07 14:22:39 i-rcaex55q systemd[1]: Started A test program.
#Feb 07 14:30:16 i-rcaex55q systemd[1]: Stopped A test program.

参考

Comments