-1

I'd like to get current process CPU/memory usage% by process name/path and print it to the console. the command should output one number and not provide an ongoing data flow like 'ps'.

ps -p PID doesn't work as:

  1. I don't have the process number (I do have process path)
  2. It doesn't print the current measurement once to the console

So for example it should look something like:

$command -getCPU | grep procesPath
jww
  • 97,681
  • 90
  • 411
  • 885
PloniStacker
  • 574
  • 4
  • 23

1 Answers1

0

You actually do know the PID if you know the process path as it is formatted like this : /proc/<pid>.

You can calculate the CPU usage with this method. It involves several steps though.

sydpy
  • 35
  • 3
  • Thanks for your answer. I looked at what you have suggested and it gives me an average since the process came to life. I am looking for current CPU usage, not average. This data will be used to create CPU usage graphs in Grafana. – PloniStacker Sep 12 '19 at 09:32
  • 1
    You can continuously poll `/proc/uptime` and `/proc/*pid*/stat` to check how the different CPU time fields have changed since the last poll. You will then have stats on the period between the two polls instead of the average since the beginning of the process – sydpy Sep 12 '19 at 09:38