【笔记】Systemd学习笔记

前言

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

下载依赖

Ubuntu

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 enable <name>

禁止开机自启

1
systemctl disable <name>

创建服务配置文件

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

<desc>:服务描述
<user>:启动服务的用户
<dir_home>:工作目录
<command>:启动程序的命令

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

[Service]
Type=simple
User=<user>
WorkingDirectory=<dir_home>
ExecStart=<command>
RestartForceExitStatus=100

[Install]
WantedBy=multi-user.target

完成

参考文献

CSDN文库
知乎——赵东颖