Kill a running shell script, and kill all process running within it











up vote
2
down vote

favorite
1












I have a shell script running in the background, which runs 5 different jobs.



When I run the shell script all 5 jobs start running in order.
When I kill this shell, whichever process is running, will still continue to run, even though I have killed the shell.



$ bash shell.sh & echo $!


this gives me the PID of the shell, which allows me to kill it later if I wish to do so at some point.



#!/bin/bash

JOB1
JOB2
JOB3
JOB4
JOB5


How can I make the shell kill all process running within, once it has been killed?










share|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    2
    down vote

    favorite
    1












    I have a shell script running in the background, which runs 5 different jobs.



    When I run the shell script all 5 jobs start running in order.
    When I kill this shell, whichever process is running, will still continue to run, even though I have killed the shell.



    $ bash shell.sh & echo $!


    this gives me the PID of the shell, which allows me to kill it later if I wish to do so at some point.



    #!/bin/bash

    JOB1
    JOB2
    JOB3
    JOB4
    JOB5


    How can I make the shell kill all process running within, once it has been killed?










    share|improve this question
















    bumped to the homepage by Community 2 days ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I have a shell script running in the background, which runs 5 different jobs.



      When I run the shell script all 5 jobs start running in order.
      When I kill this shell, whichever process is running, will still continue to run, even though I have killed the shell.



      $ bash shell.sh & echo $!


      this gives me the PID of the shell, which allows me to kill it later if I wish to do so at some point.



      #!/bin/bash

      JOB1
      JOB2
      JOB3
      JOB4
      JOB5


      How can I make the shell kill all process running within, once it has been killed?










      share|improve this question















      I have a shell script running in the background, which runs 5 different jobs.



      When I run the shell script all 5 jobs start running in order.
      When I kill this shell, whichever process is running, will still continue to run, even though I have killed the shell.



      $ bash shell.sh & echo $!


      this gives me the PID of the shell, which allows me to kill it later if I wish to do so at some point.



      #!/bin/bash

      JOB1
      JOB2
      JOB3
      JOB4
      JOB5


      How can I make the shell kill all process running within, once it has been killed?







      shell-script process kill






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 25 '16 at 1:32









      Jeff Schaller

      37.8k1053122




      37.8k1053122










      asked Feb 4 '16 at 20:14









      user

      112




      112





      bumped to the homepage by Community 2 days ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 2 days ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          #!/bin/bash
          declare -a bgpids

          cleanup() {
          for pid in ${bgpids[@]}; do
          kill -9 $pid
          done
          }
          trap "cleanup" SIGINT SIGTERM

          sometask &
          bgpids+=("$!")





          share|improve this answer

















          • 1




            jobs -p works for pids. But kill -- -0 should be enough.
            – mikeserv
            Feb 4 '16 at 20:22










          • I forgot that kill could effectively look up children. Good catch!
            – DopeGhoti
            Feb 4 '16 at 20:23












          • It doesnt look up children - but if everybody's in the same process group then just kill the group. kill -0 is a whole different animal to kill -- -0.
            – mikeserv
            Feb 4 '16 at 20:25











          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%2f259939%2fkill-a-running-shell-script-and-kill-all-process-running-within-it%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













          #!/bin/bash
          declare -a bgpids

          cleanup() {
          for pid in ${bgpids[@]}; do
          kill -9 $pid
          done
          }
          trap "cleanup" SIGINT SIGTERM

          sometask &
          bgpids+=("$!")





          share|improve this answer

















          • 1




            jobs -p works for pids. But kill -- -0 should be enough.
            – mikeserv
            Feb 4 '16 at 20:22










          • I forgot that kill could effectively look up children. Good catch!
            – DopeGhoti
            Feb 4 '16 at 20:23












          • It doesnt look up children - but if everybody's in the same process group then just kill the group. kill -0 is a whole different animal to kill -- -0.
            – mikeserv
            Feb 4 '16 at 20:25















          up vote
          0
          down vote













          #!/bin/bash
          declare -a bgpids

          cleanup() {
          for pid in ${bgpids[@]}; do
          kill -9 $pid
          done
          }
          trap "cleanup" SIGINT SIGTERM

          sometask &
          bgpids+=("$!")





          share|improve this answer

















          • 1




            jobs -p works for pids. But kill -- -0 should be enough.
            – mikeserv
            Feb 4 '16 at 20:22










          • I forgot that kill could effectively look up children. Good catch!
            – DopeGhoti
            Feb 4 '16 at 20:23












          • It doesnt look up children - but if everybody's in the same process group then just kill the group. kill -0 is a whole different animal to kill -- -0.
            – mikeserv
            Feb 4 '16 at 20:25













          up vote
          0
          down vote










          up vote
          0
          down vote









          #!/bin/bash
          declare -a bgpids

          cleanup() {
          for pid in ${bgpids[@]}; do
          kill -9 $pid
          done
          }
          trap "cleanup" SIGINT SIGTERM

          sometask &
          bgpids+=("$!")





          share|improve this answer












          #!/bin/bash
          declare -a bgpids

          cleanup() {
          for pid in ${bgpids[@]}; do
          kill -9 $pid
          done
          }
          trap "cleanup" SIGINT SIGTERM

          sometask &
          bgpids+=("$!")






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 4 '16 at 20:20









          DopeGhoti

          43k55382




          43k55382








          • 1




            jobs -p works for pids. But kill -- -0 should be enough.
            – mikeserv
            Feb 4 '16 at 20:22










          • I forgot that kill could effectively look up children. Good catch!
            – DopeGhoti
            Feb 4 '16 at 20:23












          • It doesnt look up children - but if everybody's in the same process group then just kill the group. kill -0 is a whole different animal to kill -- -0.
            – mikeserv
            Feb 4 '16 at 20:25














          • 1




            jobs -p works for pids. But kill -- -0 should be enough.
            – mikeserv
            Feb 4 '16 at 20:22










          • I forgot that kill could effectively look up children. Good catch!
            – DopeGhoti
            Feb 4 '16 at 20:23












          • It doesnt look up children - but if everybody's in the same process group then just kill the group. kill -0 is a whole different animal to kill -- -0.
            – mikeserv
            Feb 4 '16 at 20:25








          1




          1




          jobs -p works for pids. But kill -- -0 should be enough.
          – mikeserv
          Feb 4 '16 at 20:22




          jobs -p works for pids. But kill -- -0 should be enough.
          – mikeserv
          Feb 4 '16 at 20:22












          I forgot that kill could effectively look up children. Good catch!
          – DopeGhoti
          Feb 4 '16 at 20:23






          I forgot that kill could effectively look up children. Good catch!
          – DopeGhoti
          Feb 4 '16 at 20:23














          It doesnt look up children - but if everybody's in the same process group then just kill the group. kill -0 is a whole different animal to kill -- -0.
          – mikeserv
          Feb 4 '16 at 20:25




          It doesnt look up children - but if everybody's in the same process group then just kill the group. kill -0 is a whole different animal to kill -- -0.
          – mikeserv
          Feb 4 '16 at 20:25


















          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%2f259939%2fkill-a-running-shell-script-and-kill-all-process-running-within-it%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

          サソリ

          広島県道265号伴広島線

          Setup Asymptote in Texstudio