systemd 是一个 Linux 系统的服务管理软件,可以管理系统服务。
systemctl daemon-reload
systemctl status [service 文件名称]
示例:
systemctl status docker.service
systemctl start [service 文件名称]
示例:
systemctl start docker.service
systemctl stop [service 文件名称]
示例:
systemctl stop docker.service
systemctl restart [service 文件名称]
示例:
systemctl restart docker.service
systemctl enable [service 文件名称]
示例:
systemctl enable docker.service
systemctl disable [service 文件名称]
示例:
systemctl disable docker.service
/usr/lib/systemd/system/
或
/etc/systemd/system/
其中
/etc/systemd/system/
目录中的 service 文件在开机时自执行
# 定义 Unit 的元数据,以及配置与其他 Unit 的关系
[Unit]
## 当前 service 文件的描述
Description=This is description
## 文档地址
Documentation=https://wiki.hty1024.com
## 当前 Service 依赖的其他 Service,如果没有会启动失败,多个时用空格间隔
Requires=a.target b.service
## 当前 Service 依赖的其他 Service,如果没有不会启动失败,多个时用空格间隔
Wants=c.service
## 如果这些 Service 停止,则当前 Service 停止,多个时用空格间隔
BindsTo=d.target
## 这些 Service 不能和当前 Service 同时启动,多个时用空格间隔
Conflicts=e.target
## 这些 Service 需要在当前 Service 之后启动,多个时用空格间隔
Before=f.target
## 这些 Service 需要在当前 Service 之前启动,多个时用空格间隔
After=g.target
## 当前 Service 必须满足的条件,如果没有不会启动,多个时用空格间隔
Condition=h.target
## 当前 Service 必须满足的条件,如果没有会启动失败,多个时用空格间隔
Assert=i.target
# 定义 Service 的配置
[Service]
## 当前 Service 的类型,可选值:simple:启动主进程(默认);forking:启动子进程;oneshot:一次性进程;dbus:D-Bus进程;notify:当前Service启动后会通知Systemd;idle:当其他任务执行完毕后,当前Service才会启动
Type=forking
## 指定环境变量
Environment=
## 启动当前 Service 之前执行的命令
ExecStartPre=
## 启动 Service 的命令
ExecStart=
## 启动当前 Service 之后执行的命令
ExecStartPost=
## 重启当前 Service 的命令
ExecReload=
## 停止当前 Service 的命令
ExecStop=
## 停止当前 Service 之后执行的命令
ExecStopPost=
## 自动重启当前 Service 间隔秒数
RestartSec=
## 当前 Service 的重启规则,可选值:always:总是重启;on-success:成功时重启;on-failure:失败时重启;on-abnormal:不正常时重启;on-abort:中断时重启;on-watchdog:指定监察器
Restart=
## 停止当前 Service 之前等待的秒数
TimeoutSec=
# 定义如何启动,以及是否开机启动
[Install]
## 当前 Service 的别名
Alias=
## 当前 Service 需要的其他 Service,多个时用空格间隔
WangtedBy=
## 当前 Service 依赖的其他 Service,多个时用空格间隔
RequiredBy=
## 当前 Service 关联的其他 Service,多个时用空格间隔
Also=
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500
[Install]
WantedBy=multi-user.target
[Unit]
Description=Redis Server
Documentation=https://redis.io
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=forking
Group=root
User=root
ExecStart=/opt/redis/bin/redis-server /opt/redis/redis.conf
Restart=always
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]
Description=Apache Tomcat
Documentation=https://tomcat.apache.org
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=forking
Group=root
User=root
ExecStart=/opt/tomcat/apache-tomcat-9.0.89/bin/startup.sh
Restart=always
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]
Description=Nginx
Documentation=https://nginx.org
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=forking
Group=root
User=root
ExecStart=/opt/nginx/1.26.0/sbin/nginx
Restart=always
PrivateTmp=true
[Install]
WantedBy=multi-user.target