0%

Ubuntu系统服务命令service和systemctl探究

  1. service和systemctl联系与区别
  2. 命令位置
  3. 处理系统服务时的区别
  4. .service文件位置
  5. systemctl处理系统服务的用法
  6. systemctl edit用法

service和systemctl联系与区别

systemctl属于systemd,是一个系统控制的可执行二进制程序

service专门用于系统服务管理,是一个sh脚本,该脚本内调用了systemctl等命令

命令位置

service

1
2
3
4
whereis service
# service: /usr/sbin/service /usr/share/man/man8/service.8.gz
which service
# /usr/sbin/service

systemctl

1
2
3
4
whereis systemctl
# systemctl: /bin/systemctl /usr/share/man/man1/systemctl.1.gz
which systemctl
# /bin/systemctl

处理系统服务时的区别

以启动服务nginx为例:

1
2
service nginx start  # service [service_name] [command]
systemctl start nginx # systemctl [command] [service_name]

systemctl是一个功能较全的命令,先输入命令,告知要处理的是系统服务相关的任务,再输入服务名称;service专门用于服务处理,因此第一个参数为服务名称,再针对该服务启动相关的命令。

.service文件位置

Ubuntu下,.service文件主要放在以下两个目录中(直接添加就可以作为系统服务运行)

  • /lib/systemd/system
  • /etc/systemd/system

可以通过cat查看系统服务对应的文件位置:

1
2
systemctl cat nginx  # 查看单个,等价于 systemctl cat nginx.service
systemctl cat *.service # 查看全部

systemctl处理系统服务的用法

系统服务通过向指定文件夹添加后缀为.service的文件而添加,并且可以使用systemctl命令进行管理和控制。在systemctl中,系统服务称作“Unit”,管理的命令如下表所示(来自systemctl --help

系统服务相关命令(Unit Commands):

命令 说明
list-units [PATTERN...] 列出内存中所有unit
list-sockets [PATTERN...] List socket units currently in memory, ordered by address
list-timers [PATTERN...] List timer units currently in memory, ordered by next elapse
start NAME... 启动一个或多个服务(activate)
stop NAME... 停止一个或多个服务(deactivate)
reload NAME... 重新加载一个或多个服务
restart NAME... 启动或重启一个或多个服务
try-restart NAME... 如果服务已启动,则尝试重启(一个或多个)
reload-or-restart NAME... 可能的话,重新加载一个或多个服务,否则启动或重启
try-reload-or-restart NAME... 如果一个或多个服务已启动,则重新加载,若不成功,则启动或重新启动
isolate NAME Start one unit and stop all others
kill NAME... Send signal to processes of a unit
is-active PATTERN... Check whether units are active
is-failed PATTERN... Check whether units are failed
status [PATTERN...或PID...] 查看一个或多个服务的运行时状态(日志)
show [PATTERN...或JOB...] Show properties of one or more units/jobs or the manager
cat PATTERN... Show files and drop-ins of one or more units
set-property NAME ASSIGNMENT... Sets one or more properties of a unit
help PATTERN...或PID... Show manual for one or more units
reset-failed [PATTERN...] Reset failed state for all, one, or more units
list-dependencies [NAME] Recursively show units which are required or wanted by this unit or by which this unit is required or wanted

系统服务文件相关命令(Unit file commands):

命令 说明
list-unit-files [PATTERN...] 列出所有的.service文件
enable [NAME...或PATH...] Enable one or more unit files
disable NAME... Disable one or more unit files
reenable NAME... Reenable one or more unit files
preset NAME... Enable/disable one or more unit files based on preset configuration
preset-all Enable/disable all unit files based on preset configuration
is-enabled NAME... Check whether unit files are enabled
mask NAME... Mask one or more units
unmask NAME... Unmask one or more units
link PATH... Link one or more units files into the search path
revert NAME... Revert one or more unit files to vendor version
add-wants TARGET NAME... Add ‘Wants’ dependency for the target on specified one or more units
add-requires TARGET NAME... Add ‘Requires’ dependency for the target on specified one or more units
edit NAME... 编辑一个或多个系统服务文件
get-default Get the name of the default target
set-default NAME Set the default target

systemctl edit用法

该命令可以用来编辑系统服务的配置脚本文件,用法如下:

1
systemctl edit nginx

该命令将打开一个空白文件,该文件位于/etc/systemd/system/nginx.service.d文件夹中,名为override.conf,用来拼接到/etc/systemd/system/nginx.service文件中。

若要直接编辑/etc/systemd/system/nginx.service文件,需要加上--full参数

1
2
3
systemctl edit --full nginx
# 等价于:
nano /etc/systemd/system/nginx.service

如果删除了文件或目录,需要重新加载systemctl,使用命令:

1
systemctl daemon-reload  # Reload systemd manager configuration