How to stop a service before executing an ExecStart entry?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I would like to set up a Timer which will stop a service, execute a script and restart the service.



One of the possibilities is to use



Type=oneshot
ExecStartPre=/bin/systemctl stop myservice
ExecStart=/usr/local/bin/myscript.sh
ExecStartPost=/bin/systemctl start myservice


Another one is to have myscript.sh handling the whole thing, including systemctl.



I find it awkward, though, to use systemctl within a service declaration, when there may be systemd built-in mechanisms to interact with services. Is there a cleaner way to perform these operations?










share|improve this question





















  • Can you give more context here? Why do you need myservice to be stopped during the run of myscript.sh? There's nothing essentially wrong with using systemctl inside a service (and you probably won't find a solution that doesn't involve using it), but perhaps, depending on what myscript.sh is doing, you could just hook it into myservice.service directly (in an ExecStartPre=, perhaps checking if it needs to run) and then just having the timer launch a systemctl restart myservice instead... Just an idea. Having more context on what you're trying to do would help here.
    – Filipe Brandenburger
    Nov 23 at 19:17










  • @FilipeBrandenburger: this is a Timer which will perform a backup. The service backed up needs first to be stopped, then restarted afterwards.
    – WoJ
    Nov 23 at 19:22














up vote
1
down vote

favorite












I would like to set up a Timer which will stop a service, execute a script and restart the service.



One of the possibilities is to use



Type=oneshot
ExecStartPre=/bin/systemctl stop myservice
ExecStart=/usr/local/bin/myscript.sh
ExecStartPost=/bin/systemctl start myservice


Another one is to have myscript.sh handling the whole thing, including systemctl.



I find it awkward, though, to use systemctl within a service declaration, when there may be systemd built-in mechanisms to interact with services. Is there a cleaner way to perform these operations?










share|improve this question





















  • Can you give more context here? Why do you need myservice to be stopped during the run of myscript.sh? There's nothing essentially wrong with using systemctl inside a service (and you probably won't find a solution that doesn't involve using it), but perhaps, depending on what myscript.sh is doing, you could just hook it into myservice.service directly (in an ExecStartPre=, perhaps checking if it needs to run) and then just having the timer launch a systemctl restart myservice instead... Just an idea. Having more context on what you're trying to do would help here.
    – Filipe Brandenburger
    Nov 23 at 19:17










  • @FilipeBrandenburger: this is a Timer which will perform a backup. The service backed up needs first to be stopped, then restarted afterwards.
    – WoJ
    Nov 23 at 19:22












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I would like to set up a Timer which will stop a service, execute a script and restart the service.



One of the possibilities is to use



Type=oneshot
ExecStartPre=/bin/systemctl stop myservice
ExecStart=/usr/local/bin/myscript.sh
ExecStartPost=/bin/systemctl start myservice


Another one is to have myscript.sh handling the whole thing, including systemctl.



I find it awkward, though, to use systemctl within a service declaration, when there may be systemd built-in mechanisms to interact with services. Is there a cleaner way to perform these operations?










share|improve this question













I would like to set up a Timer which will stop a service, execute a script and restart the service.



One of the possibilities is to use



Type=oneshot
ExecStartPre=/bin/systemctl stop myservice
ExecStart=/usr/local/bin/myscript.sh
ExecStartPost=/bin/systemctl start myservice


Another one is to have myscript.sh handling the whole thing, including systemctl.



I find it awkward, though, to use systemctl within a service declaration, when there may be systemd built-in mechanisms to interact with services. Is there a cleaner way to perform these operations?







systemd systemd-timer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 10:14









WoJ

396113




396113











  • Can you give more context here? Why do you need myservice to be stopped during the run of myscript.sh? There's nothing essentially wrong with using systemctl inside a service (and you probably won't find a solution that doesn't involve using it), but perhaps, depending on what myscript.sh is doing, you could just hook it into myservice.service directly (in an ExecStartPre=, perhaps checking if it needs to run) and then just having the timer launch a systemctl restart myservice instead... Just an idea. Having more context on what you're trying to do would help here.
    – Filipe Brandenburger
    Nov 23 at 19:17










  • @FilipeBrandenburger: this is a Timer which will perform a backup. The service backed up needs first to be stopped, then restarted afterwards.
    – WoJ
    Nov 23 at 19:22
















  • Can you give more context here? Why do you need myservice to be stopped during the run of myscript.sh? There's nothing essentially wrong with using systemctl inside a service (and you probably won't find a solution that doesn't involve using it), but perhaps, depending on what myscript.sh is doing, you could just hook it into myservice.service directly (in an ExecStartPre=, perhaps checking if it needs to run) and then just having the timer launch a systemctl restart myservice instead... Just an idea. Having more context on what you're trying to do would help here.
    – Filipe Brandenburger
    Nov 23 at 19:17










  • @FilipeBrandenburger: this is a Timer which will perform a backup. The service backed up needs first to be stopped, then restarted afterwards.
    – WoJ
    Nov 23 at 19:22















Can you give more context here? Why do you need myservice to be stopped during the run of myscript.sh? There's nothing essentially wrong with using systemctl inside a service (and you probably won't find a solution that doesn't involve using it), but perhaps, depending on what myscript.sh is doing, you could just hook it into myservice.service directly (in an ExecStartPre=, perhaps checking if it needs to run) and then just having the timer launch a systemctl restart myservice instead... Just an idea. Having more context on what you're trying to do would help here.
– Filipe Brandenburger
Nov 23 at 19:17




Can you give more context here? Why do you need myservice to be stopped during the run of myscript.sh? There's nothing essentially wrong with using systemctl inside a service (and you probably won't find a solution that doesn't involve using it), but perhaps, depending on what myscript.sh is doing, you could just hook it into myservice.service directly (in an ExecStartPre=, perhaps checking if it needs to run) and then just having the timer launch a systemctl restart myservice instead... Just an idea. Having more context on what you're trying to do would help here.
– Filipe Brandenburger
Nov 23 at 19:17












@FilipeBrandenburger: this is a Timer which will perform a backup. The service backed up needs first to be stopped, then restarted afterwards.
– WoJ
Nov 23 at 19:22




@FilipeBrandenburger: this is a Timer which will perform a backup. The service backed up needs first to be stopped, then restarted afterwards.
– WoJ
Nov 23 at 19:22










1 Answer
1






active

oldest

votes

















up vote
0
down vote













By (ab?)using the WatchdogSec the service will terminate when it fails to acknowledge within the time. It will then restart, but execute the script first.



WatchdogSec=interval
Restart=on-watchdog
ExecStopPost=/script.sh


ref: WatchdogSec



Having the service being able to be backed up while running would be a much nicer service.






share|improve this answer




















    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',
    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%2f483169%2fhow-to-stop-a-service-before-executing-an-execstart-entry%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    By (ab?)using the WatchdogSec the service will terminate when it fails to acknowledge within the time. It will then restart, but execute the script first.



    WatchdogSec=interval
    Restart=on-watchdog
    ExecStopPost=/script.sh


    ref: WatchdogSec



    Having the service being able to be backed up while running would be a much nicer service.






    share|improve this answer
























      up vote
      0
      down vote













      By (ab?)using the WatchdogSec the service will terminate when it fails to acknowledge within the time. It will then restart, but execute the script first.



      WatchdogSec=interval
      Restart=on-watchdog
      ExecStopPost=/script.sh


      ref: WatchdogSec



      Having the service being able to be backed up while running would be a much nicer service.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        By (ab?)using the WatchdogSec the service will terminate when it fails to acknowledge within the time. It will then restart, but execute the script first.



        WatchdogSec=interval
        Restart=on-watchdog
        ExecStopPost=/script.sh


        ref: WatchdogSec



        Having the service being able to be backed up while running would be a much nicer service.






        share|improve this answer












        By (ab?)using the WatchdogSec the service will terminate when it fails to acknowledge within the time. It will then restart, but execute the script first.



        WatchdogSec=interval
        Restart=on-watchdog
        ExecStopPost=/script.sh


        ref: WatchdogSec



        Having the service being able to be backed up while running would be a much nicer service.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 14 hours ago









        danblack

        3166




        3166



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483169%2fhow-to-stop-a-service-before-executing-an-execstart-entry%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

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)