Linux下使用systemctl对程序(服务)进行管理,包括启动、停止、查看状态、开机启动。

对于使用systemctl进行管理的程序,需要在/etc/systemd/system/目录下创建一个服务配置文件,以.service为后缀,注意Linux下不以后缀区分文件类型,后缀只是为了方便识别,具体可自行查询了解。下面所使用服务名称为服务配置文件名称,如 ***.service

注意:该命令需要使用root用户进行操作,非root用户需要在命令前增加 sudo ,提权。

1. 启动

systemctl start  服务名称

2. 查看状态

systemctl status 服务名称

3. 停止

systemctl stop 服务名称

4. 开机启动

#设置服务开机启动

systemctl enable 服务名称

#取消服务开机启动

systemctl disable 服务名称

5. 其他常用命令

 systemctl reboot    # 重启系统

systemctl poweroff   # 关闭系统,切断电源

systemctl halt      # CPU停止工作

systemctl suspend     # 暂停系统

systemctl hibernate     # 让系统进入冬眠状态

systemctl hybrid-sleep    # 让系统进入交互式休眠状态

systemctl rescue    # 启动进入救援状态(单用户状态)

systemctl list-units                    #查看系统中所有正在运行的服务

systemctl list-unit-files        #查看系统中所有服务的开机启动状态

systemctl list-dependencies 服务名          #查看系统中服务的依赖关系

systemctl mask 服务名                                   #冻结服务

systemctl unmask 服务名                               #解冻服务

systemctl set-default multi-user.target          #开机时不启动图形界面

systemctl set-default graphical.target            #开机时启动图形界面

systemctl daemon-reload                          #修改服务配置文件后需要

systemctl is-active application.service            # 显示某个 Unit 是否正在运行

systemctl is-failed application.service             # 显示某个 Unit 是否处于启动失败状态

systemctl is-enabled application.service         # 显示某个 Unit 服务是否建立了启动链接

6. 服务配置文件内容(简单版,更多配置请查询相关文档)

[Unit]

Description=服务简介,自定义

[Service]

Type=simple

WorkingDirectory=启动程序目录

ExecStart=启动程序命令

#ExecReload=重启命令,可选,不设置即使用启动命令

Restart=always #是否自动重启

# Restart service after 10 seconds if the dotnet service crashes:

#Restart=on-failure

RestartSec=10

KillSignal=SIGINT

SyslogIdentifier=dotnet-example

#User=www-data

#Environment=ASPNETCORE_ENVIRONMENT=Production

#Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]

WantedBy=multi-user.target

7. 服务配置文件文档

7.1 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 运行必须满足的条件,否则会报启动失败

7.2 Service 块

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:指定环境变量

7.3 Install 块

WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中
RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中
Alias:当前 Unit 可用于启动的别名
Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit

文章信息

创建时间
2024-05-31
作者
郭铭心
是否所有人可见
所有人可见
最后修改日期
2024-05-31
点击数
304
标签