systemctl

systemctl 用户shell的环境变量是不生效的

一、常用命令
systemctl daemon-reload
刚刚配置的服务需要让systemctl能识别,就必须刷新配置
systemctl enable nginx.service
设置开机自启动
systemctl disable nginx.service
停止开机自启动
systemctl kill nginx.service
杀死服务
----------
systemctl start nginx.service
systemctl restart nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl status nginx.service
----------

------------------------------------------------------------

二、配置文件举例
systemctl cat nginx.service
----------
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
----------
Target的含义是服务组,表示一组服务。WantedBy=multi-user.target指的是,sshd 所在的 Target 是multi-user.target。
这个设置非常重要,因为执行systemctl enable sshd.service命令时,sshd.service的一个符号链接,就会放在/etc/systemd/system目录下面的multi-user.target.wants子目录之中。

每一个 Unit 都有一个配置文件,告诉 Systemd 怎么启动这个 Unit 。
Systemd 默认从目录/etc/systemd/system/读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录/usr/lib/systemd/system/,真正的配置文件存放在那个目录。
systemctl enable命令用于在上面两个目录之间,建立符号链接关系。
sudo systemctl enable nginx.service
等同于
sudo ln -s '/usr/lib/systemd/system/nginx.service' '/etc/systemd/system/multi-user.target.wants/nginx.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 运行必须满足的条件,否则会报启动失败

[Install]通常是配置文件的最后一个区块,用来定义如何启动,以及是否开机启动。它的主要字段如下:

    WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中
    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 会自动重启当前服务,可能的值包括always(总是重启)、on-success、on-failure、on-abnormal、on-abort、on-watchdog
    TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
    Environment:指定环境变量

------------------------------------------------------------

五、传送门
https://blog.csdn.net/jiankunking/article/details/79826025
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html
http://www.jinbuguo.com/systemd/systemd.service.html
https://linux.cn/article-5926-1.html

原文地址:https://www.cnblogs.com/lazy-sang/p/11513095.html