Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 254
Stephan Richter Avatar asked Sep 14 '25 07:09

Stephan Richter


1 Answers

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.

like image 117
Toby Speight Avatar answered Sep 17 '25 02:09

Toby Speight