bash Systemd 脚本在 ExecStart 之后立即执行 ExecStop
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30640717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Systemd script does ExecStop right after ExecStart
提问by S4rg0n
Here's my Systemd script:
这是我的 Systemd 脚本:
[Unit]
Description=RDS Services
[Service]
WorkingDirectory=/home/rdsdb2/script_rds/
Type=oneshot
ExecStart=/bin/bash start_services.sh
ExecStop=/bin/bash stop_services.sh
KillMode=process
[Install]
WantedBy=multi-user.target
I can't figure out why it executes sequentially (at system start or when i start it manually) ExecStart and ExecStop.
我无法弄清楚它为什么按顺序执行(在系统启动时或当我手动启动它时)ExecStart 和 ExecStop。
Can you help me?
你能帮助我吗?
Thanks in advance.
提前致谢。
回答by Christophe
Type=oneshotis used for units, such as a filesystem check or a cleanup, which execute an action without keeping active processes. Such systemd units will wail until the process specified by ExecStartterminates, and then deactivate by running the process specified by ExecStop.
Type=oneshot用于单元,例如文件系统检查或清理,它们在不保持活动进程的情况下执行操作。这样的 systemd 单元将一直等待,直到ExecStart指定的进程终止,然后通过运行ExecStop指定的进程停用。
Type=simple(the default setting) is used when the process configured with ExecStartis the main process of the service. Such units will wait until the process specified by ExecStartreturns, then deactivate by running the process specified by ExecStop.
Type=simple(默认设置)用于配置ExecStart的进程是服务的主进程。这些单元将等到ExecStart指定的进程返回,然后通过运行ExecStop指定的进程停用。
With RemainAfterExit=yes, the service will be considered active even when all its processes have returned, and therefore the process specified by ExecStopwill not run automatically. However, this setting is not recommended since the service will still appear as being active even if it has crashed. This setting is disabled by default.
使用RemainAfterExit=yes,即使其所有进程都已返回,该服务也将被视为处于活动状态,因此ExecStop指定的进程将不会自动运行。但是,不建议使用此设置,因为即使服务崩溃,它仍会显示为活动状态。默认情况下禁用此设置。
Type=forkingis used when the process specified by ExecStartis expected to exit after start-up is complete, while its child process(es) continue(s) to run in the background. This is the behavior of traditional UNIX daemons and the recommended choice in your case. The ExecStopsetting is optional and is used to communicate with the service for a clean termination. The process specified by ExecStopwill run in case the service crashes. In the absence of any ExecStopoption, the systemctl stop servicenamecommand will simply kill the remaining processes of the unit, as specified by the KillModeoption.
当ExecStart指定的进程预计在启动完成后退出,而其子进程继续在后台运行时,使用Type=forking。这是传统 UNIX 守护程序的行为,也是您的情况下的推荐选择。该ExecStop设置是可选的,并使用一个干净的终止服务进行通信。ExecStop指定的进程将在服务崩溃时运行。在没有任何ExecStop选项的情况下,systemctl stop servicename命令将简单地终止单元的剩余进程,如KillMode选项指定的那样。
回答by xsor
if you run
如果你跑
[Service]
Type=simple
than you need: RemainAfterExit=yes
比你需要的: RemainAfterExit=yes
OR use forking:
或使用分叉:
[Service]
Type=forking
回答by sylvek
just add: RemainAfterExit=yes it work like a charm
只需添加: RemainAfterExit=yes 它就像一个魅力