Find process id by command used by the process and kill it

5,713

This will give you the process id(s) of ffmpeg command:

pgrep -x ffmpeg

But you can directly kill it using pkill:

pkill -x ffmpeg

Specify the signal (default is SIGTERM), e.g.

pkill -x -9 ffmpeg

Why -x:
pgrep/pkill matches a pattern, so unless you add -x option (exact match), it will match also thisisnotffmpeg.

You might need -f to match the full command instead of the process name only, e.g. if you have multiple ffmpeg command running from which you only want to kill specific ones ending with blabla:

pkill -f '^ffmpeg.*blabla$'
Share:
5,713

Related videos on Youtube

Sambir
Author by

Sambir

Updated on September 18, 2022

Comments

  • Sambir
    Sambir over 1 year

    So sometimes ffmpeg hangs and i need to look into system monitor and the command used by it to find the specific process and kill it.

    Is there an easier way / bash script to just say ./scipt blabla where blabla is the part of the command used in ffmpeg -i ..... blabla and when it's found it should be killed or someting which returns the PID so i can manually just pkill the process id? instead of manually scrolling trough all the active ffmpeg processes and commands used by them.

    I got it working by:

    ps -Af | grep '/root/bin/ffmpeg.*blabla' | grep ? | awk '{print $2}' | xargs sudo kill -15
    

    But what if the command gives me 2 pids?

    • sudodus
      sudodus over 4 years
      If you are running only one ffmpeg command (each time), you can look for it with the following command line, ps -A | grep ffmpeg; Otherwise you can let grep look for something more specific, the 'blabla', for example a file name. The first field of the output is the PID of the process.
    • sudodus
      sudodus over 4 years
      ps -Af | grep 'ffmpeg.*blabla' and ignore the last hit which shows the grep command
    • Arkadiusz Drabczyk
      Arkadiusz Drabczyk over 4 years
      @sudodus: Or ps -Af | grep '[f]fmpeg.*blabla'
    • sudodus
      sudodus over 4 years
      You get the header of the ps list with the following command: ps -Af|head -n1
    • Sambir
      Sambir over 4 years
      I tried ps -A | grep ffmpeg and i gor the processid's but i also tried ps -Af | grep 'ffmpeg.*blabla' no result. the blabla part is part of a command line used by ffmpeg.
    • Sambir
      Sambir over 4 years
      ps -Af | grep '/root/bin/ffmpeg.*blabla' got me the procesid and whole command.
    • Sambir
      Sambir over 4 years
      but also another process from pts/6 which is not relivant I added | awk '{print $2}' behind and this ends up with only the PID but 1 pid is correct the other is from another process but is irrelivant. Seems to be the TTY. I need the TTY with ? So I also added grep ? and that seems to do the trick
  • sudodus
    sudodus over 4 years
    You might need -f option to each: pgrep -xf 'ffmpeg.*blabla'