Stopping a script process with a Control + C or something

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











up vote
0
down vote

favorite












I am trying to make an AV bug raspberry pi for a class.



I am using sox to record sound.
Which is working fine.



the issue is sox needs to be stopped by a control+C to stop and create the new file. If killall is sent from a different ssh session it will drop the other session and sox will not create the file.



listen.sh



#! /bin/bash
NOW=$( date '+%F_%H:%M:%S' )

filename="/home/pi/gets/$NOW.wav"

sox -t alsa plughw:1 $NOW.wav;

sleep 6;

echo $filename


I have tried making a separate script for stopping it; pretty much



killlisten.sh



#! /bin/bash
sleep 5;
ps | grep sox | kill 0;


Then run a



superscript.sh



#! /bin/bash
./listen.sh;
./killlisten.sh;


Any advice on how to stop sox in a way that would still produce an output file would be great. This will ideally be set to run at set times so avoiding human interaction is essential.







share|improve this question


























    up vote
    0
    down vote

    favorite












    I am trying to make an AV bug raspberry pi for a class.



    I am using sox to record sound.
    Which is working fine.



    the issue is sox needs to be stopped by a control+C to stop and create the new file. If killall is sent from a different ssh session it will drop the other session and sox will not create the file.



    listen.sh



    #! /bin/bash
    NOW=$( date '+%F_%H:%M:%S' )

    filename="/home/pi/gets/$NOW.wav"

    sox -t alsa plughw:1 $NOW.wav;

    sleep 6;

    echo $filename


    I have tried making a separate script for stopping it; pretty much



    killlisten.sh



    #! /bin/bash
    sleep 5;
    ps | grep sox | kill 0;


    Then run a



    superscript.sh



    #! /bin/bash
    ./listen.sh;
    ./killlisten.sh;


    Any advice on how to stop sox in a way that would still produce an output file would be great. This will ideally be set to run at set times so avoiding human interaction is essential.







    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to make an AV bug raspberry pi for a class.



      I am using sox to record sound.
      Which is working fine.



      the issue is sox needs to be stopped by a control+C to stop and create the new file. If killall is sent from a different ssh session it will drop the other session and sox will not create the file.



      listen.sh



      #! /bin/bash
      NOW=$( date '+%F_%H:%M:%S' )

      filename="/home/pi/gets/$NOW.wav"

      sox -t alsa plughw:1 $NOW.wav;

      sleep 6;

      echo $filename


      I have tried making a separate script for stopping it; pretty much



      killlisten.sh



      #! /bin/bash
      sleep 5;
      ps | grep sox | kill 0;


      Then run a



      superscript.sh



      #! /bin/bash
      ./listen.sh;
      ./killlisten.sh;


      Any advice on how to stop sox in a way that would still produce an output file would be great. This will ideally be set to run at set times so avoiding human interaction is essential.







      share|improve this question














      I am trying to make an AV bug raspberry pi for a class.



      I am using sox to record sound.
      Which is working fine.



      the issue is sox needs to be stopped by a control+C to stop and create the new file. If killall is sent from a different ssh session it will drop the other session and sox will not create the file.



      listen.sh



      #! /bin/bash
      NOW=$( date '+%F_%H:%M:%S' )

      filename="/home/pi/gets/$NOW.wav"

      sox -t alsa plughw:1 $NOW.wav;

      sleep 6;

      echo $filename


      I have tried making a separate script for stopping it; pretty much



      killlisten.sh



      #! /bin/bash
      sleep 5;
      ps | grep sox | kill 0;


      Then run a



      superscript.sh



      #! /bin/bash
      ./listen.sh;
      ./killlisten.sh;


      Any advice on how to stop sox in a way that would still produce an output file would be great. This will ideally be set to run at set times so avoiding human interaction is essential.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 6 at 12:14









      Stephen Kitt

      141k22307367




      141k22307367










      asked Mar 6 at 11:09









      Reckless Liberty

      84




      84




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Your pipeline



          ps -aux | grep sox | kill 0


          will not do what you want to do. This is because kill won't ever read the input from grep (the result from grep will also contain a lot of other things than just the PID of the sox process).



          If you have pkill, just do



          pkill sox


          instead (use pkill -INT sox to send the same signal as Ctrl+C does).



          If you change your startup script to



          #!/bin/bash

          NOW=$( date '+%F_%H:%M:%S' )

          filename="/home/pi/gets/$NOW.wav"
          sox -t alsa plughw:1 "$NOW.wav" & sox_pid="$!"

          printf 'sox pid is %dn' "$sox_pid"

          wait

          # Alternatively (instead of "wait", if you want to kill sox after six seconds):
          # sleep 6 && kill "$sox_pid"

          echo "$filename"


          You will get the PID of the sox process printed to the terminal and you could use that to do kill pid (with pid replaced by that number).



          Using & ofte the sox invocation places it in the background. The PID of that background task is automatically stored in $! and the code above assigns it to sox_pid which is later printed.



          The wait command waits for the sox command (running in the background) to finish.




          As we discussed in a previous session: Double-quote all variable expansions.






          share|improve this answer






















          • Awesome, thank you for the great explanation as well as the solution!
            – Reckless Liberty
            Mar 7 at 11:00











          • for: printf 'sox pid is %dn' "$sox_pid" Did you %d to ensure that the PID was inputted as an integer? Why not use %s? Just trying to learn, thank you.
            – Reckless Liberty
            Mar 8 at 10:34











          • @RecklessLiberty No worries :-) I used %d because I'm assuming that $sox_pid is an integer. If it's not an integer (for whatever reason), then the statement will generate an error message that can easily be seen. You could obviously use %s for everything, but I personally tend to use more specific format specifiers if I know that the thing I'm printing is of a particular type.
            – Kusalananda
            Mar 8 at 10:42










          • Error IDing, nice. The & after the sox is so BASH will create the $sox_pid? and printf? so sox needs to be in the bg for the rest of the script to work?
            – Reckless Liberty
            Mar 8 at 10:53











          • @RecklessLiberty The & starts the given command as a background task, which gives control back to the script immediately. The PID of the command is then available in $!. The printf is solely for the user, in case they would want to kill the job manually with kill so that they know what PID to kill (it's not strictly needed).
            – Kusalananda
            Mar 8 at 11:09

















          up vote
          0
          down vote













          kill -SIGINT <pid>
          is the usual equivalent to ^C.






          share|improve this answer



























            up vote
            0
            down vote













            use trapping:



            #!/bin/bash

            # this is a trap for ctrl + c
            trap ctrl_c INT

            function ctrl_c()

            echo "Trap: CTRL+C received, exit"
            exit






            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: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f428469%2fstopping-a-script-process-with-a-control-c-or-something%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              Your pipeline



              ps -aux | grep sox | kill 0


              will not do what you want to do. This is because kill won't ever read the input from grep (the result from grep will also contain a lot of other things than just the PID of the sox process).



              If you have pkill, just do



              pkill sox


              instead (use pkill -INT sox to send the same signal as Ctrl+C does).



              If you change your startup script to



              #!/bin/bash

              NOW=$( date '+%F_%H:%M:%S' )

              filename="/home/pi/gets/$NOW.wav"
              sox -t alsa plughw:1 "$NOW.wav" & sox_pid="$!"

              printf 'sox pid is %dn' "$sox_pid"

              wait

              # Alternatively (instead of "wait", if you want to kill sox after six seconds):
              # sleep 6 && kill "$sox_pid"

              echo "$filename"


              You will get the PID of the sox process printed to the terminal and you could use that to do kill pid (with pid replaced by that number).



              Using & ofte the sox invocation places it in the background. The PID of that background task is automatically stored in $! and the code above assigns it to sox_pid which is later printed.



              The wait command waits for the sox command (running in the background) to finish.




              As we discussed in a previous session: Double-quote all variable expansions.






              share|improve this answer






















              • Awesome, thank you for the great explanation as well as the solution!
                – Reckless Liberty
                Mar 7 at 11:00











              • for: printf 'sox pid is %dn' "$sox_pid" Did you %d to ensure that the PID was inputted as an integer? Why not use %s? Just trying to learn, thank you.
                – Reckless Liberty
                Mar 8 at 10:34











              • @RecklessLiberty No worries :-) I used %d because I'm assuming that $sox_pid is an integer. If it's not an integer (for whatever reason), then the statement will generate an error message that can easily be seen. You could obviously use %s for everything, but I personally tend to use more specific format specifiers if I know that the thing I'm printing is of a particular type.
                – Kusalananda
                Mar 8 at 10:42










              • Error IDing, nice. The & after the sox is so BASH will create the $sox_pid? and printf? so sox needs to be in the bg for the rest of the script to work?
                – Reckless Liberty
                Mar 8 at 10:53











              • @RecklessLiberty The & starts the given command as a background task, which gives control back to the script immediately. The PID of the command is then available in $!. The printf is solely for the user, in case they would want to kill the job manually with kill so that they know what PID to kill (it's not strictly needed).
                – Kusalananda
                Mar 8 at 11:09














              up vote
              1
              down vote



              accepted










              Your pipeline



              ps -aux | grep sox | kill 0


              will not do what you want to do. This is because kill won't ever read the input from grep (the result from grep will also contain a lot of other things than just the PID of the sox process).



              If you have pkill, just do



              pkill sox


              instead (use pkill -INT sox to send the same signal as Ctrl+C does).



              If you change your startup script to



              #!/bin/bash

              NOW=$( date '+%F_%H:%M:%S' )

              filename="/home/pi/gets/$NOW.wav"
              sox -t alsa plughw:1 "$NOW.wav" & sox_pid="$!"

              printf 'sox pid is %dn' "$sox_pid"

              wait

              # Alternatively (instead of "wait", if you want to kill sox after six seconds):
              # sleep 6 && kill "$sox_pid"

              echo "$filename"


              You will get the PID of the sox process printed to the terminal and you could use that to do kill pid (with pid replaced by that number).



              Using & ofte the sox invocation places it in the background. The PID of that background task is automatically stored in $! and the code above assigns it to sox_pid which is later printed.



              The wait command waits for the sox command (running in the background) to finish.




              As we discussed in a previous session: Double-quote all variable expansions.






              share|improve this answer






















              • Awesome, thank you for the great explanation as well as the solution!
                – Reckless Liberty
                Mar 7 at 11:00











              • for: printf 'sox pid is %dn' "$sox_pid" Did you %d to ensure that the PID was inputted as an integer? Why not use %s? Just trying to learn, thank you.
                – Reckless Liberty
                Mar 8 at 10:34











              • @RecklessLiberty No worries :-) I used %d because I'm assuming that $sox_pid is an integer. If it's not an integer (for whatever reason), then the statement will generate an error message that can easily be seen. You could obviously use %s for everything, but I personally tend to use more specific format specifiers if I know that the thing I'm printing is of a particular type.
                – Kusalananda
                Mar 8 at 10:42










              • Error IDing, nice. The & after the sox is so BASH will create the $sox_pid? and printf? so sox needs to be in the bg for the rest of the script to work?
                – Reckless Liberty
                Mar 8 at 10:53











              • @RecklessLiberty The & starts the given command as a background task, which gives control back to the script immediately. The PID of the command is then available in $!. The printf is solely for the user, in case they would want to kill the job manually with kill so that they know what PID to kill (it's not strictly needed).
                – Kusalananda
                Mar 8 at 11:09












              up vote
              1
              down vote



              accepted







              up vote
              1
              down vote



              accepted






              Your pipeline



              ps -aux | grep sox | kill 0


              will not do what you want to do. This is because kill won't ever read the input from grep (the result from grep will also contain a lot of other things than just the PID of the sox process).



              If you have pkill, just do



              pkill sox


              instead (use pkill -INT sox to send the same signal as Ctrl+C does).



              If you change your startup script to



              #!/bin/bash

              NOW=$( date '+%F_%H:%M:%S' )

              filename="/home/pi/gets/$NOW.wav"
              sox -t alsa plughw:1 "$NOW.wav" & sox_pid="$!"

              printf 'sox pid is %dn' "$sox_pid"

              wait

              # Alternatively (instead of "wait", if you want to kill sox after six seconds):
              # sleep 6 && kill "$sox_pid"

              echo "$filename"


              You will get the PID of the sox process printed to the terminal and you could use that to do kill pid (with pid replaced by that number).



              Using & ofte the sox invocation places it in the background. The PID of that background task is automatically stored in $! and the code above assigns it to sox_pid which is later printed.



              The wait command waits for the sox command (running in the background) to finish.




              As we discussed in a previous session: Double-quote all variable expansions.






              share|improve this answer














              Your pipeline



              ps -aux | grep sox | kill 0


              will not do what you want to do. This is because kill won't ever read the input from grep (the result from grep will also contain a lot of other things than just the PID of the sox process).



              If you have pkill, just do



              pkill sox


              instead (use pkill -INT sox to send the same signal as Ctrl+C does).



              If you change your startup script to



              #!/bin/bash

              NOW=$( date '+%F_%H:%M:%S' )

              filename="/home/pi/gets/$NOW.wav"
              sox -t alsa plughw:1 "$NOW.wav" & sox_pid="$!"

              printf 'sox pid is %dn' "$sox_pid"

              wait

              # Alternatively (instead of "wait", if you want to kill sox after six seconds):
              # sleep 6 && kill "$sox_pid"

              echo "$filename"


              You will get the PID of the sox process printed to the terminal and you could use that to do kill pid (with pid replaced by that number).



              Using & ofte the sox invocation places it in the background. The PID of that background task is automatically stored in $! and the code above assigns it to sox_pid which is later printed.



              The wait command waits for the sox command (running in the background) to finish.




              As we discussed in a previous session: Double-quote all variable expansions.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 7 at 11:12

























              answered Mar 6 at 11:14









              Kusalananda

              103k13202318




              103k13202318











              • Awesome, thank you for the great explanation as well as the solution!
                – Reckless Liberty
                Mar 7 at 11:00











              • for: printf 'sox pid is %dn' "$sox_pid" Did you %d to ensure that the PID was inputted as an integer? Why not use %s? Just trying to learn, thank you.
                – Reckless Liberty
                Mar 8 at 10:34











              • @RecklessLiberty No worries :-) I used %d because I'm assuming that $sox_pid is an integer. If it's not an integer (for whatever reason), then the statement will generate an error message that can easily be seen. You could obviously use %s for everything, but I personally tend to use more specific format specifiers if I know that the thing I'm printing is of a particular type.
                – Kusalananda
                Mar 8 at 10:42










              • Error IDing, nice. The & after the sox is so BASH will create the $sox_pid? and printf? so sox needs to be in the bg for the rest of the script to work?
                – Reckless Liberty
                Mar 8 at 10:53











              • @RecklessLiberty The & starts the given command as a background task, which gives control back to the script immediately. The PID of the command is then available in $!. The printf is solely for the user, in case they would want to kill the job manually with kill so that they know what PID to kill (it's not strictly needed).
                – Kusalananda
                Mar 8 at 11:09
















              • Awesome, thank you for the great explanation as well as the solution!
                – Reckless Liberty
                Mar 7 at 11:00











              • for: printf 'sox pid is %dn' "$sox_pid" Did you %d to ensure that the PID was inputted as an integer? Why not use %s? Just trying to learn, thank you.
                – Reckless Liberty
                Mar 8 at 10:34











              • @RecklessLiberty No worries :-) I used %d because I'm assuming that $sox_pid is an integer. If it's not an integer (for whatever reason), then the statement will generate an error message that can easily be seen. You could obviously use %s for everything, but I personally tend to use more specific format specifiers if I know that the thing I'm printing is of a particular type.
                – Kusalananda
                Mar 8 at 10:42










              • Error IDing, nice. The & after the sox is so BASH will create the $sox_pid? and printf? so sox needs to be in the bg for the rest of the script to work?
                – Reckless Liberty
                Mar 8 at 10:53











              • @RecklessLiberty The & starts the given command as a background task, which gives control back to the script immediately. The PID of the command is then available in $!. The printf is solely for the user, in case they would want to kill the job manually with kill so that they know what PID to kill (it's not strictly needed).
                – Kusalananda
                Mar 8 at 11:09















              Awesome, thank you for the great explanation as well as the solution!
              – Reckless Liberty
              Mar 7 at 11:00





              Awesome, thank you for the great explanation as well as the solution!
              – Reckless Liberty
              Mar 7 at 11:00













              for: printf 'sox pid is %dn' "$sox_pid" Did you %d to ensure that the PID was inputted as an integer? Why not use %s? Just trying to learn, thank you.
              – Reckless Liberty
              Mar 8 at 10:34





              for: printf 'sox pid is %dn' "$sox_pid" Did you %d to ensure that the PID was inputted as an integer? Why not use %s? Just trying to learn, thank you.
              – Reckless Liberty
              Mar 8 at 10:34













              @RecklessLiberty No worries :-) I used %d because I'm assuming that $sox_pid is an integer. If it's not an integer (for whatever reason), then the statement will generate an error message that can easily be seen. You could obviously use %s for everything, but I personally tend to use more specific format specifiers if I know that the thing I'm printing is of a particular type.
              – Kusalananda
              Mar 8 at 10:42




              @RecklessLiberty No worries :-) I used %d because I'm assuming that $sox_pid is an integer. If it's not an integer (for whatever reason), then the statement will generate an error message that can easily be seen. You could obviously use %s for everything, but I personally tend to use more specific format specifiers if I know that the thing I'm printing is of a particular type.
              – Kusalananda
              Mar 8 at 10:42












              Error IDing, nice. The & after the sox is so BASH will create the $sox_pid? and printf? so sox needs to be in the bg for the rest of the script to work?
              – Reckless Liberty
              Mar 8 at 10:53





              Error IDing, nice. The & after the sox is so BASH will create the $sox_pid? and printf? so sox needs to be in the bg for the rest of the script to work?
              – Reckless Liberty
              Mar 8 at 10:53













              @RecklessLiberty The & starts the given command as a background task, which gives control back to the script immediately. The PID of the command is then available in $!. The printf is solely for the user, in case they would want to kill the job manually with kill so that they know what PID to kill (it's not strictly needed).
              – Kusalananda
              Mar 8 at 11:09




              @RecklessLiberty The & starts the given command as a background task, which gives control back to the script immediately. The PID of the command is then available in $!. The printf is solely for the user, in case they would want to kill the job manually with kill so that they know what PID to kill (it's not strictly needed).
              – Kusalananda
              Mar 8 at 11:09












              up vote
              0
              down vote













              kill -SIGINT <pid>
              is the usual equivalent to ^C.






              share|improve this answer
























                up vote
                0
                down vote













                kill -SIGINT <pid>
                is the usual equivalent to ^C.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  kill -SIGINT <pid>
                  is the usual equivalent to ^C.






                  share|improve this answer












                  kill -SIGINT <pid>
                  is the usual equivalent to ^C.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 6 at 11:14









                  L29Ah

                  451112




                  451112




















                      up vote
                      0
                      down vote













                      use trapping:



                      #!/bin/bash

                      # this is a trap for ctrl + c
                      trap ctrl_c INT

                      function ctrl_c()

                      echo "Trap: CTRL+C received, exit"
                      exit






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        use trapping:



                        #!/bin/bash

                        # this is a trap for ctrl + c
                        trap ctrl_c INT

                        function ctrl_c()

                        echo "Trap: CTRL+C received, exit"
                        exit






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          use trapping:



                          #!/bin/bash

                          # this is a trap for ctrl + c
                          trap ctrl_c INT

                          function ctrl_c()

                          echo "Trap: CTRL+C received, exit"
                          exit






                          share|improve this answer












                          use trapping:



                          #!/bin/bash

                          # this is a trap for ctrl + c
                          trap ctrl_c INT

                          function ctrl_c()

                          echo "Trap: CTRL+C received, exit"
                          exit







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 6 at 11:22









                          karmax

                          1




                          1






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428469%2fstopping-a-script-process-with-a-control-c-or-something%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay