Debian – Kill second instance of a process

debianprocessraspbianUbuntu

I'm running two instances of omxiv (Omx Image Viewer). The first instance acts like a background image, and the second instance acts as a slideshow. My question is how can I specifically kill the second instance?

Small note: The second instance will always be opened after the first instance.

I was thinking about killing it by pid, but I am not sure if it will have the same pid after the reboot, therefore this might not work.

Right now I am calling:

pkill -9 omxiv

which is terminating both instances.

Best Answer

pkill has a -n flag that will make it only affect the most recently started ("newest") matching process.

pkill -n omxiv

If the omxiv process is well behaved, there is no need to use -9.

There is also a -o flag that will make pkill send a signal to the oldest matching process.

Note that using pkill -n will always kill the newest instance. If you have three processes, it will kill the third, not the second (as per title of question).

Related Question