How to correctly stop busybox shell process and child process?











up vote
2
down vote

favorite
1












The actual server process is spawned by a shell script



I am trying to write an init script for djb daemontools within Entware on a router running busybox 1.24 (ash shell). The daemontools way to start itself, is using the svscanboot shell script. Note that I have removed readproctitle from svscanboot.



PATH=/opt/sbin:/opt/bin:/bin:/sbin:/usr/bin:/usr/sbin
exec </dev/null
exec >/dev/null
exec 2>/dev/null
/opt/bin/svc -dx /opt/service/* /opt/service/*/log
env - PATH=$PATH svscan /opt/service 2>&1


The parent shell script process spawns a (descendant) svscan child process, which is the actual running server process.



The TERM signal is received by the shell process



Running svscanboot & (in background) and killing the parent process results in the child process running:



# ps l | grep svscan
S 0 1526 1 1560 404 0:0 22:57 00:00:00 {svscanboot} /bin/sh /opt/bin/svscanboot
S 0 1528 1526 976 252 0:0 22:57 00:00:00 svscan /opt/service
# killall svscanboot


But svscan will continue to run



# ps l | grep svscan
S 0 1528 1 976 252 0:0 22:57 00:00:00 svscan /opt/service


Executing svscanboot (in foreground) and killing the parent process also results in the child process still running:



# ps l | grep svscan
S 0 676 671 1560 400 pts1 23:41 00:00:00 {svscanboot} /bin/sh /opt/bin/svscanboot
S 0 678 676 976 252 pts1 23:41 00:00:00 svscan /opt/service
# killall svscanboot
# ps l | grep svscan
S 0 678 1 976 252 pts1 23:41 00:00:00 svscan /opt/service


Busybox is quite limited, killall only has flags -l and -q and ps only has wide, long and show Threads.



And when quitting the foreground version with Ctrl + C both parent and child processes are terminated.



How to stop both the parent and the child process in this case, preferably using killall and eventually by modifying svscanboot?










share|improve this question




















  • 1




    Running svscan* processes forever is normal behaviour. Do you have any special reason to stop them?
    – Ipor Sircer
    Nov 29 '16 at 9:47










  • @IporSircer Most init scripts have actions like: start, stop, restart, kill, etcetera. For the sake of having an init script that works the way most users do expect it to, I do want to stop deamontools (svscan*).
    – Pro Backup
    Nov 29 '16 at 12:46








  • 1




    svscan has a similar job like systemd, so it is the recommended way to leave it run forever. If you don't wanna use its features, then remove it completely and start your services directly from initscripts.
    – Ipor Sircer
    Nov 29 '16 at 13:09

















up vote
2
down vote

favorite
1












The actual server process is spawned by a shell script



I am trying to write an init script for djb daemontools within Entware on a router running busybox 1.24 (ash shell). The daemontools way to start itself, is using the svscanboot shell script. Note that I have removed readproctitle from svscanboot.



PATH=/opt/sbin:/opt/bin:/bin:/sbin:/usr/bin:/usr/sbin
exec </dev/null
exec >/dev/null
exec 2>/dev/null
/opt/bin/svc -dx /opt/service/* /opt/service/*/log
env - PATH=$PATH svscan /opt/service 2>&1


The parent shell script process spawns a (descendant) svscan child process, which is the actual running server process.



The TERM signal is received by the shell process



Running svscanboot & (in background) and killing the parent process results in the child process running:



# ps l | grep svscan
S 0 1526 1 1560 404 0:0 22:57 00:00:00 {svscanboot} /bin/sh /opt/bin/svscanboot
S 0 1528 1526 976 252 0:0 22:57 00:00:00 svscan /opt/service
# killall svscanboot


But svscan will continue to run



# ps l | grep svscan
S 0 1528 1 976 252 0:0 22:57 00:00:00 svscan /opt/service


Executing svscanboot (in foreground) and killing the parent process also results in the child process still running:



# ps l | grep svscan
S 0 676 671 1560 400 pts1 23:41 00:00:00 {svscanboot} /bin/sh /opt/bin/svscanboot
S 0 678 676 976 252 pts1 23:41 00:00:00 svscan /opt/service
# killall svscanboot
# ps l | grep svscan
S 0 678 1 976 252 pts1 23:41 00:00:00 svscan /opt/service


Busybox is quite limited, killall only has flags -l and -q and ps only has wide, long and show Threads.



And when quitting the foreground version with Ctrl + C both parent and child processes are terminated.



How to stop both the parent and the child process in this case, preferably using killall and eventually by modifying svscanboot?










share|improve this question




















  • 1




    Running svscan* processes forever is normal behaviour. Do you have any special reason to stop them?
    – Ipor Sircer
    Nov 29 '16 at 9:47










  • @IporSircer Most init scripts have actions like: start, stop, restart, kill, etcetera. For the sake of having an init script that works the way most users do expect it to, I do want to stop deamontools (svscan*).
    – Pro Backup
    Nov 29 '16 at 12:46








  • 1




    svscan has a similar job like systemd, so it is the recommended way to leave it run forever. If you don't wanna use its features, then remove it completely and start your services directly from initscripts.
    – Ipor Sircer
    Nov 29 '16 at 13:09















up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





The actual server process is spawned by a shell script



I am trying to write an init script for djb daemontools within Entware on a router running busybox 1.24 (ash shell). The daemontools way to start itself, is using the svscanboot shell script. Note that I have removed readproctitle from svscanboot.



PATH=/opt/sbin:/opt/bin:/bin:/sbin:/usr/bin:/usr/sbin
exec </dev/null
exec >/dev/null
exec 2>/dev/null
/opt/bin/svc -dx /opt/service/* /opt/service/*/log
env - PATH=$PATH svscan /opt/service 2>&1


The parent shell script process spawns a (descendant) svscan child process, which is the actual running server process.



The TERM signal is received by the shell process



Running svscanboot & (in background) and killing the parent process results in the child process running:



# ps l | grep svscan
S 0 1526 1 1560 404 0:0 22:57 00:00:00 {svscanboot} /bin/sh /opt/bin/svscanboot
S 0 1528 1526 976 252 0:0 22:57 00:00:00 svscan /opt/service
# killall svscanboot


But svscan will continue to run



# ps l | grep svscan
S 0 1528 1 976 252 0:0 22:57 00:00:00 svscan /opt/service


Executing svscanboot (in foreground) and killing the parent process also results in the child process still running:



# ps l | grep svscan
S 0 676 671 1560 400 pts1 23:41 00:00:00 {svscanboot} /bin/sh /opt/bin/svscanboot
S 0 678 676 976 252 pts1 23:41 00:00:00 svscan /opt/service
# killall svscanboot
# ps l | grep svscan
S 0 678 1 976 252 pts1 23:41 00:00:00 svscan /opt/service


Busybox is quite limited, killall only has flags -l and -q and ps only has wide, long and show Threads.



And when quitting the foreground version with Ctrl + C both parent and child processes are terminated.



How to stop both the parent and the child process in this case, preferably using killall and eventually by modifying svscanboot?










share|improve this question















The actual server process is spawned by a shell script



I am trying to write an init script for djb daemontools within Entware on a router running busybox 1.24 (ash shell). The daemontools way to start itself, is using the svscanboot shell script. Note that I have removed readproctitle from svscanboot.



PATH=/opt/sbin:/opt/bin:/bin:/sbin:/usr/bin:/usr/sbin
exec </dev/null
exec >/dev/null
exec 2>/dev/null
/opt/bin/svc -dx /opt/service/* /opt/service/*/log
env - PATH=$PATH svscan /opt/service 2>&1


The parent shell script process spawns a (descendant) svscan child process, which is the actual running server process.



The TERM signal is received by the shell process



Running svscanboot & (in background) and killing the parent process results in the child process running:



# ps l | grep svscan
S 0 1526 1 1560 404 0:0 22:57 00:00:00 {svscanboot} /bin/sh /opt/bin/svscanboot
S 0 1528 1526 976 252 0:0 22:57 00:00:00 svscan /opt/service
# killall svscanboot


But svscan will continue to run



# ps l | grep svscan
S 0 1528 1 976 252 0:0 22:57 00:00:00 svscan /opt/service


Executing svscanboot (in foreground) and killing the parent process also results in the child process still running:



# ps l | grep svscan
S 0 676 671 1560 400 pts1 23:41 00:00:00 {svscanboot} /bin/sh /opt/bin/svscanboot
S 0 678 676 976 252 pts1 23:41 00:00:00 svscan /opt/service
# killall svscanboot
# ps l | grep svscan
S 0 678 1 976 252 pts1 23:41 00:00:00 svscan /opt/service


Busybox is quite limited, killall only has flags -l and -q and ps only has wide, long and show Threads.



And when quitting the foreground version with Ctrl + C both parent and child processes are terminated.



How to stop both the parent and the child process in this case, preferably using killall and eventually by modifying svscanboot?







kill busybox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '16 at 12:48

























asked Nov 28 '16 at 22:58









Pro Backup

1,94862956




1,94862956








  • 1




    Running svscan* processes forever is normal behaviour. Do you have any special reason to stop them?
    – Ipor Sircer
    Nov 29 '16 at 9:47










  • @IporSircer Most init scripts have actions like: start, stop, restart, kill, etcetera. For the sake of having an init script that works the way most users do expect it to, I do want to stop deamontools (svscan*).
    – Pro Backup
    Nov 29 '16 at 12:46








  • 1




    svscan has a similar job like systemd, so it is the recommended way to leave it run forever. If you don't wanna use its features, then remove it completely and start your services directly from initscripts.
    – Ipor Sircer
    Nov 29 '16 at 13:09
















  • 1




    Running svscan* processes forever is normal behaviour. Do you have any special reason to stop them?
    – Ipor Sircer
    Nov 29 '16 at 9:47










  • @IporSircer Most init scripts have actions like: start, stop, restart, kill, etcetera. For the sake of having an init script that works the way most users do expect it to, I do want to stop deamontools (svscan*).
    – Pro Backup
    Nov 29 '16 at 12:46








  • 1




    svscan has a similar job like systemd, so it is the recommended way to leave it run forever. If you don't wanna use its features, then remove it completely and start your services directly from initscripts.
    – Ipor Sircer
    Nov 29 '16 at 13:09










1




1




Running svscan* processes forever is normal behaviour. Do you have any special reason to stop them?
– Ipor Sircer
Nov 29 '16 at 9:47




Running svscan* processes forever is normal behaviour. Do you have any special reason to stop them?
– Ipor Sircer
Nov 29 '16 at 9:47












@IporSircer Most init scripts have actions like: start, stop, restart, kill, etcetera. For the sake of having an init script that works the way most users do expect it to, I do want to stop deamontools (svscan*).
– Pro Backup
Nov 29 '16 at 12:46






@IporSircer Most init scripts have actions like: start, stop, restart, kill, etcetera. For the sake of having an init script that works the way most users do expect it to, I do want to stop deamontools (svscan*).
– Pro Backup
Nov 29 '16 at 12:46






1




1




svscan has a similar job like systemd, so it is the recommended way to leave it run forever. If you don't wanna use its features, then remove it completely and start your services directly from initscripts.
– Ipor Sircer
Nov 29 '16 at 13:09






svscan has a similar job like systemd, so it is the recommended way to leave it run forever. If you don't wanna use its features, then remove it completely and start your services directly from initscripts.
– Ipor Sircer
Nov 29 '16 at 13:09












1 Answer
1






active

oldest

votes

















up vote
0
down vote













To exec



After I read the question and answer of Meaning of "exec env COMMAND " a possible solution could be to prefix the env … with an exec. That will prevent the spawning of a child process. However ps outputs {svscanboot} /bin/sh /opt/bin/svscanboot. That name will change to svscan /opt/service after the exec.



Or to trap signals



The other way to handle this is via a trap that propagates the signals to the child process. Like Forward SIGTERM to child in Bash or the in more detail explained http://veithen.github.io/2014/11/16/sigterm-propagation.html
The issue with this solution route is that SIGKILL a.k.a. signal number -9 can not be trapped.



Exec with a name change workaround



For not being able to trap the KILL signal, I will choose for the exec route:




exec env - PATH=$PATH svscan /opt/service 2>&1


This does still make things complicated to stop/kill/reconfigure "svscanboot" within the Entware init system because of the changed name. That exec process name change will need a workaround.



Mimic svscanboot



The entware-daemontools-init-script can be changed to mimic svscanboot, like:



ENABLED=yes
#PRECMD="exec </dev/null;exec >/dev/null;exec 2>/dev/null;/opt/bin/svc -dx /opt/service/* /opt/service/*/log"
PROCS="svscan"
ARGS="/opt/service 2>&1"
PATH=/opt/sbin:/opt/bin:/sbin:/bin:/usr/sbin:/usr/bin
PREARGS="env - PATH=$PATH"
DESC="daemontools"


The commented PRECMD makes that svscan will not be started.






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%2f326685%2fhow-to-correctly-stop-busybox-shell-process-and-child-process%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













    To exec



    After I read the question and answer of Meaning of "exec env COMMAND " a possible solution could be to prefix the env … with an exec. That will prevent the spawning of a child process. However ps outputs {svscanboot} /bin/sh /opt/bin/svscanboot. That name will change to svscan /opt/service after the exec.



    Or to trap signals



    The other way to handle this is via a trap that propagates the signals to the child process. Like Forward SIGTERM to child in Bash or the in more detail explained http://veithen.github.io/2014/11/16/sigterm-propagation.html
    The issue with this solution route is that SIGKILL a.k.a. signal number -9 can not be trapped.



    Exec with a name change workaround



    For not being able to trap the KILL signal, I will choose for the exec route:




    exec env - PATH=$PATH svscan /opt/service 2>&1


    This does still make things complicated to stop/kill/reconfigure "svscanboot" within the Entware init system because of the changed name. That exec process name change will need a workaround.



    Mimic svscanboot



    The entware-daemontools-init-script can be changed to mimic svscanboot, like:



    ENABLED=yes
    #PRECMD="exec </dev/null;exec >/dev/null;exec 2>/dev/null;/opt/bin/svc -dx /opt/service/* /opt/service/*/log"
    PROCS="svscan"
    ARGS="/opt/service 2>&1"
    PATH=/opt/sbin:/opt/bin:/sbin:/bin:/usr/sbin:/usr/bin
    PREARGS="env - PATH=$PATH"
    DESC="daemontools"


    The commented PRECMD makes that svscan will not be started.






    share|improve this answer



























      up vote
      0
      down vote













      To exec



      After I read the question and answer of Meaning of "exec env COMMAND " a possible solution could be to prefix the env … with an exec. That will prevent the spawning of a child process. However ps outputs {svscanboot} /bin/sh /opt/bin/svscanboot. That name will change to svscan /opt/service after the exec.



      Or to trap signals



      The other way to handle this is via a trap that propagates the signals to the child process. Like Forward SIGTERM to child in Bash or the in more detail explained http://veithen.github.io/2014/11/16/sigterm-propagation.html
      The issue with this solution route is that SIGKILL a.k.a. signal number -9 can not be trapped.



      Exec with a name change workaround



      For not being able to trap the KILL signal, I will choose for the exec route:




      exec env - PATH=$PATH svscan /opt/service 2>&1


      This does still make things complicated to stop/kill/reconfigure "svscanboot" within the Entware init system because of the changed name. That exec process name change will need a workaround.



      Mimic svscanboot



      The entware-daemontools-init-script can be changed to mimic svscanboot, like:



      ENABLED=yes
      #PRECMD="exec </dev/null;exec >/dev/null;exec 2>/dev/null;/opt/bin/svc -dx /opt/service/* /opt/service/*/log"
      PROCS="svscan"
      ARGS="/opt/service 2>&1"
      PATH=/opt/sbin:/opt/bin:/sbin:/bin:/usr/sbin:/usr/bin
      PREARGS="env - PATH=$PATH"
      DESC="daemontools"


      The commented PRECMD makes that svscan will not be started.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        To exec



        After I read the question and answer of Meaning of "exec env COMMAND " a possible solution could be to prefix the env … with an exec. That will prevent the spawning of a child process. However ps outputs {svscanboot} /bin/sh /opt/bin/svscanboot. That name will change to svscan /opt/service after the exec.



        Or to trap signals



        The other way to handle this is via a trap that propagates the signals to the child process. Like Forward SIGTERM to child in Bash or the in more detail explained http://veithen.github.io/2014/11/16/sigterm-propagation.html
        The issue with this solution route is that SIGKILL a.k.a. signal number -9 can not be trapped.



        Exec with a name change workaround



        For not being able to trap the KILL signal, I will choose for the exec route:




        exec env - PATH=$PATH svscan /opt/service 2>&1


        This does still make things complicated to stop/kill/reconfigure "svscanboot" within the Entware init system because of the changed name. That exec process name change will need a workaround.



        Mimic svscanboot



        The entware-daemontools-init-script can be changed to mimic svscanboot, like:



        ENABLED=yes
        #PRECMD="exec </dev/null;exec >/dev/null;exec 2>/dev/null;/opt/bin/svc -dx /opt/service/* /opt/service/*/log"
        PROCS="svscan"
        ARGS="/opt/service 2>&1"
        PATH=/opt/sbin:/opt/bin:/sbin:/bin:/usr/sbin:/usr/bin
        PREARGS="env - PATH=$PATH"
        DESC="daemontools"


        The commented PRECMD makes that svscan will not be started.






        share|improve this answer














        To exec



        After I read the question and answer of Meaning of "exec env COMMAND " a possible solution could be to prefix the env … with an exec. That will prevent the spawning of a child process. However ps outputs {svscanboot} /bin/sh /opt/bin/svscanboot. That name will change to svscan /opt/service after the exec.



        Or to trap signals



        The other way to handle this is via a trap that propagates the signals to the child process. Like Forward SIGTERM to child in Bash or the in more detail explained http://veithen.github.io/2014/11/16/sigterm-propagation.html
        The issue with this solution route is that SIGKILL a.k.a. signal number -9 can not be trapped.



        Exec with a name change workaround



        For not being able to trap the KILL signal, I will choose for the exec route:




        exec env - PATH=$PATH svscan /opt/service 2>&1


        This does still make things complicated to stop/kill/reconfigure "svscanboot" within the Entware init system because of the changed name. That exec process name change will need a workaround.



        Mimic svscanboot



        The entware-daemontools-init-script can be changed to mimic svscanboot, like:



        ENABLED=yes
        #PRECMD="exec </dev/null;exec >/dev/null;exec 2>/dev/null;/opt/bin/svc -dx /opt/service/* /opt/service/*/log"
        PROCS="svscan"
        ARGS="/opt/service 2>&1"
        PATH=/opt/sbin:/opt/bin:/sbin:/bin:/usr/sbin:/usr/bin
        PREARGS="env - PATH=$PATH"
        DESC="daemontools"


        The commented PRECMD makes that svscan will not be started.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 13 '17 at 12:36









        Community

        1




        1










        answered Dec 2 '16 at 16:13









        Pro Backup

        1,94862956




        1,94862956






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f326685%2fhow-to-correctly-stop-busybox-shell-process-and-child-process%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

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt