Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Bash: check if program is running from the current script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Dominique_71
Veteran
Veteran


Joined: 17 Aug 2005
Posts: 1709
Location: Switzerland (Romandie)

PostPosted: Sat Jun 30, 2012 6:28 pm    Post subject: Bash: check if program is running from the current script Reply with quote

I am writing a script in order to automatize my work with mplayer.

I run mplayer in slave mode, and I want to be able to send command to it. Ideally, for this, I need to check if mplayer is already running in slave mode from this script, if not start it and send the command, if it is running, only start the command.

The relevant parts are:
Code:
## The real user id
USERID=`id -un`
## Command file used to send commands to mplayer
MPLAYER_INPUT="/home/$USERID/.mplayer/pipe"
## Start mplayer in slave mode
MPLAYER_EXEC="mplayer -quiet -fs -fixed-vo -slave -idle -input file=$MPLAYER_INPUT"

## launch mplayer
launch_mplayer()
{
if ! jobs |grep mplayer 1> /dev/null
then
   echo "start it"
   $MPLAYER_EXEC &
fi
}

##send a command to the running mplayer
send_mplayer_command()
{
echo "$1" > $MPLAYER_INPUT
}

## command to play a DVD
DVD_PLAY="loadfile dvd://"

## Play a dvd
play_dvd()
{
launch_mplayer
send_mplayer_command "$DVD_PLAY"
}


For DVD play to work, dvd-device must by set in ~/.mplayer/config.

The issue I get is than, each time I call the play_dvd function, it start a new mplayer instance before to send the DVD_PLAY command. And I can see in the terminal the echo "start it". What can I do to fix it?

The silly part is when I launch "$MPLAYER_EXEC &" from a terminal, and then issue the same if then loop, it work fine.
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3792
Location: Hamburg

PostPosted: Sat Jun 30, 2012 6:53 pm    Post subject: Reply with quote

Ick
Code:
if ! jobs |grep mplayer 1> /dev/null
why not (untested !)
Code:

jobs | grep -q mplayer
if [[ $? -eq 1 ]]; then
    echo "start it"
    $MPLAYER_EXEC &
fi
Back to top
View user's profile Send private message
Dominique_71
Veteran
Veteran


Joined: 17 Aug 2005
Posts: 1709
Location: Switzerland (Romandie)

PostPosted: Sat Jun 30, 2012 8:13 pm    Post subject: Reply with quote

Thank you for the answer. I find the solution, it was under my eyes, an horrible mistake, jobs doesn't return the command line but
Code:
MPlayer 1.1-4.5.3 (C) 2000-2012 MPlayer Team


So, adding -i to grep solve this issue.

EDIT: I am wrong again. To put MPLAYER was the right thing to do. The real output of jobs when mplayer is running is
Code:
[1]+  En cours d'exécution   $MPLAYER_EXEC &
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Sun Jul 01, 2012 1:29 am    Post subject: Reply with quote

Dominique_71 ...

If mplayer is creating the fifo/pipe in ~/.mplayer/ when in slave mode wouldn't it be simpler checking for the presence of the pipe?

Code:
if [[ ! -p $HOME/.mplayer/pipe ]]; then
   $MPLAYER_EXEC &
fi

best ... khay
Back to top
View user's profile Send private message
Dominique_71
Veteran
Veteran


Joined: 17 Aug 2005
Posts: 1709
Location: Switzerland (Romandie)

PostPosted: Sun Jul 01, 2012 11:19 am    Post subject: Reply with quote

khayyam wrote:
Dominique_71 ...

If mplayer is creating the fifo/pipe in ~/.mplayer/ when in slave mode wouldn't it be simpler checking for the presence of the pipe?

Code:
if [[ ! -p $HOME/.mplayer/pipe ]]; then
   $MPLAYER_EXEC &
fi

best ... khay


No, the pipe is not created by mplayer, it have to be created explicitly with mkfifo.
http://www.mplayerhq.hu/DOCS/tech/slave.txt

I can send command via the pipe in order to get some answer, but if mplayer is not running, the script will wait the answer forever. OK, I can add & at the end of the command, but even so, I don't know any simple way to get the answer back into the script (when you send a command to mplayer via the pipe, mplayer output the answer on its stdout).
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Sun Jul 01, 2012 12:37 pm    Post subject: Reply with quote

Dominique_71 wrote:
No, the pipe is not created by mplayer, it have to be created explicitly with mkfifo.

Dominique_71 ... I see, in which case this is a half implimented feature, the fifo should be created by mplayer if the -slave option is provided, and removed when the processes exists.

Dominique_71 wrote:
I can send command via the pipe in order to get some answer, but if mplayer is not running, the script will wait the answer forever. OK, I can add & at the end of the command, but even so, I don't know any simple way to get the answer back into the script (when you send a command to mplayer via the pipe, mplayer output the answer on its stdout).

This is because a fifo must be opened on both ends, if its not then a 'read' will be much like trying to suck milkshake through a straw with a knot tied in it.

best ... khay
Back to top
View user's profile Send private message
truc
Advocate
Advocate


Joined: 25 Jul 2005
Posts: 3199

PostPosted: Sun Jul 01, 2012 4:47 pm    Post subject: Reply with quote

Code:
launch_mplayer()
{
if ! jobs |grep mplayer 1> /dev/null
then
   echo "start it"
   $MPLAYER_EXEC &
fi
}

I'd replace this bloc with this one which looks cleaner ([/code]but is not tested!)
Code:
# at the beginning of your script somewhere,
slave_mplayer_pid=

# ...

launch_mplayer()
{
   if ! pgrep -P $$ mplayer | grep -qx "$slave_mplayer_pid"; then
      $MPLAYER_EXEC &
      slave_mplayer_pid=$!
   fi
}

_________________
The End of the Internet!
Back to top
View user's profile Send private message
Dominique_71
Veteran
Veteran


Joined: 17 Aug 2005
Posts: 1709
Location: Switzerland (Romandie)

PostPosted: Mon Jul 02, 2012 4:16 pm    Post subject: Reply with quote

Thanks, I will try it.

If I understand right, $! will get the pid of the last command executed in the background.

Do you know where I can find the reference for this syntax?
I just searched man bash and the Advanced Bash Scripting Guide, but I find no reference for $!.

Forget it, I found it (firefox is not refreshing automaticaly its search box): http://tldp.org/LDP/abs/html/internalvariables.html
Back to top
View user's profile Send private message
Dominique_71
Veteran
Veteran


Joined: 17 Aug 2005
Posts: 1709
Location: Switzerland (Romandie)

PostPosted: Mon Jul 02, 2012 9:16 pm    Post subject: Reply with quote

It seem to work very well. This is a very interesting approach because to save the PID is a much more flexible.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum