MacOS – How to use an application name to kill it from a script

javamacosterminal

I'm trying to create a cron job to prevent children at a school from running the Minecraft server. (We actually need them to run the Minecraft client because we use it to teach programming.) The name of the PROCESS is always "java". That's not useful for me.

I found a way using jps and lsof of getting the filesize of all running .jar files. I could kill processes based on that, but it seems messy.

The name of the Application (not the process) that runs the Minecraft server (as show by the Apple logo on the top left corner of the screen) AND its Java class name are both net.minecraft.server.MinecraftServer but I can't ever get that string from the command line, no matter how many variations of ps I try. ps and top always call the process "java". It's easy to find the file name being used, but that's useless because it's easily modified.

Is there a good, clean way to find if the Application or Java class net.minecraft.server.MinecraftServer is running and what its process is?

Best Answer

Minecraft Server should be detectable through the arguments passed to its java process.

Over at Unix & Linux, the question kill process based on arguments tackles this problem: how to kill a specific java process based on its command line arguments.

The top answer recommends using pkill with the -f flag to kill based on the arguments associated with a specific process. The final command may be as simple as:

pkill -f 'MinecraftServer'

With regard to using cron, this is fine but Apple recommend migrating to launchd as cron is less efficient.