4

I'm almost sure there is an easy solution to my following problem:

i have a bash script, let's say blob.bash:

#!/bin/bash
function player_test(){
    if [ "$(pidof someplayer)" ]; then
      # do some stuff
      exit 0
    else
      nohup someplayer &
      exit 1
    fi
}

if $(player_test); then
  echo Message A
else
  echo Message B
fi

if the player is running, the method returns and I get Message A. Good. If it's not running, it is started. However, the condition only returns after the player has quit and Message B is delayed.

Background: I'm writing a script, that continously feeds tracks into the playlist of an audio program. in the corresponding function, the player is started with nohup, when it is not runnung already.

Best wishes...

1
  • Use return instead of exit in player_test, and don't call it in a command substitution. Use if player_test; then instead.
    – chepner
    Commented Dec 4, 2015 at 12:37

1 Answer 1

4

The nohup command still has your stdout and stderr open. Redirect to /dev/null like this:

nohup someplayer &>/dev/null &

and your function returns immediately.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.