Thursday, May 2, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 466  / 2 Years ago, wed, march 16, 2022, 10:53:17

I have made a script that plays audio using ffplay directly onto the terminal. Now after it's executed I am unable to stop the sound without specifically killing it through the terminal or System Monitor.
What I am trying to do is to specify an argument, say audio.sh stop to kill the PID and stop the audio.
But I am unable to do so by simply


PID=$$
elif [[ $# == 1 ]] && [[ $1 == 'stop' ]]; then
kill ${PID}

I got the problem here, that after running this with the stop argument it will create another instance of the script and then kill it instantly.
So, is there some way to do that?
Here is my full code.


  1 folder=<folder path>
2
3 PID=$$
4
5 if [[ $# == 0 ]]; then
6 RANDOM=$$
7 for i in `seq 1`
8 do
9 R=$(($(($RANDOM%53))+1))
10 file=${folder}${R}.mp3
11 ffplay -nodisp -autoexit ${file} >/dev/null 2>&1 &
12 done
13 elif [[ $# == 1 ]]; then
14 file=${folder}${1}.mp3
15 ffplay -nodisp -autoexit ${file} >/dev/null 2>&1 &
16 elif [[ $# == 1 ]] && [[ $1 == 'stop' ]]; then
17 kill ${PID}
18 fi

More From » command-line

 Answers
1

To kill all instances of ffplay, replace:


kill ${PID}

with:


pkill ffplay

This will make your life much simpler.


[#431] Friday, March 18, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kroapathe

Total Points: 445
Total Questions: 117
Total Answers: 99

Location: Botswana
Member since Sun, Sep 19, 2021
3 Years ago
;