Ubuntu使用systemd配置开机运行service

systemd

Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置。

由来

历史上,Linux 的启动一直采用init进程。

下面的命令用来启动服务。

 $ sudo /etc/init.d/apache2 start
 # 或者
 $ service apache2 start

这种方法有两个缺点。

一是启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。

二是启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。

Systemd 概述

Systemd 就是为了解决这些问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。

根据 Linux 惯例,字母d是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。

使用了 Systemd,就不需要再用init了。Systemd 取代了initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。

 $ systemctl --version

上面的命令查看 Systemd 的版本。

使用 systemd 实现开机执行 Shell 脚本

通用操作步骤

创建希望开机马上执行的脚本,本文举例脚本存放位置为 /home/navxin/Example/startup.sh,脚本内容如下:

#!/bin/bash
# 开机时在脚本的同级目录下创建一个名为 StartupTouch.txt 的文件
touch /home/navxin/Example/startup.sh.txt

开机执行的脚本需增加可执行权限才能被 systemd 运行,使用如下命令

chmod u+x /home/navxin/Example/startup.sh
chmod g+x /home/navxin/Example/startup.sh

进入 systemd 放置 service 的目录,在该目录下可看到大量服务配置文件,命令如下

# 进入 systemd 的 service 目录
cd /usr/lib/systemd/system
# 查看文件列表
ls -al
在该目录创建一个新的 .service 文件用于配置开机启动脚本,本例中的文件名为 StartupExample.service,所执行命令和文件中的配置内容如下:

创建服务配置文件

sudo touch /usr/lib/systemd/system/StartupExample.service

以下为 StartupExample.service 配置文件的内容

[Unit]
Description=Startup Example
[Service]
ExecStart=/home/navxin/Example/startup.sh
[Install]
WantedBy=multi-user.target

尝试手动运行新创建的 service,使用如下命令:

# 手动运行 StartupExample.service
sudo systemctl start StartupExample.service
# 查看运行日志
systemctl status StartupExample.service
 # 删除刚测试服务时创建的文件
 rm -f /home/navxin/Example/startup.sh.txt
 # 设置服务为 enable 状态,使之能开机运行
 sudo systemctl enable StartupExample.service
 # 重启机器
 systemctl reboot

附注:Unit

Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位)。

Unit 一共分成12种。

 Service unit:系统服务
Target unit:多个 Unit 构成的一个组
Device Unit:硬件设备
Mount Unit:文件系统的挂载点
Automount Unit:自动挂载点
Path Unit:文件或路径
Scope Unit:不是由 Systemd 启动的外部进程
Slice Unit:进程组
Snapshot Unit:Systemd 快照,可以切回某个快照
Socket Unit:进程间通信的 socket
Swap Unit:swap 文件
Timer Unit:定时器

systemctl list-units命令可以查看当前系统的所有 Unit 。

 # 列出正在运行的 Unit
 $ 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

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

 # 命令用于列出所有配置文件。
 $systemctl list-unit-files
 $systemctl list-unit-files --type=mount

修改 myadmin.service文件,增加语句

 After=network-online.target remote-fs.target nss-lookup.target navxin-kn1.mount
Wants=network-online.target

全部文件内容如下(部分内容参考nginx.service):

 # /lib/systemd/system/myadmin.service
[Unit]
Description=Start myAdmin web server
Documentation=https://www.lyhuilin.com/
After=network-online.target remote-fs.target nss-lookup.target navxin-kn1.mount
Wants=network-online.target
[Service]
Environment="WELCOME=Baicai myAdmin Base Environment."
ExecStartPre=/bin/echo ${WELCOME}
ExecStart=/baicai/systemdStart/my_admin/my_admin -c /baicai/systemdStart/my_admin/conf/config.yaml
ExecStop=/bin/kill -s TERM ${MAINPID}
[Install]
WantedBy=multi-user.target

文章来源:

Author:白菜
link:https://blog.baicai.me/article/2021/ubuntu_systemd_service/