Kill Process When Pid Is Constantly Changing [Duplicate]

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

You can use ps to find the PID of the process, then pass that to kill:

kill $(ps -C /usr/lib/gvfs/gvfsd-mtp -o pid=)

The -C flag specifies a command name to search for in the list of processes, and the -o pid= option means ps will print only the PID. The result is passed as the only argument to kill.

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 the supplied answer above I assume will also work!

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest