由于依赖关系更新,启动停止服务

我有两个systemd服务postgresql.serviceapp.servicepostgresql.service是发行版提供的(ubuntu 15.10), app.service是我自己编写的。

由于app需要postgresql ,该服务如下所示:

 [Unit] Description=Start App Requires=postgresql.service After=postgresql.service [Install] WantedBy=multi-user.target [Service] Restart=always ExecStart=/path/to/app 

今天postgresql得到更新。 当然,它自动更新后重新启动postgresql.service 。 但是这杀了我的app.service ,因为它需要postgresql

 # systemctl status app [...] Apr 08 12:04:42 host systemd[1]: Stopping Start App... Apr 08 12:04:42 host systemd[1]: Stopped Start App. 

Apr 08 12:04:42postgresql更新发生的时间。

如何更改app.service ,使其不会在postgresql重新启动时被简单地杀死,但是(重新启动)也会自动启动(当然,只有当启用时)。

或者问一个问题:如何configurationapp.service ,当app.service被启用时,它始终会在postgresql.service之后自动启动? 所以基本上systemctl start postgresql首先启动postgresql ,然后自动app

我想systemd希望我使用Wants而不是Requires 。 从手册:

需要=

configuration其他单元的需求依赖关系。 如果这个单位被激活,这里列出的单位也会被激活。 如果其他单位之一被取消激活或激活失败,则该单位将被停用。 通常,为了在处理失败的服务时实现更健壮的系统,使用“要求=”而不是“要求=”是更好的select。

现在使用Wants ,这似乎是我真的想要一个强大的系统处理失败的服务。 所以我呢。

你可以添加postgresql.service到你的服务WantedBy

 [Unit] Description=Start App Requires=postgresql.service After=postgresql.service [Install] WantedBy=multi-user.target postgresql.service [Service] Restart=always ExecStart=/path/to/app 

不要忘记在systemctl daemon-reload之后systemctl reenable app.service让systemd创build新的符号链接