5

I've got a problem getting a real-time CPU usage of a single process (by its PID). I'd like to setup a watchdog for the CPU usage of a process because it's suddenly reaching 100% of the CPU usage sometimes.

What I have tried:

top -p $PID, top -pid $PID - seems like those two are not working on FreeBSD

ps h -p $PID -o %cpu - works, but returned CPU usage percentage is always 0.

The question is - how can I get a real time CPU usage for a single process by its PID on FreeBSD?

4

I don't have access to a BSD machine to check but your ps command should work as advertised. In any case, as a dirty hack, you could always just parse the output of the full ps (where NNN is the PID you are after):

ps aux | awk  -v OFS="\t" '$2=="NNN"'

Or, to keep the output format identical to that of ps:

ps aux | grep -i '^[a-z ]*NNN '

You may have to tweak the ps options a bit since they're different in BSD. Just use whichever combination prints all processes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.