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)


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.

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

2 Answers 2


You can try:

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


  • 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
  • Seems like you might be able to use pkill and save the extra process. Sep 21, 2021 at 0:42
  • Seems so, but pkill and pgrep are support the same features, and as I said, I couldn't figure out a way to have a single invocation do both matching on the command name and inverted matching on the process ID.
    – muru
    Sep 21, 2021 at 7:22

Easiest Tip

Just open htop and go to search and then search mplayer and then just click next and click on kill and its done. As simple as that.

Htop install command

  • Fedora/Red Hat Enterprise
    dnf install htop
  • Ubuntu/Debain
    sudo apt install htop
  • Arch
    sudo pacman -S htop

Hope it works for you.
Give me a upvote if it does.

  • Thank you for the easiest tip. I know I didn't mention I need this for an automatic process but in the "Expected solution" you can see the details. Your "Easiest Tip" cannot be reproduced from a CRONJOB or a SCRIPT without the usage of something to manage things on screen. Thanks anyway, this issue was solved on 2016 with the solution provided by muru and have been working pretty fine since then. Best regards. Mar 2, 2023 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .