Killing the previous instances of a script before running the same Unix script

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












2














I want to kill the background process belonging to a shell script that I am going to run again.



That means before executing the shell script I want to delete the background process running for the same script.










share|improve this question























  • Did you mean you want to kill all the child processes started by the shell script before you run it again?
    – Sree
    Jul 1 '15 at 10:55










  • Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
    – terdon
    Jul 1 '15 at 11:04















2














I want to kill the background process belonging to a shell script that I am going to run again.



That means before executing the shell script I want to delete the background process running for the same script.










share|improve this question























  • Did you mean you want to kill all the child processes started by the shell script before you run it again?
    – Sree
    Jul 1 '15 at 10:55










  • Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
    – terdon
    Jul 1 '15 at 11:04













2












2








2







I want to kill the background process belonging to a shell script that I am going to run again.



That means before executing the shell script I want to delete the background process running for the same script.










share|improve this question















I want to kill the background process belonging to a shell script that I am going to run again.



That means before executing the shell script I want to delete the background process running for the same script.







shell-script background-process






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 20 '15 at 1:13









Thomas Dickey

52.1k594164




52.1k594164










asked Jul 1 '15 at 10:53









Abin

1112




1112











  • Did you mean you want to kill all the child processes started by the shell script before you run it again?
    – Sree
    Jul 1 '15 at 10:55










  • Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
    – terdon
    Jul 1 '15 at 11:04
















  • Did you mean you want to kill all the child processes started by the shell script before you run it again?
    – Sree
    Jul 1 '15 at 10:55










  • Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
    – terdon
    Jul 1 '15 at 11:04















Did you mean you want to kill all the child processes started by the shell script before you run it again?
– Sree
Jul 1 '15 at 10:55




Did you mean you want to kill all the child processes started by the shell script before you run it again?
– Sree
Jul 1 '15 at 10:55












Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
– terdon
Jul 1 '15 at 11:04




Please edit your question and give us a simple example we can reproduce. What process? The script itself? Something the script launches? How is it launched?
– terdon
Jul 1 '15 at 11:04










6 Answers
6






active

oldest

votes


















4














Check for the existence of a PID from the same script.



add this at the beginning of the script:



#!/bin/bash
script_name=$BASH_SOURCE[0]
for pid in $(pidof -x $script_name); do
if [ $pid != $$ ]; then
kill -9 $pid
fi
done





share|improve this answer






























    2














    I did this a long time back in one of my shell scripts. Here is how I did it:



    ps aux | 
    grep -P $BASH_SOURCE[0] |
    grep -v $$ |
    grep -P "bash" |
    grep -oP "^[[:alnum:]]+s+d+s" |
    grep -oP "d+" |
    xargs kill -9


    The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.



    A sample script to demonstrate the above method is this:



    #!/bin/bash

    ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"

    sleep 100


    Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.






    share|improve this answer






























      0














      Try this:



      #!/bin/sh
      if [ -f /var/run/sh.pid ]; then
      echo "Process already running."
      kill -9 `cat /var/run/sh.pid`
      rm -f /var/run/sh.pid
      fi
      echo `pidof $$` > /var/run/sh.pid

      # From here, your normal shell script can resume





      share|improve this answer






















      • This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
        – shivams
        Jul 1 '15 at 11:10


















      0














      this one is shorter and worked for me in my ruby app



      ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9


      replace search_pattern with the name of your script






      share|improve this answer




















      • grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach...
        – jasonwryan
        Jun 11 '16 at 6:45


















      0














      kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)



      Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.






      share|improve this answer








      New contributor




      Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.

























        0














        kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)


        Very simple.

        Uses pgrep to search for all instances of the currently running script.



        pgrep is nice since it only returns pids.



        Then grep out the currently running pid using $$, so it won't suicide.



        Finally kill -9 terminates the previous instances.






        share|improve this answer








        New contributor




        Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.

















          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%2f213288%2fkilling-the-previous-instances-of-a-script-before-running-the-same-unix-script%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          Check for the existence of a PID from the same script.



          add this at the beginning of the script:



          #!/bin/bash
          script_name=$BASH_SOURCE[0]
          for pid in $(pidof -x $script_name); do
          if [ $pid != $$ ]; then
          kill -9 $pid
          fi
          done





          share|improve this answer



























            4














            Check for the existence of a PID from the same script.



            add this at the beginning of the script:



            #!/bin/bash
            script_name=$BASH_SOURCE[0]
            for pid in $(pidof -x $script_name); do
            if [ $pid != $$ ]; then
            kill -9 $pid
            fi
            done





            share|improve this answer

























              4












              4








              4






              Check for the existence of a PID from the same script.



              add this at the beginning of the script:



              #!/bin/bash
              script_name=$BASH_SOURCE[0]
              for pid in $(pidof -x $script_name); do
              if [ $pid != $$ ]; then
              kill -9 $pid
              fi
              done





              share|improve this answer














              Check for the existence of a PID from the same script.



              add this at the beginning of the script:



              #!/bin/bash
              script_name=$BASH_SOURCE[0]
              for pid in $(pidof -x $script_name); do
              if [ $pid != $$ ]; then
              kill -9 $pid
              fi
              done






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 22 '18 at 23:58









              Wis

              33




              33










              answered Jul 1 '15 at 11:10









              jcbermu

              3,312819




              3,312819























                  2














                  I did this a long time back in one of my shell scripts. Here is how I did it:



                  ps aux | 
                  grep -P $BASH_SOURCE[0] |
                  grep -v $$ |
                  grep -P "bash" |
                  grep -oP "^[[:alnum:]]+s+d+s" |
                  grep -oP "d+" |
                  xargs kill -9


                  The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.



                  A sample script to demonstrate the above method is this:



                  #!/bin/bash

                  ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"

                  sleep 100


                  Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.






                  share|improve this answer



























                    2














                    I did this a long time back in one of my shell scripts. Here is how I did it:



                    ps aux | 
                    grep -P $BASH_SOURCE[0] |
                    grep -v $$ |
                    grep -P "bash" |
                    grep -oP "^[[:alnum:]]+s+d+s" |
                    grep -oP "d+" |
                    xargs kill -9


                    The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.



                    A sample script to demonstrate the above method is this:



                    #!/bin/bash

                    ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"

                    sleep 100


                    Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.






                    share|improve this answer

























                      2












                      2








                      2






                      I did this a long time back in one of my shell scripts. Here is how I did it:



                      ps aux | 
                      grep -P $BASH_SOURCE[0] |
                      grep -v $$ |
                      grep -P "bash" |
                      grep -oP "^[[:alnum:]]+s+d+s" |
                      grep -oP "d+" |
                      xargs kill -9


                      The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.



                      A sample script to demonstrate the above method is this:



                      #!/bin/bash

                      ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"

                      sleep 100


                      Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.






                      share|improve this answer














                      I did this a long time back in one of my shell scripts. Here is how I did it:



                      ps aux | 
                      grep -P $BASH_SOURCE[0] |
                      grep -v $$ |
                      grep -P "bash" |
                      grep -oP "^[[:alnum:]]+s+d+s" |
                      grep -oP "d+" |
                      xargs kill -9


                      The beauty of this method is that it will NOT kill the current running script itself, only the previous instances of it.



                      A sample script to demonstrate the above method is this:



                      #!/bin/bash

                      ps aux | grep -P $BASH_SOURCE[0] | grep -v $$ | grep -P "bash" | grep -oP "^[[:alnum:]]+s+d+s" | grep -oP "d+"

                      sleep 100


                      Now, run one instance of this script in your terminal. And then run another instance in a different terminal. You will see that the previous instance will be immediately killed while the second one runs fine.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 1 '15 at 11:23

























                      answered Jul 1 '15 at 11:05









                      shivams

                      2,89111425




                      2,89111425





















                          0














                          Try this:



                          #!/bin/sh
                          if [ -f /var/run/sh.pid ]; then
                          echo "Process already running."
                          kill -9 `cat /var/run/sh.pid`
                          rm -f /var/run/sh.pid
                          fi
                          echo `pidof $$` > /var/run/sh.pid

                          # From here, your normal shell script can resume





                          share|improve this answer






















                          • This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
                            – shivams
                            Jul 1 '15 at 11:10















                          0














                          Try this:



                          #!/bin/sh
                          if [ -f /var/run/sh.pid ]; then
                          echo "Process already running."
                          kill -9 `cat /var/run/sh.pid`
                          rm -f /var/run/sh.pid
                          fi
                          echo `pidof $$` > /var/run/sh.pid

                          # From here, your normal shell script can resume





                          share|improve this answer






















                          • This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
                            – shivams
                            Jul 1 '15 at 11:10













                          0












                          0








                          0






                          Try this:



                          #!/bin/sh
                          if [ -f /var/run/sh.pid ]; then
                          echo "Process already running."
                          kill -9 `cat /var/run/sh.pid`
                          rm -f /var/run/sh.pid
                          fi
                          echo `pidof $$` > /var/run/sh.pid

                          # From here, your normal shell script can resume





                          share|improve this answer














                          Try this:



                          #!/bin/sh
                          if [ -f /var/run/sh.pid ]; then
                          echo "Process already running."
                          kill -9 `cat /var/run/sh.pid`
                          rm -f /var/run/sh.pid
                          fi
                          echo `pidof $$` > /var/run/sh.pid

                          # From here, your normal shell script can resume






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jul 1 '15 at 11:15

























                          answered Jul 1 '15 at 11:04









                          SHW

                          8,02033570




                          8,02033570











                          • This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
                            – shivams
                            Jul 1 '15 at 11:10
















                          • This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
                            – shivams
                            Jul 1 '15 at 11:10















                          This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
                          – shivams
                          Jul 1 '15 at 11:10




                          This method will kill the script itself, besides killing its previous instances. OP wants to kill only the previous instances.
                          – shivams
                          Jul 1 '15 at 11:10











                          0














                          this one is shorter and worked for me in my ruby app



                          ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9


                          replace search_pattern with the name of your script






                          share|improve this answer




















                          • grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach...
                            – jasonwryan
                            Jun 11 '16 at 6:45















                          0














                          this one is shorter and worked for me in my ruby app



                          ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9


                          replace search_pattern with the name of your script






                          share|improve this answer




















                          • grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach...
                            – jasonwryan
                            Jun 11 '16 at 6:45













                          0












                          0








                          0






                          this one is shorter and worked for me in my ruby app



                          ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9


                          replace search_pattern with the name of your script






                          share|improve this answer












                          this one is shorter and worked for me in my ruby app



                          ps -ef| grep search_pattern | awk 'print $2' | xargs kill -9


                          replace search_pattern with the name of your script







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jun 11 '16 at 6:25









                          nullqube

                          1




                          1











                          • grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach...
                            – jasonwryan
                            Jun 11 '16 at 6:45
















                          • grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach...
                            – jasonwryan
                            Jun 11 '16 at 6:45















                          grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach...
                          – jasonwryan
                          Jun 11 '16 at 6:45




                          grep and awk is redundant; Awk does pattern matching. pgrep would be an even simpler approach...
                          – jasonwryan
                          Jun 11 '16 at 6:45











                          0














                          kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)



                          Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.






                          share|improve this answer








                          New contributor




                          Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






















                            0














                            kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)



                            Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.






                            share|improve this answer








                            New contributor




                            Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.




















                              0












                              0








                              0






                              kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)



                              Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.






                              share|improve this answer








                              New contributor




                              Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.









                              kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)



                              Very simple. Uses pgrep to search for all instances of the currently running script. pgrep is nice since it only returns pids. Then grep out the currently running pid, so it won't suicide. Finally kill -9 terminates the previous instances.







                              share|improve this answer








                              New contributor




                              Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.









                              share|improve this answer



                              share|improve this answer






                              New contributor




                              Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.









                              answered Dec 31 '18 at 17:00









                              Anonymous

                              1




                              1




                              New contributor




                              Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.





                              New contributor





                              Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.






                              Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.





















                                  0














                                  kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)


                                  Very simple.

                                  Uses pgrep to search for all instances of the currently running script.



                                  pgrep is nice since it only returns pids.



                                  Then grep out the currently running pid using $$, so it won't suicide.



                                  Finally kill -9 terminates the previous instances.






                                  share|improve this answer








                                  New contributor




                                  Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.






















                                    0














                                    kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)


                                    Very simple.

                                    Uses pgrep to search for all instances of the currently running script.



                                    pgrep is nice since it only returns pids.



                                    Then grep out the currently running pid using $$, so it won't suicide.



                                    Finally kill -9 terminates the previous instances.






                                    share|improve this answer








                                    New contributor




                                    Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.




















                                      0












                                      0








                                      0






                                      kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)


                                      Very simple.

                                      Uses pgrep to search for all instances of the currently running script.



                                      pgrep is nice since it only returns pids.



                                      Then grep out the currently running pid using $$, so it won't suicide.



                                      Finally kill -9 terminates the previous instances.






                                      share|improve this answer








                                      New contributor




                                      Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.









                                      kill -9 $(pgrep -f $BASH_SOURCE[0] | grep -v $$)


                                      Very simple.

                                      Uses pgrep to search for all instances of the currently running script.



                                      pgrep is nice since it only returns pids.



                                      Then grep out the currently running pid using $$, so it won't suicide.



                                      Finally kill -9 terminates the previous instances.







                                      share|improve this answer








                                      New contributor




                                      Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.









                                      share|improve this answer



                                      share|improve this answer






                                      New contributor




                                      Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.









                                      answered Dec 31 '18 at 17:04









                                      Anonymous

                                      1




                                      1




                                      New contributor




                                      Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.





                                      New contributor





                                      Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.






                                      Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                      Check out our Code of Conduct.



























                                          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%2f213288%2fkilling-the-previous-instances-of-a-script-before-running-the-same-unix-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

                                          Peggy Mitchell

                                          Palaiologos

                                          The Forum (Inglewood, California)