Stephan Richter
Stephan Richter

Reputation: 1156

Non-blocking nohup in bash function?

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

Upvotes: 4

Views: 2088

Answers (1)

Toby Speight
Toby Speight

Reputation: 30791

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.

Upvotes: 4

Related Questions