systemd服务

作者: admin 分类: Linux 发布时间: 2023-07-11 17:42 浏览:1,366 次    

、简介

Systemd 是 Linux 系统工具,用来启动守护进程,Systemd服务缩短了启动时间,是为解决init缺点而诞生的一套完整的解决方案。Systemd 取代了initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。

二、Systemd脚本

Systemd脚本分为三部分:[Unit]、[Service]、[Install],以xxx.service结尾

  • [Unit] 区块通常是配置文件的第一个区块,用来定义 Unit 的元数据,以及配置与其他 Unit 的关系。
Description:简短描述
Documentation:文档地址
Requires:当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败
Wants:与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败
BindsTo:与Requires类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行
Before:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动
After:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动
Conflicts:这里指定的 Unit 不能与当前 Unit 同时运行
Condition…:当前 Unit 运行必须满足的条件,否则不会运行
Assert…:当前 Unit 运行必须满足的条件,否则会报启动失败
  • [Service] 区块通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动。
WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中
multi-user.target: # 表示多用户命令行状态,这个设置很重要
graphical.target:  # 表示图形用户状体,它依赖于multi-user.target
RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中
Alias:当前 Unit 可用于启动的别名
Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit
  • [Service] 区块用来 Service 的配置,只有 Service 类型的 Unit 才有这个区块。
Type:定义启动时的进程行为。它有以下几种值。
Type=simple:默认值,执行ExecStart指定的命令,启动主进程
Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
Type=dbus:当前服务通过D-Bus启动
Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
Type=idle:若有其他任务执行完毕,当前服务才会运行
ExecStart:启动当前服务的命令
ExecStartPre:启动当前服务之前执行的命令
ExecStartPost:启动当前服务之后执行的命令
ExecReload:重启当前服务时执行的命令
ExecStop:停止当前服务时执行的命令
ExecStopPost:停止当其服务之后执行的命令
RestartSec:自动重启当前服务间隔的秒数
Restart:定义何种情况 Systemd 会自动重启当前服务 
    no(默认值): # 退出后无操作
    on-success:  # 只有正常退出时(退出状态码为0),才会重启
    on-failure:  # 非正常退出时,重启,包括被信号终止和超时等
    on-abnormal: # 只有被信号终止或超时,才会重启
    on-abort:    # 只有在收到没有捕捉到的信号终止时,才会重启
    on-watchdog: # 超时退出时,才会重启
    always:      # 不管什么退出原因,都会重启(除了systemctl stop)
    # 对于守护进程,推荐用on-failure
KillMode的类型:
    control-group(默认):# 当前控制组里的所有子进程,都会被杀掉
    process: # 只杀主进程
    mixed:   # 主进程将收到SIGTERM信号,子进程收到SIGKILL信号
    none:    # 没有进程会被杀掉,只是执行服务的stop命令
TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
Environment:指定环境变量

三、示例

  • 示例1:基本模板样式
[Unit]   # 服务说明
Description=service     # 描述服务
After=network.target    # 服务类别,表示本服务需要在network服务启动后在启动
Before=xxx.service      # 表示需要在某些服务启动之前启动,After和Before字段只涉及启动顺序,不涉及依赖关系。
 
[Service]  # 服务的关键
Type=forking     # 表示后台运行模式
PIDFile=/path/xx.pid    # PID的绝对路径
ExecStart=/path/xxx.sh    # 服务启动命令(绝对路径)
ExecReload=xxx  # 重启命令
ExecStop=xxx    # 停止命令
PrivateTmp=true                               # 表示给服务分配独立的临时空间
   
[Install]   
WantedBy=multi-user.target  # 多用户
  • 示例2:Nginx启动脚本
 [Unit]
 Description=The nginx HTTP and reverse proxy server
 After=network.target remote-fs.target nss-lookup.target

[Service]
 Type=forking
 PIDFile=/usr/local/nginx/logs/nginx.pid
 ExecStartPre=/usr/bin/rm -f /run/nginx.pid
 ExecStartPre=/usr/local/nginx/sbin/nginx -t
 ExecStart=/usr/local/nginx/sbin/nginx
 ExecReload=/bin/kill -s HUP $MAINPID
 KillMode=process
 KillSignal=SIGQUIT
 TimeoutStopSec=5
 PrivateTmp=true

[Install]
 WantedBy=multi-user.target
  • 示例3:挂载 NFS
[Unit]
After=network.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/bin/mount -t nfs -o defaults ip:/path/myfile /mnt
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target

四、存放目录

/usr/lib/systemd/system # 系统服务存放目录(无需登录可运行,可开机自启)
/usr/lib/systemd/user   # 用户服务存放目录(需要登录后才能运行程序)
systemd-analyze verify xxxx.service  # 检查service配置文件正确性
systemctl daemon-reload              # 重载配置文件

五、Systemd 命令管理

  • systemctl命令用于管理系统
systemctl reboot                   # 重启系统
systemctl poweroff                 # 关闭系统,切断电源
systemctl rescue                   # 进入救援状态(单用户状态)
systemctl enable xxxx.service      # 设置开机启动 
systemctl start  xxxx.service      # 启动服务
systemctl restart xxxx.service     # 重启服务
systemctl stop xxxx.service        # 停止服务
systemctl disable xxxx.service     # 禁用开机启动
systemctl kill xxxx.service        # 杀死服务的所有子进程
systemctl reload xxxx.service      # 重新加载服务的配置文件
systemctl daemon-reload            # 重载所有修改过的配置文件
  • systemd-analyze命令用于查看启动耗时
systemd-analyze                              # 查看启动耗时
systemd-analyze blame                        # 查看每个服务的启动耗时
systemd-analyze critical-chain atd.service   # 显示指定服务的启动流
  • hostnamectl命令用于设置主机名
hostnamectl set-hostname qunniao_server      # 设置主机名
hostnamectl                                  # 查看当前主机名
  • localectl命令用于查看本地化设置
localectl                                    # 查看本地化设置
localectl set-locale LANG=en_GB.utf8         # 设置本地化字符集环境变量
localectl list-locales                       # 列出本地所有字符集
localectl set-keymap en_GB                   # 设置控制台的键盘映射
localectl list-keymaps                       # 列出所有可用的控制台键盘映射
  • timedatectl命令用于时区设置
timedatectl                                 # 查看当前时区
timedatectl list-timezones                  # 查看所有可用时区
timedatectl set-timezone Asia/Shanghai      # 设置当前时区
timedatectl set-time YYYY-MM-DD             # 设置日期
timedatectl set-time HH:MM:SS               # 设置时间
  • loginctl命令用于查看当前登录用户
loginctl list-sessions                     # 查看当前session
loginctl list-users                        # 查看当前登录用户
loginctl show-user root                    # 查看指定用户信息

参考文章:

http://localnetwork.cn/project-3/doc-297/

https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html


温馨提示:如无特殊说明,本站文章均为作者原创,转载时请注明出处及相应链接!

发表评论