Systemd:如何在另一个服务启动后启动服务

Jac*_*ack 13 systemd ubuntu-18.04 systemd-service

我有这两项服务,一项是Google启动脚本服务,第二项是redis服务,我想在启动脚本服务启动并完成后启动redis服务,我有以下systemd配置,但我的redis服务无法启动有了这些配置

google-startup-scripts.service
[Unit]
Description=Google Compute Engine Startup Scripts
After=network-online.target network.target rsyslog.service
After=google-instance-setup.service google-network-daemon.service
After=cloud-final.service multi-user.target
Wants=cloud-final.service
After=snapd.seeded.service
Wants=snapd.seeded.service

[Service]
RemainAfterExit=yes
ExecStart=/usr/bin/google_metadata_script_runner --script-type startup
KillMode=process
Type=oneshot
StandardOutput=journal+console
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
redis.service

[Unit]
Description=Redis In-Memory Data Store
After=google-startup-scripts.service

[Service]
Type=notify
PIDFile=/run/redis-6378.pid
ExecStart=/usr/bin/redis-getdevice /etc/redis-getdevice/6378.conf
ExecStop=/usr/bin/redis-cli -p 6378 shutdown
Restart=always

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

一旦 google-startup-script.service 运行并执行操作并进入退出状态。并且 redis 服务根本没有启动(我After在单元中使用)我在这里做错了什么

Bob*_*Bob 16

有许多不同的关键字来指定 systemd 单元依赖性。每个在失败处理方面都有稍微不同的效果和含义,例如“启动”和“等待启动完成”:

Wants

配置对其他单元的(弱)需求依赖性。... 如果配置单元是,则将启动此选项中列出的单元。...

Requires

与 Wants= 类似,但声明了更强的需求依赖性。...如果该单位被激活,列出的单位也将被激活。如果其他单元之一无法激活,并且设置了对失败单元的排序依赖性 After=,则该单元将不会启动。...

After

...如果 unitfoo.service包含该设置Before=bar.service并且两个单元都正在启动,bar.service则 的启动将被延迟,直到 foo.service 完成启动。After=是 的倒数Before=,即 whileBefore=确保在列出的单元开始启动之前启动配置的单元,After=确保相反,在启动配置的单元之前列出的单元完全启动。...

在这方面,您的 redid.service 单元具有依赖性After=google-startup-scripts.service,看起来是正确的。

您的问题似乎是 google-startup-scripts.service 是Type=oneshot。主ExecStart= 进程退出后,systemd 服务管理器将考虑该单元。

可能发生的情况是,脚本触发任务在后台启动和运行,并且不等待这些任务完成(或完成启动)。该脚本将继续执行后续步骤,然后成功完成,systemd 认为该单元已启动,但一个或多个进程尚未完成。

您可能需要对正在运行的任何内容进行调整/usr/bin/google_metadata_script_runner --script-type startup