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.

2
  • 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

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
2
  • 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
0

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.

1
  • 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 at 17:13

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.