27

When I issue

ps aux | grep mtp

I get

ubuntu-+ 15934 0.1 0.0 519848 7068 ? Sl 21:13 0:00 /usr/lib/gvfs/gvfsd-mtp --spawner :1.9 /org/gtk/gvfs/exec_spaw/20

So the PID in this case is 15934. But every new time this is run the PID is different. Is there any other way to kill a process other than by PID?

3
  • Different PID → different process.
    – phk
    Commented Dec 28, 2016 at 21:31
  • 4
    If the PID is constantly changing then the process is constantly exiting and a new process with the same name is starting. So you probably want to go after the parent process, the one that is starting the children. Look at ps -ely | awk 'NR==1||/mtp/' and look at the PPID column
    – icarus
    Commented Dec 28, 2016 at 22:00
  • 2
    A better approach would be to understand what is actually going on, and fix that instead. Find out why the MTP mount isn't working, by checking logs and so forth, and fix it so that it actually works. Find out why GVFS even attempts the MTP mount, and stop it from needing it. Find out where MTP automount is happening, and turn it off. Find out why GVFS is needed at all, and stop that being so. As opposed to killing processes willy-nilly without understanding, and breaking one's GNOME desktop applications as a consequence.
    – JdeBP
    Commented Dec 29, 2016 at 7:08

5 Answers 5

11

Probably there is a parent process which kills child processes and forks new children. You can use pstree to find the parent process:

pgrep mtp | xargs -i pstree -ps {}

Or alternatively you can use the ppid option of ps:

pgrep mtp | while read line; do ps -p $line -o ppid; done

Then you can kill the parent process

1
  • In my case the parent of the process is the login account, therefore killing the parent process won't do.
    – Konkret
    Commented Sep 10, 2020 at 22:58
4

If you wanted to kill a process which is constantly changing, you do so dynamically:

pkill name_of_process

or

kill -kill $(ps -A | grep name_of_process | awk '{print $1}')

Learn more about Kill here: https://www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/

Note: The searches performed by grep or sed are case-sensitive, and you'll have to make sure to narrow down a particular process to an unique identifier other than the PID before you can kill it using these examples.

If you wanted to find the parent of that process dynamically you'd do:

ps -o ppid=$(ps -A | grep name_of_process | awk '{print $1}')

From there you could kill the parent process, since you have its PPID.

If you cannot kill the parent process you'd have to resort to the 1st solution. The problem is, sometimes the PID of processes changes faster than you can find it. In that case you may want to use Sed instead of Grep. The 1st example becomes:

kill -kill $(ps -A | sed '/name_of_process/!d' | awk '{print $1}')

In order to speed things furthermore, you may want to hone the ps command returned results by using other flags other -A. Check out the ps man page:

https://www.man7.org/linux/man-pages/man1/ps.1.html

Sometimes, a process keeps on going by the help of a Daemon. See if you can kill that as well.

Two other things you may consider is the terminal associated with the process or the killall command.

https://linoxide.com/linux-command/linux-killall-my-options/

0

You can use pkill mtp, or kill -9 pidof mtp

Please notice that if there are several mtp processes, both these commands will kill all the mtp processes in the system.

Rami Rosen

1
  • 5
    This or killall mtp will kill mtp's, however, possibly there is another process which constantly forks new mtp processes as children. Without killing, this is of no use.
    – S.C
    Commented Dec 28, 2016 at 22:07
0

I issued

ps aux | grep mtp

which gave me a few mtp related processes and compared with the camera mounted and unmounted to get the specific process

and then

pkill -9 gvfsd-mtp

does the trick.

But I assume the above answer will work as well:)

1
  • this one does not works for me as the pid of the process changes while I launch the pkill after having launched the ps aux
    – Tms91
    Commented Apr 4, 2023 at 11:16
0

You can launch a pkill based on the name of the command which started the process (it is the last / 11th column of ps aux output)

sudo pkill -9 -f COMMAND_STARTING_THE_PROCESS*

And since in your case the command that started the process is

/usr/lib/gvfs/gvfsd-mtp --spawner :1.9 /org/gtk/gvfs/exec_spaw/20

enter image description here

you can kill all the processes associated with the launch of the script gvfsd-mtp with

sudo pkill -9 -f /usr/lib/gvfs/gvfsd-mtp*

Note: be sure of using sudo!

You must log in to answer this question.

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