3

I am working on a project in which I need mplayer to run several instances in order to create voice-over-music real-time mixing from two files. So, the music is constantly playing at 90% volume (represented as "M" in the next timeline) and at certain moments we lower the volume ("m") in order to place voice ("V") over the music, after which we'll return the music to 90% when the voice sample finishes.

TIMELINE (Minutes)

0===========1===========2===========3===========4===========...
_____________VVVVVVVV__________VVVVVVVVVVVVVV__________VVVVV...
MMMMMMMMMMMMMmmmmmmmmMMMMMMMMMMmmmmmmmmmmmmmmMMMMMMMMMMmmmmm...

Everything is working pretty fine so far. But, for some reason, many times the second mplayer instance finishes the voice-off playback but the process still remains active and won't ever end.

The answer provided right here: Kill all processes except the one running the script is not working for this case.

I have also tried the accepted answer on this question: https://unix.stackexchange.com/questions/50555/kill-many-instances-of-a-running-process-with-one-command but this will kill EVERY instance of mplayer and I need to keep the first one constantly playing music.

In this page http://www.oracleflash.com/20/How-to-kill-all-processes-with-one-command-in-Linux.html they reproduce a way to Kill all processes initiated by an application. Which is really useful for this case but this will also kill all the instances of mplayer.

Expected solution

I am saving the PID of the music player with:

mplayer -shuffle -playlist playlist.m3u </dev/null >/dev/null 2>&1 & echo $! > player.pid

With that PID number, I'd like to use the last given page's solution in order to kill all the processes EXCEPT the one saved in the player.pid file. So, placing cat player.pid in the middle of

kill -9 `ps -ef | grep oraxpo | grep -v grep | awk '{print $2}' 

may do the trick.

The problem is I don't know where to place it. Or if is it there a different/better way to do it.

Another idea is to kill the last processes one at once but stopping on the very first one.

Any ideas are appreciated.

Thanks in advance.

  • How about ... pgrep mplayer | grep -vf player.pid | xargs kill -9? – muru Nov 20 '16 at 16:06
  • That easy? Pfff... I need to learn that many things. It works. Thank you. – Geppettvs D'Constanzo Nov 20 '16 at 16:15
3

You can try:

pgrep mplayer | grep -vf player.pid | xargs kill -9

Notes:

  • pgrep is the tool for looking up process information. It even supports negating matches and using PID files, but unfortunately I couldn't figure out a way to selectively negate matches.
  • Since you have the file, just have grep read from the file with -f, and negate matches with -v
  • xargs converts input to arguments, so you don't need to use command subtitution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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