【笔记】Systemd学习笔记

前言

Linux通过systemd的systemctl命令管理服务

下载依赖

Debian

1
apt install systemd

CentOS

1
apt install systemd

启动服务

<name>:服务名,可以以.service结尾,也可以省略

1
systemctl start <name>

关闭服务

1
systemctl stop <name>

重启服务

1
systemctl restart <name>

查看服务运行状态

1
systemctl status <name>

判断开机自欺状态

1
systemctl is-enabled <name>

启用开机自启

1
systemctl enable <name>

禁止开机自启

1
systemctl disable <name>

创建服务配置文件

  • /etc/systemd/system/目录下创建一个以.service为后缀名的文件作为服务配置文件

Description=<desc>:定义服务描述
Type=simple:定义服务类型
User=<user>:启动服务的用户,缺省值为root用户
WorkingDirectory=<dir_home>:定义工作目录
ExecStartPre=<command>:启动程序之前需要运行的程序,可以定义多个
ExecStart=<command>:启动程序的命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=<desc>
After=network-online.target

[Service]
Type=simple
User=<user>
PIDFile=/run/<service_name>.pid
WorkingDirectory=<dir_home>
ExecStartPre=<command>
ExecStartPre=<command>
ExecStart=<command>

[Install]
WantedBy=multi-user.target

完成

参考文献

CSDN文库
知乎——赵东颖