Results 1 to 6 of 6
Was having problems getting to play mp3 off the web right so thought to make a shell program do that........ here's part of it
Code:
function download ()
{
WGETLOG="playmp3.wget-log"
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 07-19-2012 #1Just Joined!
- Join Date
- Jul 2012
- Posts
- 10
terminanting bash script from background job
Was having problems getting to play mp3 off the web right so thought to make a shell program do that........ here's part of it
so my problem here is if wget doesn't connect how to stop it from try to play the file.... as this doesn't work as writtenCode:function download () { WGETLOG="playmp3.wget-log" MEDIAFILE=~/Download/`echo $URI | sed 's:^.*/:: ; s:[^a-zA-Z0-9\.]+:-:g'` #run wget in background ( wget -O $MEDIAFILE -o $WGETLOG $URI if [ $? ] ; then echo "ERROR: wget ... $URI" ; exit 1; fi ) & } #................................. case $1 in *m3u) URI=`cat $1` download #wait for something to be buffered sleep 20 #convert to wav whatever is downloaded so far & play play ;;
- 07-20-2012 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,664
well, there are things i'd do differently, but here is a quick fix you can try:
Code:#!/bin/bash function download() { WGETLOG="playmp3.wget-log" MEDIAFILE=~/Download/`echo $URI | sed 's:^.*/:: ; s:[^a-zA-Z0-9\.]+:-:g'` #run wget in background wget --background -O $MEDIAFILE -o $WGETLOG $URI # it is good practice to return an exit value from functions, # instead of simply exiting return $? } #................................. case $1 in *m3u) URI=`cat $1` # now when we call this function, exit if it did not return successfully download rc=$? if [ $rc -ne 0 ]; then echo "wget returned exit code $rc -aborting" exit $rc fi #wait for something to be buffered sleep 20 #convert to wav whatever is downloaded so far & play play ;; esac
- 07-20-2012 #3Just Joined!
- Join Date
- Jul 2012
- Posts
- 10
thanks for the reply atreyu.
As I understand $? that only works on foreground tasks. And I had seen the --background switch but the problem is wget didn't show up jobs as I do kill `jobs -p` to stop all the background tasks at the end of the script.....
- 07-20-2012 #4Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,664
okay, so maybe you should explain what kind errors you are expecting from wget. there may be better ways to determine if there is a problem with it (like examining the wget-log, for one).
also, what process(es) are you trying to kill? wget? why, if I may ask? you can use pidof to get the pid of wget (or whatever) and kill it that way. or you can get the pid of the last executed command using the $! variable, and use that.
- 07-22-2012 #5Just Joined!
- Join Date
- Jul 2012
- Posts
- 10
I had found that kills the script but your idea to search wget-log is better. tried another site it retrieve an html page instead....Code:( wget -O $MEDIAFILE -o $WGETLOG $URI if [ $? ] ; then echo "ERROR WGET"; kill $$; fi ) &
the other running processes would( wget, lame, aplay ) basically the Im listening the song just enough to see if its something I could do something with. Totem I guess would have been rebuilt, since I cant adjust the buffer, and wouldnt allow replaying without restarting the streaming. and also if was something I could do something with it would already downloaded.
- 07-22-2012 #6Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,664
that is because $$ is the pid of the current process. you'd want $!, which is the pid of the last executed process.
what do you mean? that sounds like you had an incorrect URL...or maybe you just have to parse the log and look for a specific error?tried another site it retrieve an html page instead....
okay, now that I understand better, this is what I'd recommend: create a separate script that does the converting/playing, and have your URL downloading script call it.the other running processes would( wget, lame, aplay ) basically the Im listening the song just enough to see if its something I could do something with. Totem I guess would have been rebuilt, since I cant adjust the buffer, and wouldnt allow replaying without restarting the streaming. and also if was something I could do something with it would already downloaded.
what is your main problem, though: is it that you want to kill wget in mid-download, after you've played the partially downloaded file? or is it that if wget has a problem, correctly detect that problem and quit the d/l script? or something else?Last edited by atreyu; 07-22-2012 at 03:55 PM. Reason: removed bogus quote tag


Reply With Quote

