Stopping a script process with a Control + C or something

Clash 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.
bash shell-script scripting audio process-management
add a comment |Â
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.
bash shell-script scripting audio process-management
add a comment |Â
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.
bash shell-script scripting audio process-management
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.
bash shell-script scripting audio process-management
edited Mar 6 at 12:14
Stephen Kitt
141k22307367
141k22307367
asked Mar 6 at 11:09
Reckless Liberty
84
84
add a comment |Â
add a comment |Â
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.
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%dbecause I'm assuming that$sox_pidis 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%sfor 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$!. Theprintfis solely for the user, in case they would want to kill the job manually withkillso that they know what PID to kill (it's not strictly needed).
â Kusalananda
Mar 8 at 11:09
add a comment |Â
up vote
0
down vote
kill -SIGINT <pid>
is the usual equivalent to ^C.
add a comment |Â
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
add a comment |Â
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.
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%dbecause I'm assuming that$sox_pidis 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%sfor 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$!. Theprintfis solely for the user, in case they would want to kill the job manually withkillso that they know what PID to kill (it's not strictly needed).
â Kusalananda
Mar 8 at 11:09
add a comment |Â
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.
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%dbecause I'm assuming that$sox_pidis 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%sfor 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$!. Theprintfis solely for the user, in case they would want to kill the job manually withkillso that they know what PID to kill (it's not strictly needed).
â Kusalananda
Mar 8 at 11:09
add a comment |Â
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.
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.
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%dbecause I'm assuming that$sox_pidis 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%sfor 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$!. Theprintfis solely for the user, in case they would want to kill the job manually withkillso that they know what PID to kill (it's not strictly needed).
â Kusalananda
Mar 8 at 11:09
add a comment |Â
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%dbecause I'm assuming that$sox_pidis 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%sfor 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$!. Theprintfis solely for the user, in case they would want to kill the job manually withkillso 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
add a comment |Â
up vote
0
down vote
kill -SIGINT <pid>
is the usual equivalent to ^C.
add a comment |Â
up vote
0
down vote
kill -SIGINT <pid>
is the usual equivalent to ^C.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
kill -SIGINT <pid>
is the usual equivalent to ^C.
kill -SIGINT <pid>
is the usual equivalent to ^C.
answered Mar 6 at 11:14
L29Ah
451112
451112
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Mar 6 at 11:22
karmax
1
1
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password