lucaba
lucaba

Reputation: 175

Fast way to run shell commands

I'm working on a Java program that should repeatedly kill two processes as soon as possible, as they keep restarting when killed. Of course it shouldn't use too much CPU Time. I've written pretty much the same Program in python and it works well using some 5% of CPU.
I started using Runtime.exec with "ps -ax" to list all running processes and then search the string for the unwanted processes' name and then run exec with "killall -9" + process_name to kill it. This was just the first way i could think of, any ideas are appreciated.
But when i executed the code my computer kept running hot and after some timing tests i found out the exec command takes some 15 to 25 ms. Now why does it take that long and is there a faster way to do the job?
Btw i use mac os and don't care if the solution isn't platform-independant.

Upvotes: 0

Views: 482

Answers (1)

dj_segfault
dj_segfault

Reputation: 12449

I agree that Java is definitely not the right tool for this job. I also agree with others that this seems like a pretty silly thing to do in the first place. If it were me I would focus on getting the daemons to not run in the first place.

I would just write a bash script, which has all the functionality you need, and would save some of the process creation time. You could feed the output of ps into grep to get just the processes you need to eliminate, then pass that to kill.

You can make it even cheaper to run if you use ps parameters to select the right processes by name or user or group.

Upvotes: 1

Related Questions