LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2012, 10:08 PM   #1
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
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.
 
Old 09-17-2012, 10:22 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Launch "aplay" in the background with &
 
Old 09-18-2012, 05:39 AM   #3
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Original Poster
Rep: Reputation: 26
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?
 
Old 09-18-2012, 06:29 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,852
Blog Entries: 1

Rep: Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868Reputation: 1868
try 'wait' command
 
1 members found this post helpful.
Old 09-18-2012, 06:48 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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).

Last edited by firstfire; 09-18-2012 at 06:50 AM. Reason: Typos.
 
2 members found this post helpful.
Old 09-23-2012, 06:45 AM   #6
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Original Poster
Rep: Reputation: 26
Thanks firstfire and NevenTeve.
I will check the same and will update in this thread for future reference.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"cooperative multitasking" in user space versus "preemptive multitasking" in kernel? bayoulinux Programming 2 08-06-2012 05:44 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration