manutd manutd - 6 months ago 32
Java Question

Java process executor in Mac OS

I am using the below code to close all the FireFox instances in a windows machine. Can this same code used in Mac machine? If not what will be the best approach?

Runtime.getRuntime().exec("taskkill /F /IM FireFox.exe");

Answer

Can this same code used in Mac machine?

Nope.

But this will work:

Runtime.getRuntime().exec("pgrep 'firefox' | xargs kill");

Explanation of pgrep 'firefox' | xargs kill

pgrep 'firefox' - find all process ids that match processes having firefox in their name.

| xargs kill - Pass all the process IDs found from pgrep as input to the kill command.