Systemd optional service dependency that checks for error





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I'm trying to rewrite an init script to systemd, but I'm struggling with an issue of optional dependencies.



My service depends (optionally) on two other services. These two dependencies are mutually exclusive (server and client package) - only one can be installed at a time, and can be missing completely.



If either of the dependencies is installed, the dependency should be started before my service, and if the start fails, my service should not start.



But if neither of the dependencies is installed, my service should start normally.



How can I achieve this functionality? Optional dependencies are handled through Wants=, but this ignores errors.










share|improve this question

























  • Please do clarify.Your service depends on the package installation or on starting of the other services?

    – Thushi
    Aug 17 '15 at 12:09











  • @WitchDoctor I explicitly wrote "they should be started and if the start fails, the service should not start". You could technically check for installed files in a systemd service description, but that would be pretty ridiculous.

    – Let_Me_Be
    Aug 17 '15 at 12:16











  • Yeah. So your service depends on other two services. And either one of them start fails then your service should not start. And if both the services start then your service should start normally. Am I right?

    – Thushi
    Aug 17 '15 at 12:22











  • @WitchDoctor OMG, seriously read the question. On each machine, there will be either none of the dependencies installed, in which case, the service should start normally, or there will be one of them installed (since they conflict with each other), in which case, it should be treated as a hard dependency (do not start the service on fail).

    – Let_Me_Be
    Aug 17 '15 at 12:31











  • Now I got it. Thanks. Anyhow I will just confirm. If the dependency is installed then it should start before your service and by chance if it fails your service should not start and if the dependency service is not installed at all then your service should start normally.Right? :)

    – Thushi
    Aug 17 '15 at 13:45


















1















I'm trying to rewrite an init script to systemd, but I'm struggling with an issue of optional dependencies.



My service depends (optionally) on two other services. These two dependencies are mutually exclusive (server and client package) - only one can be installed at a time, and can be missing completely.



If either of the dependencies is installed, the dependency should be started before my service, and if the start fails, my service should not start.



But if neither of the dependencies is installed, my service should start normally.



How can I achieve this functionality? Optional dependencies are handled through Wants=, but this ignores errors.










share|improve this question

























  • Please do clarify.Your service depends on the package installation or on starting of the other services?

    – Thushi
    Aug 17 '15 at 12:09











  • @WitchDoctor I explicitly wrote "they should be started and if the start fails, the service should not start". You could technically check for installed files in a systemd service description, but that would be pretty ridiculous.

    – Let_Me_Be
    Aug 17 '15 at 12:16











  • Yeah. So your service depends on other two services. And either one of them start fails then your service should not start. And if both the services start then your service should start normally. Am I right?

    – Thushi
    Aug 17 '15 at 12:22











  • @WitchDoctor OMG, seriously read the question. On each machine, there will be either none of the dependencies installed, in which case, the service should start normally, or there will be one of them installed (since they conflict with each other), in which case, it should be treated as a hard dependency (do not start the service on fail).

    – Let_Me_Be
    Aug 17 '15 at 12:31











  • Now I got it. Thanks. Anyhow I will just confirm. If the dependency is installed then it should start before your service and by chance if it fails your service should not start and if the dependency service is not installed at all then your service should start normally.Right? :)

    – Thushi
    Aug 17 '15 at 13:45














1












1








1








I'm trying to rewrite an init script to systemd, but I'm struggling with an issue of optional dependencies.



My service depends (optionally) on two other services. These two dependencies are mutually exclusive (server and client package) - only one can be installed at a time, and can be missing completely.



If either of the dependencies is installed, the dependency should be started before my service, and if the start fails, my service should not start.



But if neither of the dependencies is installed, my service should start normally.



How can I achieve this functionality? Optional dependencies are handled through Wants=, but this ignores errors.










share|improve this question
















I'm trying to rewrite an init script to systemd, but I'm struggling with an issue of optional dependencies.



My service depends (optionally) on two other services. These two dependencies are mutually exclusive (server and client package) - only one can be installed at a time, and can be missing completely.



If either of the dependencies is installed, the dependency should be started before my service, and if the start fails, my service should not start.



But if neither of the dependencies is installed, my service should start normally.



How can I achieve this functionality? Optional dependencies are handled through Wants=, but this ignores errors.







systemd services






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 17 '15 at 13:19







Let_Me_Be

















asked Aug 17 '15 at 11:32









Let_Me_BeLet_Me_Be

4,57983258




4,57983258













  • Please do clarify.Your service depends on the package installation or on starting of the other services?

    – Thushi
    Aug 17 '15 at 12:09











  • @WitchDoctor I explicitly wrote "they should be started and if the start fails, the service should not start". You could technically check for installed files in a systemd service description, but that would be pretty ridiculous.

    – Let_Me_Be
    Aug 17 '15 at 12:16











  • Yeah. So your service depends on other two services. And either one of them start fails then your service should not start. And if both the services start then your service should start normally. Am I right?

    – Thushi
    Aug 17 '15 at 12:22











  • @WitchDoctor OMG, seriously read the question. On each machine, there will be either none of the dependencies installed, in which case, the service should start normally, or there will be one of them installed (since they conflict with each other), in which case, it should be treated as a hard dependency (do not start the service on fail).

    – Let_Me_Be
    Aug 17 '15 at 12:31











  • Now I got it. Thanks. Anyhow I will just confirm. If the dependency is installed then it should start before your service and by chance if it fails your service should not start and if the dependency service is not installed at all then your service should start normally.Right? :)

    – Thushi
    Aug 17 '15 at 13:45



















  • Please do clarify.Your service depends on the package installation or on starting of the other services?

    – Thushi
    Aug 17 '15 at 12:09











  • @WitchDoctor I explicitly wrote "they should be started and if the start fails, the service should not start". You could technically check for installed files in a systemd service description, but that would be pretty ridiculous.

    – Let_Me_Be
    Aug 17 '15 at 12:16











  • Yeah. So your service depends on other two services. And either one of them start fails then your service should not start. And if both the services start then your service should start normally. Am I right?

    – Thushi
    Aug 17 '15 at 12:22











  • @WitchDoctor OMG, seriously read the question. On each machine, there will be either none of the dependencies installed, in which case, the service should start normally, or there will be one of them installed (since they conflict with each other), in which case, it should be treated as a hard dependency (do not start the service on fail).

    – Let_Me_Be
    Aug 17 '15 at 12:31











  • Now I got it. Thanks. Anyhow I will just confirm. If the dependency is installed then it should start before your service and by chance if it fails your service should not start and if the dependency service is not installed at all then your service should start normally.Right? :)

    – Thushi
    Aug 17 '15 at 13:45

















Please do clarify.Your service depends on the package installation or on starting of the other services?

– Thushi
Aug 17 '15 at 12:09





Please do clarify.Your service depends on the package installation or on starting of the other services?

– Thushi
Aug 17 '15 at 12:09













@WitchDoctor I explicitly wrote "they should be started and if the start fails, the service should not start". You could technically check for installed files in a systemd service description, but that would be pretty ridiculous.

– Let_Me_Be
Aug 17 '15 at 12:16





@WitchDoctor I explicitly wrote "they should be started and if the start fails, the service should not start". You could technically check for installed files in a systemd service description, but that would be pretty ridiculous.

– Let_Me_Be
Aug 17 '15 at 12:16













Yeah. So your service depends on other two services. And either one of them start fails then your service should not start. And if both the services start then your service should start normally. Am I right?

– Thushi
Aug 17 '15 at 12:22





Yeah. So your service depends on other two services. And either one of them start fails then your service should not start. And if both the services start then your service should start normally. Am I right?

– Thushi
Aug 17 '15 at 12:22













@WitchDoctor OMG, seriously read the question. On each machine, there will be either none of the dependencies installed, in which case, the service should start normally, or there will be one of them installed (since they conflict with each other), in which case, it should be treated as a hard dependency (do not start the service on fail).

– Let_Me_Be
Aug 17 '15 at 12:31





@WitchDoctor OMG, seriously read the question. On each machine, there will be either none of the dependencies installed, in which case, the service should start normally, or there will be one of them installed (since they conflict with each other), in which case, it should be treated as a hard dependency (do not start the service on fail).

– Let_Me_Be
Aug 17 '15 at 12:31













Now I got it. Thanks. Anyhow I will just confirm. If the dependency is installed then it should start before your service and by chance if it fails your service should not start and if the dependency service is not installed at all then your service should start normally.Right? :)

– Thushi
Aug 17 '15 at 13:45





Now I got it. Thanks. Anyhow I will just confirm. If the dependency is installed then it should start before your service and by chance if it fails your service should not start and if the dependency service is not installed at all then your service should start normally.Right? :)

– Thushi
Aug 17 '15 at 13:45










2 Answers
2






active

oldest

votes


















1














If you have control over the dependencies (i. e. they are your software), make them install a drop-in file for the unit file in question. That is, let's say you have foo.service which needs to depend on bar.service || baz.service.



Then the bar package can install a file /usr/lib/systemd/system/foo.service.d/50-bar-baz.conf with these contents:



[Unit]
Requires=bar.service


Similarly for baz package.





If you have no control over the dependencies, then simply make a "dispatcher" script which checks unit files for existence and then does an exec systemctl start on the chosen unit file. Then write a separate unit and require/after it from the main unit:



[Unit]
# inexistent unit names are ignored here
After=bar.service baz.service

[Service]
Type=oneshot
ExecStart=/path/to/dispatcher





share|improve this answer
























  • I'm not OP, but I had the exact same requirement, and your first solution is exactly what I needed. Don't know why I didn't think of that. I'd mark your answer as accepted if this was my question, but alas... Thanks!

    – Aaron Burke
    Sep 13 '17 at 0:09



















-1














You can use the systemd path units for that.Your service file should have a corresponding path unit to check for the path/file exists or not.



Example:



service.path
[Unit]
Description:Service description

[Path]
PathExists=!/other/service/binary/path/or/some/other/file/from/that/package

[Install]
WantedBy=some-user.target


This one will activate your service only if the mentioned file exists.






share|improve this answer


























  • I have tried to reformulate the question for more clarity. But this is pretty much as clear as I can make it.

    – Let_Me_Be
    Aug 17 '15 at 13:21












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f223663%2fsystemd-optional-service-dependency-that-checks-for-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you have control over the dependencies (i. e. they are your software), make them install a drop-in file for the unit file in question. That is, let's say you have foo.service which needs to depend on bar.service || baz.service.



Then the bar package can install a file /usr/lib/systemd/system/foo.service.d/50-bar-baz.conf with these contents:



[Unit]
Requires=bar.service


Similarly for baz package.





If you have no control over the dependencies, then simply make a "dispatcher" script which checks unit files for existence and then does an exec systemctl start on the chosen unit file. Then write a separate unit and require/after it from the main unit:



[Unit]
# inexistent unit names are ignored here
After=bar.service baz.service

[Service]
Type=oneshot
ExecStart=/path/to/dispatcher





share|improve this answer
























  • I'm not OP, but I had the exact same requirement, and your first solution is exactly what I needed. Don't know why I didn't think of that. I'd mark your answer as accepted if this was my question, but alas... Thanks!

    – Aaron Burke
    Sep 13 '17 at 0:09
















1














If you have control over the dependencies (i. e. they are your software), make them install a drop-in file for the unit file in question. That is, let's say you have foo.service which needs to depend on bar.service || baz.service.



Then the bar package can install a file /usr/lib/systemd/system/foo.service.d/50-bar-baz.conf with these contents:



[Unit]
Requires=bar.service


Similarly for baz package.





If you have no control over the dependencies, then simply make a "dispatcher" script which checks unit files for existence and then does an exec systemctl start on the chosen unit file. Then write a separate unit and require/after it from the main unit:



[Unit]
# inexistent unit names are ignored here
After=bar.service baz.service

[Service]
Type=oneshot
ExecStart=/path/to/dispatcher





share|improve this answer
























  • I'm not OP, but I had the exact same requirement, and your first solution is exactly what I needed. Don't know why I didn't think of that. I'd mark your answer as accepted if this was my question, but alas... Thanks!

    – Aaron Burke
    Sep 13 '17 at 0:09














1












1








1







If you have control over the dependencies (i. e. they are your software), make them install a drop-in file for the unit file in question. That is, let's say you have foo.service which needs to depend on bar.service || baz.service.



Then the bar package can install a file /usr/lib/systemd/system/foo.service.d/50-bar-baz.conf with these contents:



[Unit]
Requires=bar.service


Similarly for baz package.





If you have no control over the dependencies, then simply make a "dispatcher" script which checks unit files for existence and then does an exec systemctl start on the chosen unit file. Then write a separate unit and require/after it from the main unit:



[Unit]
# inexistent unit names are ignored here
After=bar.service baz.service

[Service]
Type=oneshot
ExecStart=/path/to/dispatcher





share|improve this answer













If you have control over the dependencies (i. e. they are your software), make them install a drop-in file for the unit file in question. That is, let's say you have foo.service which needs to depend on bar.service || baz.service.



Then the bar package can install a file /usr/lib/systemd/system/foo.service.d/50-bar-baz.conf with these contents:



[Unit]
Requires=bar.service


Similarly for baz package.





If you have no control over the dependencies, then simply make a "dispatcher" script which checks unit files for existence and then does an exec systemctl start on the chosen unit file. Then write a separate unit and require/after it from the main unit:



[Unit]
# inexistent unit names are ignored here
After=bar.service baz.service

[Service]
Type=oneshot
ExecStart=/path/to/dispatcher






share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 20 '15 at 19:38









intelfxintelfx

3,1991329




3,1991329













  • I'm not OP, but I had the exact same requirement, and your first solution is exactly what I needed. Don't know why I didn't think of that. I'd mark your answer as accepted if this was my question, but alas... Thanks!

    – Aaron Burke
    Sep 13 '17 at 0:09



















  • I'm not OP, but I had the exact same requirement, and your first solution is exactly what I needed. Don't know why I didn't think of that. I'd mark your answer as accepted if this was my question, but alas... Thanks!

    – Aaron Burke
    Sep 13 '17 at 0:09

















I'm not OP, but I had the exact same requirement, and your first solution is exactly what I needed. Don't know why I didn't think of that. I'd mark your answer as accepted if this was my question, but alas... Thanks!

– Aaron Burke
Sep 13 '17 at 0:09





I'm not OP, but I had the exact same requirement, and your first solution is exactly what I needed. Don't know why I didn't think of that. I'd mark your answer as accepted if this was my question, but alas... Thanks!

– Aaron Burke
Sep 13 '17 at 0:09













-1














You can use the systemd path units for that.Your service file should have a corresponding path unit to check for the path/file exists or not.



Example:



service.path
[Unit]
Description:Service description

[Path]
PathExists=!/other/service/binary/path/or/some/other/file/from/that/package

[Install]
WantedBy=some-user.target


This one will activate your service only if the mentioned file exists.






share|improve this answer


























  • I have tried to reformulate the question for more clarity. But this is pretty much as clear as I can make it.

    – Let_Me_Be
    Aug 17 '15 at 13:21
















-1














You can use the systemd path units for that.Your service file should have a corresponding path unit to check for the path/file exists or not.



Example:



service.path
[Unit]
Description:Service description

[Path]
PathExists=!/other/service/binary/path/or/some/other/file/from/that/package

[Install]
WantedBy=some-user.target


This one will activate your service only if the mentioned file exists.






share|improve this answer


























  • I have tried to reformulate the question for more clarity. But this is pretty much as clear as I can make it.

    – Let_Me_Be
    Aug 17 '15 at 13:21














-1












-1








-1







You can use the systemd path units for that.Your service file should have a corresponding path unit to check for the path/file exists or not.



Example:



service.path
[Unit]
Description:Service description

[Path]
PathExists=!/other/service/binary/path/or/some/other/file/from/that/package

[Install]
WantedBy=some-user.target


This one will activate your service only if the mentioned file exists.






share|improve this answer















You can use the systemd path units for that.Your service file should have a corresponding path unit to check for the path/file exists or not.



Example:



service.path
[Unit]
Description:Service description

[Path]
PathExists=!/other/service/binary/path/or/some/other/file/from/that/package

[Install]
WantedBy=some-user.target


This one will activate your service only if the mentioned file exists.







share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago









Rui F Ribeiro

41.9k1483142




41.9k1483142










answered Aug 17 '15 at 13:10









ThushiThushi

6,52941439




6,52941439













  • I have tried to reformulate the question for more clarity. But this is pretty much as clear as I can make it.

    – Let_Me_Be
    Aug 17 '15 at 13:21



















  • I have tried to reformulate the question for more clarity. But this is pretty much as clear as I can make it.

    – Let_Me_Be
    Aug 17 '15 at 13:21

















I have tried to reformulate the question for more clarity. But this is pretty much as clear as I can make it.

– Let_Me_Be
Aug 17 '15 at 13:21





I have tried to reformulate the question for more clarity. But this is pretty much as clear as I can make it.

– Let_Me_Be
Aug 17 '15 at 13:21


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f223663%2fsystemd-optional-service-dependency-that-checks-for-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

CARDNET

Boot-repair Failure: Unable to locate package grub-common:i386

濃尾地震