I run a process that takes lot of time. Time is not a problem actually but I would like to know how much RAM memory it requires. Process is already running so /usr/bin/time is not an option. I found the pid of my process and run

watch -n 1 grep VmHWM /proc/3100/status

it works nice, I can leave it, do some other work, and just check later what was the value recorded with 1 seconds intervals. The problem starts if/when a process terminates (either finish job, or get killed because of OOM) because watch replace the last valid output with

grep: /proc/3100/status: No such file or directory

using option -e does not help. Is there a way to make watch not refresh output on non-zero exit? or show two most recent ones, so the previous zero exit can still be seen?

  • If you know what your process is called, you could incorporate pgrep into that. I'm not posting this as an answer as I don't have a Linux system to test properly on at the moment.
    – Kusalananda
    Dec 5 '19 at 12:54
  • @Kusalananda I used pgrep to find process id. I don't understand how pgrep would help me more, if process terminates then pgrep won't list it anyway, isn't?
    – jangorecki
    Dec 5 '19 at 13:23
  • Oh, I'm sorry. I misinterpreted what you were writing and though that you were watching a particular PID, and that the process later restarted with a new PID, and that you wanted to follow that instead.
    – Kusalananda
    Dec 5 '19 at 13:33
  • 1
    I would just use a shell while loop for this. while grep VmHWM /proc/3100/status; do sleep 1; done.
    – Mikel
    Dec 5 '19 at 13:35
  • @Mikel thanks, you suggestion does the job well. Yet I find it strange that watch -e cannot handle that in better way.
    – jangorecki
    Dec 5 '19 at 13:40

I would use a shell while loop for this.

while out=$(grep VmHWM /proc/$pid/status); do
  printf "\r$out     "
  sleep 1
printf "\n"

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.