目录
systemd示例
[Unit]
Description=NTA Admin Service
Requires=network.target elasticsearch.service
After=network.target elasticsearch.service
[Service]
Type=forking
WorkingDirectory=/opt/idss/apps/nta_admin
Environment=JAVA_HOME=/opt/idss/jdk
ExecStart=/opt/idss/apps/nta_admin/admin-service.sh
Restart=on-failure
StartLimitInterval=60
StartLimitBurst=3000
[Install]
WantedBy=multi-user.target
注释
[Unit]
主要是对这个服务的说明,内容, 文档介绍以及对一些依赖服务定义#Description:描述信息
#After:表明需要依赖的服务,作用决定启动顺序
#Before:表明被依赖的服务
#Requles:依赖到的其他unit ,强依赖,即依赖的unit启动失败。该unit不启动。
#Wants:依赖到的其他unit,弱依赖,即依赖的unit 启动失败。该unit继续启动
#Conflicts:定义冲突关系[Service]
服务的主体定义,主要定义服务的一些运行参数,及操作动作#Type:
simple: 默认值,执行ExecStart指定的命令,启动主进程
forking: 以 fork 方式从父进程创建子进程,创建后父进程会立即退出,子进程将成为主进程
oneshot: 一次性进程,类似于simple,但只执行一次,Systemd 会等当前服务退出,再继续往下执行
dbus: 当前服务通过D-Bus启动,类似于simple,但会等待 D-Bus 信号后启动
notify: 当前服务启动完毕,会发出通知信号通知Systemd,然后 Systemd 再继续启动其他服务
idle: 类似于simple,但是要等到其他任务都执行完毕,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混合
#User:指定开机自动运行该程序的用户名#Group:指定开机自动运行该程序的用户组
#LimitCORE=infinity:限制内核文件的大小
#LimitNOFILE=65536:服务最大允许打开的文件描述符数量
#LimitNPROC=65536:进程的最大数量
#PIDFile:指定开机自动运行该程序的pid文件(一般在程序配置文件中配置该项)
#ExecStart:启动当前服务的命令
#ExecStartPre:启动当前服务之前执行的命令
#ExecStartPost:启动当前服务之后执行的命令
#ExecReload:重启当前服务时执行的命令
#ExecStop:停止当前服务时执行的命令
#ExecStopPost:停止当其服务之后执行的命令
#KillMode:定义如何停止服务。KillMode字段可以设置的值如下
control-group(默认值):当前控制组里面的所有子进程,都会被杀掉;
process:只杀主进程;
mixed:主进程将收到SIGTERM信号,子进程收到SIGKILL信号;
none:没有进程会被杀掉,只是执行服务的stop命令。如ssh服务将KillMode设为process,不停止任何sshd子进程,即子进程打开的SSH session仍然保持连接,这个设置不太常见,但对 sshd 很重要,否则你停止服务的时候,会连自己打开的 SSH session一起杀掉。
#KillSignal: 设置杀死进程的第一步使用什么信号, 默认值为 SIGTERM 信号。#RestartSec:自动重启当前服务等待的秒数
#Restart:定义了当前服务退出后,Systemd的重启方式,可能的值包括
no(默认值):退出后不会重启;
always:不管是什么退出原因,总是重启;
on-success:只有正常退出时(退出状态码为0),才会重启;
on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启;
on-abnormal:只有被信号终止和超时,才会重启;
on-abort:只有在收到没有捕捉到的信号终止时,才会重启;
on-watchdog:超时退出,才会重启,如ssh服务设置为on-failure,表示任何意外的失败,就将重启sshd。如果sshd正常停止(比如执行systemctl stop命令),它就不会重启。
#RemainAfterExit:值为yes或no,表示进程退出以后,服务仍然保持执行。这样的话,一旦使用systemctl stop命令停止服务,ExecStop指定的命令就会执行#TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
StartLimitIntervalSec=
, StartLimitBurst=#StartLimitInterval =用于设置间隔时长, 默认值等于
DefaultStartLimitIntervalSec=
的值(默认为10秒),设为 0 表示不作限制。#StartLimitBurst=用于设置在一段给定的时长内,最多允许启动多少次, 默认值等于
DefaultStartLimitBurst=
的值(默认为5次)。#Environment:指定当前服务的环境变量
#EnvironmentFile:指定当前服务的环境参数文件,该文件的key=value键值对,可以用$key的形式,在当前配置文件中获取
所有的启动设置都可以加上一个连词号(-),表示"抑制错误",即发生错误的时候,不影响其他命令的执行。比如,EnvironmentFile=-/etc/sysconfig/sshd,表示即使/etc/sysconfig/sshd文件不存在,也不会抛出错误。
[Install]
服务安装的相关设置,一般可设置为多用户的#WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中
#RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中
#Alias:当前 Unit 可用于启动的别名
#Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit
systemd服务命令
[root@bogon ~]# systemctl --help
systemctl [OPTIONS...] {COMMAND} ...
Query or send control commands to the systemd manager.
-h --help Show this help
--version Show package version
--system Connect to system manager
-H --host=[USER@]HOST
Operate on remote host
-M --machine=CONTAINER
Operate on local container
-t --type=TYPE List units of a particular type
--state=STATE List units with particular LOAD or SUB or ACTIVE state
-p --property=NAME Show only properties by this name
-a --all Show all loaded units/properties, including dead/empty
ones. To list all units installed on the system, use
the 'list-unit-files' command instead.
-l --full Don't ellipsize unit names on output
-r --recursive Show unit list of host and local containers
--reverse Show reverse dependencies with 'list-dependencies'
--job-mode=MODE Specify how to deal with already queued jobs, when
queueing a new job
--show-types When showing sockets, explicitly show their type
-i --ignore-inhibitors
When shutting down or sleeping, ignore inhibitors
--kill-who=WHO Who to send signal to
-s --signal=SIGNAL Which signal to send
--now Start or stop unit in addition to enabling or disabling it
-q --quiet Suppress output
--no-block Do not wait until operation finished
--no-wall Don't send wall message before halt/power-off/reboot
--no-reload Don't reload daemon after en-/dis-abling unit files
--no-legend Do not print a legend (column headers and hints)
--no-pager Do not pipe output into a pager
--no-ask-password
Do not ask for system passwords
--global Enable/disable unit files globally
--runtime Enable unit files only temporarily until next reboot
-f --force When enabling unit files, override existing symlinks
When shutting down, execute action immediately
--preset-mode= Apply only enable, only disable, or all presets
--root=PATH Enable unit files in the specified root directory
-n --lines=INTEGER Number of journal entries to show
-o --output=STRING Change journal output mode (short, short-iso,
short-precise, short-monotonic, verbose,
export, json, json-pretty, json-sse, cat)
--plain Print unit dependencies as a list instead of a tree
Unit Commands:
list-units [PATTERN...] List loaded units
list-sockets [PATTERN...] List loaded sockets ordered by address
list-timers [PATTERN...] List loaded timers ordered by next elapse
start NAME... Start (activate) one or more units
stop NAME... Stop (deactivate) one or more units
reload NAME... Reload one or more units
restart NAME... Start or restart one or more units
try-restart NAME... Restart one or more units if active
reload-or-restart NAME... Reload one or more units if possible,
otherwise start or restart
reload-or-try-restart NAME... Reload one or more units if possible,
otherwise restart if active
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 runtime status of one or more units
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...] List installed unit files
enable NAME... 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
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... Edit one or more unit files
get-default Get the name of the default target
set-default NAME Set the default target
Machine Commands:
list-machines [PATTERN...] List local containers and host
Job Commands:
list-jobs [PATTERN...] List jobs
cancel [JOB...] Cancel all, one, or more jobs
Snapshot Commands:
snapshot [NAME] Create a snapshot
delete NAME... Remove one or more snapshots
Environment Commands:
show-environment Dump environment
set-environment NAME=VALUE... Set one or more environment variables
unset-environment NAME... Unset one or more environment variables
import-environment [NAME...] Import all or some environment variables
Manager Lifecycle Commands:
daemon-reload Reload systemd manager configuration
daemon-reexec Reexecute systemd manager
System Commands:
is-system-running Check whether system is fully running
default Enter system default mode
rescue Enter system rescue mode
emergency Enter system emergency mode
halt Shut down and halt the system
poweroff Shut down and power-off the system
reboot [ARG] Shut down and reboot the system
kexec Shut down and reboot the system with kexec
exit Request user instance exit
switch-root ROOT [INIT] Change to a different root file system
suspend Suspend the system
hibernate Hibernate the system
hybrid-sleep Hibernate and suspend the system
常用的就这几项
#查看docker服务的状态
systemctl status docker.service
#停止docker服务
systemctl stop docker.service
#启动docker服务
systemctl start docker.service
#重启docker服务
systemctl restart docker.service
#开机自启docker
systemctl enable docker.service
#禁用docker
systemctl disable docker.service
#重新加载systemd,修改了systemd服务启动程序后需要重新reload一下
systemctl daemon-reload
#列举所有服务
systemctl list-units --type=service
#查看docker用户的相关日志报错信息,适用于服务启动失败后,可使用此命令查看具体启动失败的日志
journalctl -xe -u docker