Freebsd – Get CPU usage for single process

cpufreebsdprocess

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?

Best Answer

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.

Related Question