Restarting a service from a script












3















I have a init script - /etc/init.d/myservice, which starts some processes, including a CLI process. And from this CLI process, I am adding a command which can be used to restart the service (restart ourselves - kind of).



What I tried - from a script (that is spawned from the CLI command), ran sudo service myservice restart, where restart is stop;start.
But, this doesn't work! i.e, even though the stop happens, by the time start part is executing, the script terminates (not with any signals, but plain EXIT).



I tried combinations of nohup, disown etc, but didn't seem to work! I am not sure how to complete the restart without getting terminated - any pointers, or workarounds to debug further ?



To summarize:





  • myservice -> spawns many processes, including CLI

  • CLI -> has a restart command, which invokes a script


  • script does sudo service myservice restart


--More info--



I'm on Ubuntu 12.04, i.e, upstart










share|improve this question




















  • 1





    What's the OS, init system (upstart, systemd, sysvinit...)?

    – Stéphane Chazelas
    Aug 26 '14 at 13:01











  • How is the "stop" action done? Does it help if you add a trap '' TERM in your restart script?

    – Stéphane Chazelas
    Aug 26 '14 at 13:02











  • @StéphaneChazelas added more info, stop does a usual kill by process name. If I add trap..TERM wouldn't it affect other ways of start/stop - like, from shell if one does sudo myservice stop ?

    – vyom
    Aug 26 '14 at 13:09











  • would your kill by process name (it's better to combine that with pid files to avoid problems or use cgroups as systemd) not possibly kill your script?

    – Stéphane Chazelas
    Aug 26 '14 at 13:16











  • I don't understand cgroups as systemd :(. The script name is not in the list of processes to be stopped.

    – vyom
    Aug 26 '14 at 13:24
















3















I have a init script - /etc/init.d/myservice, which starts some processes, including a CLI process. And from this CLI process, I am adding a command which can be used to restart the service (restart ourselves - kind of).



What I tried - from a script (that is spawned from the CLI command), ran sudo service myservice restart, where restart is stop;start.
But, this doesn't work! i.e, even though the stop happens, by the time start part is executing, the script terminates (not with any signals, but plain EXIT).



I tried combinations of nohup, disown etc, but didn't seem to work! I am not sure how to complete the restart without getting terminated - any pointers, or workarounds to debug further ?



To summarize:





  • myservice -> spawns many processes, including CLI

  • CLI -> has a restart command, which invokes a script


  • script does sudo service myservice restart


--More info--



I'm on Ubuntu 12.04, i.e, upstart










share|improve this question




















  • 1





    What's the OS, init system (upstart, systemd, sysvinit...)?

    – Stéphane Chazelas
    Aug 26 '14 at 13:01











  • How is the "stop" action done? Does it help if you add a trap '' TERM in your restart script?

    – Stéphane Chazelas
    Aug 26 '14 at 13:02











  • @StéphaneChazelas added more info, stop does a usual kill by process name. If I add trap..TERM wouldn't it affect other ways of start/stop - like, from shell if one does sudo myservice stop ?

    – vyom
    Aug 26 '14 at 13:09











  • would your kill by process name (it's better to combine that with pid files to avoid problems or use cgroups as systemd) not possibly kill your script?

    – Stéphane Chazelas
    Aug 26 '14 at 13:16











  • I don't understand cgroups as systemd :(. The script name is not in the list of processes to be stopped.

    – vyom
    Aug 26 '14 at 13:24














3












3








3








I have a init script - /etc/init.d/myservice, which starts some processes, including a CLI process. And from this CLI process, I am adding a command which can be used to restart the service (restart ourselves - kind of).



What I tried - from a script (that is spawned from the CLI command), ran sudo service myservice restart, where restart is stop;start.
But, this doesn't work! i.e, even though the stop happens, by the time start part is executing, the script terminates (not with any signals, but plain EXIT).



I tried combinations of nohup, disown etc, but didn't seem to work! I am not sure how to complete the restart without getting terminated - any pointers, or workarounds to debug further ?



To summarize:





  • myservice -> spawns many processes, including CLI

  • CLI -> has a restart command, which invokes a script


  • script does sudo service myservice restart


--More info--



I'm on Ubuntu 12.04, i.e, upstart










share|improve this question
















I have a init script - /etc/init.d/myservice, which starts some processes, including a CLI process. And from this CLI process, I am adding a command which can be used to restart the service (restart ourselves - kind of).



What I tried - from a script (that is spawned from the CLI command), ran sudo service myservice restart, where restart is stop;start.
But, this doesn't work! i.e, even though the stop happens, by the time start part is executing, the script terminates (not with any signals, but plain EXIT).



I tried combinations of nohup, disown etc, but didn't seem to work! I am not sure how to complete the restart without getting terminated - any pointers, or workarounds to debug further ?



To summarize:





  • myservice -> spawns many processes, including CLI

  • CLI -> has a restart command, which invokes a script


  • script does sudo service myservice restart


--More info--



I'm on Ubuntu 12.04, i.e, upstart







bash init-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 26 '14 at 13:29









slm

249k66522680




249k66522680










asked Aug 26 '14 at 12:18









vyomvyom

133114




133114








  • 1





    What's the OS, init system (upstart, systemd, sysvinit...)?

    – Stéphane Chazelas
    Aug 26 '14 at 13:01











  • How is the "stop" action done? Does it help if you add a trap '' TERM in your restart script?

    – Stéphane Chazelas
    Aug 26 '14 at 13:02











  • @StéphaneChazelas added more info, stop does a usual kill by process name. If I add trap..TERM wouldn't it affect other ways of start/stop - like, from shell if one does sudo myservice stop ?

    – vyom
    Aug 26 '14 at 13:09











  • would your kill by process name (it's better to combine that with pid files to avoid problems or use cgroups as systemd) not possibly kill your script?

    – Stéphane Chazelas
    Aug 26 '14 at 13:16











  • I don't understand cgroups as systemd :(. The script name is not in the list of processes to be stopped.

    – vyom
    Aug 26 '14 at 13:24














  • 1





    What's the OS, init system (upstart, systemd, sysvinit...)?

    – Stéphane Chazelas
    Aug 26 '14 at 13:01











  • How is the "stop" action done? Does it help if you add a trap '' TERM in your restart script?

    – Stéphane Chazelas
    Aug 26 '14 at 13:02











  • @StéphaneChazelas added more info, stop does a usual kill by process name. If I add trap..TERM wouldn't it affect other ways of start/stop - like, from shell if one does sudo myservice stop ?

    – vyom
    Aug 26 '14 at 13:09











  • would your kill by process name (it's better to combine that with pid files to avoid problems or use cgroups as systemd) not possibly kill your script?

    – Stéphane Chazelas
    Aug 26 '14 at 13:16











  • I don't understand cgroups as systemd :(. The script name is not in the list of processes to be stopped.

    – vyom
    Aug 26 '14 at 13:24








1




1





What's the OS, init system (upstart, systemd, sysvinit...)?

– Stéphane Chazelas
Aug 26 '14 at 13:01





What's the OS, init system (upstart, systemd, sysvinit...)?

– Stéphane Chazelas
Aug 26 '14 at 13:01













How is the "stop" action done? Does it help if you add a trap '' TERM in your restart script?

– Stéphane Chazelas
Aug 26 '14 at 13:02





How is the "stop" action done? Does it help if you add a trap '' TERM in your restart script?

– Stéphane Chazelas
Aug 26 '14 at 13:02













@StéphaneChazelas added more info, stop does a usual kill by process name. If I add trap..TERM wouldn't it affect other ways of start/stop - like, from shell if one does sudo myservice stop ?

– vyom
Aug 26 '14 at 13:09





@StéphaneChazelas added more info, stop does a usual kill by process name. If I add trap..TERM wouldn't it affect other ways of start/stop - like, from shell if one does sudo myservice stop ?

– vyom
Aug 26 '14 at 13:09













would your kill by process name (it's better to combine that with pid files to avoid problems or use cgroups as systemd) not possibly kill your script?

– Stéphane Chazelas
Aug 26 '14 at 13:16





would your kill by process name (it's better to combine that with pid files to avoid problems or use cgroups as systemd) not possibly kill your script?

– Stéphane Chazelas
Aug 26 '14 at 13:16













I don't understand cgroups as systemd :(. The script name is not in the list of processes to be stopped.

– vyom
Aug 26 '14 at 13:24





I don't understand cgroups as systemd :(. The script name is not in the list of processes to be stopped.

– vyom
Aug 26 '14 at 13:24










2 Answers
2






active

oldest

votes


















0














Before starting the service again you might want to wait for a few seconds so that it stops properly. You can delay your script with the following line



# waiting for 5 seconds
sleep 5


You can see more details here - http://www.cyberciti.biz/faq/linux-unix-sleep-bash-scripting/






share|improve this answer
























  • the stop is happening, but at start the script terminates.

    – vyom
    Aug 26 '14 at 13:07



















0














I had searched for quite some time, but I found one solution here to this SuperUser Q&A titled: How do I detach a process from Terminal, entirely?, after posting my question here.



| at now





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',
    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%2f152246%2frestarting-a-service-from-a-script%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









    0














    Before starting the service again you might want to wait for a few seconds so that it stops properly. You can delay your script with the following line



    # waiting for 5 seconds
    sleep 5


    You can see more details here - http://www.cyberciti.biz/faq/linux-unix-sleep-bash-scripting/






    share|improve this answer
























    • the stop is happening, but at start the script terminates.

      – vyom
      Aug 26 '14 at 13:07
















    0














    Before starting the service again you might want to wait for a few seconds so that it stops properly. You can delay your script with the following line



    # waiting for 5 seconds
    sleep 5


    You can see more details here - http://www.cyberciti.biz/faq/linux-unix-sleep-bash-scripting/






    share|improve this answer
























    • the stop is happening, but at start the script terminates.

      – vyom
      Aug 26 '14 at 13:07














    0












    0








    0







    Before starting the service again you might want to wait for a few seconds so that it stops properly. You can delay your script with the following line



    # waiting for 5 seconds
    sleep 5


    You can see more details here - http://www.cyberciti.biz/faq/linux-unix-sleep-bash-scripting/






    share|improve this answer













    Before starting the service again you might want to wait for a few seconds so that it stops properly. You can delay your script with the following line



    # waiting for 5 seconds
    sleep 5


    You can see more details here - http://www.cyberciti.biz/faq/linux-unix-sleep-bash-scripting/







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 26 '14 at 13:00









    redmosesredmoses

    1




    1













    • the stop is happening, but at start the script terminates.

      – vyom
      Aug 26 '14 at 13:07



















    • the stop is happening, but at start the script terminates.

      – vyom
      Aug 26 '14 at 13:07

















    the stop is happening, but at start the script terminates.

    – vyom
    Aug 26 '14 at 13:07





    the stop is happening, but at start the script terminates.

    – vyom
    Aug 26 '14 at 13:07













    0














    I had searched for quite some time, but I found one solution here to this SuperUser Q&A titled: How do I detach a process from Terminal, entirely?, after posting my question here.



    | at now





    share|improve this answer






























      0














      I had searched for quite some time, but I found one solution here to this SuperUser Q&A titled: How do I detach a process from Terminal, entirely?, after posting my question here.



      | at now





      share|improve this answer




























        0












        0








        0







        I had searched for quite some time, but I found one solution here to this SuperUser Q&A titled: How do I detach a process from Terminal, entirely?, after posting my question here.



        | at now





        share|improve this answer















        I had searched for quite some time, but I found one solution here to this SuperUser Q&A titled: How do I detach a process from Terminal, entirely?, after posting my question here.



        | at now






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 20 '17 at 10:18









        Community

        1




        1










        answered Aug 26 '14 at 13:06









        vyomvyom

        133114




        133114






























            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%2f152246%2frestarting-a-service-from-a-script%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

            How to make a Squid Proxy server?

            Is this a new Fibonacci Identity?

            19世紀