LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Multitasking in shell script. (https://www.linuxquestions.org/questions/programming-9/multitasking-in-shell-script-4175427749/)

kauuttt 09-17-2012 10:08 PM

Multitasking in shell script.
 
Hi,

My motive is to measure time taken for delayed audio playback through shell script..

The steps which I thought are as below:
1)
a) The user will press Enter to start the playback.
b) A Timer should be started at this point to measure the delayed audio playback.
c) The actual playback will be done through the "aplay" tool through the script. "aplay" is blocking call, I mean until the audio is completely played, the contol will remain on it.

2) Whenever the user will hear the actual sound, he will press "Enter" to stop the timer.

Now the problem is how can I do these multi tasks (playback and user interaction for time measurement) at the same time.

Please suggest.

suicidaleggroll 09-17-2012 10:22 PM

Launch "aplay" in the background with &

kauuttt 09-18-2012 05:39 AM

Thanks,
I thought of it but there seems to be some issues due to which I cant proceed with it.

If I run aplay in the background (suppose for a audio file of length 5 mins.), then in the foreground (the user interaction) the work will get over sooner and the control will come out from my function. But the audio is still getting played in the background..

I want the user interaction to happen in parallel, but the function should not bail out until all of my work (including playing of the full audio) are finished.

Any suggestions? Is it possible in shell script?

NevemTeve 09-18-2012 06:29 AM

try 'wait' command

firstfire 09-18-2012 06:48 AM

Hi.

First, obtain process ID (pid) of aplay using $! variable, then use kill command to send any signal to the process (SIGTERM for example) to terminate it:
Code:

file=./Noise.wav

aplay $file &
pid=$!
read ok
kill $pid

I assume here that you want to stop playback prematurely. Previously suggested command, wait $pid, will wait for aplay to complete (so you will hear whole song to the end).

kauuttt 09-23-2012 06:45 AM

Thanks firstfire and NevenTeve.
I will check the same and will update in this thread for future reference.


All times are GMT -5. The time now is 11:19 AM.