Find the answer to your Linux question:
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.
  1. #1
    Just 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

    Code:
    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
      ;;
    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 written

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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

  3. #3
    Just 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.....

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by fzwd View Post
    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.....
    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.

  5. #5
    Just Joined!
    Join Date
    Jul 2012
    Posts
    10
    Quote Originally Posted by atreyu View Post
    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.
    Code:
    ( wget  -O $MEDIAFILE -o $WGETLOG $URI 
        if [ $? ] ; then echo "ERROR WGET"; kill $$;  fi ) &
    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....

    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.

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by fzwd View Post
    Code:
    ( wget  -O $MEDIAFILE -o $WGETLOG $URI 
        if [ $? ] ; then echo "ERROR WGET"; kill $$;  fi ) &
    I had found that kills the script but your idea to search wget-log is better.
    that is because $$ is the pid of the current process. you'd want $!, which is the pid of the last executed process.

    tried another site it retrieve an html page instead....
    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?

    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.
    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.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •