I'm running a process and I want to store its CPU usage.

So I used top command

{ test_case.sh > out.log ; } && { sleep 2s ; x=`pidof test_case` ; top -p $x -d 5 -b > cpu.log ; }

But the cpu.log file is empty and out.log contains the output of test_case

I don't know why.

Can you help me to get the output of the top command till that process completes

  • there is no pid for x.. test_case is finished once it writes to out.log.
    – WU-TANG
    Oct 27 '20 at 8:04
  • yes, so i had used && to run both commands parallely. I want to store top output while running the process and as the process completes top command must end Oct 27 '20 at 8:22
  • that's not what && does
    – WU-TANG
    Oct 30 '20 at 21:24
{ ./can.sh > canny.log & x=$(pidof -s /bin/bash ./can.sh) ; echo > cpu.log ; } && { while test $x == $(pidof -s /bin/bash ./can.sh) ; do top -p $x -bn 1 >> cpu.log ; sleep 5s ; done ; }

can.sh was my process.. The first line of can.sh is a sleep 1s, which is needed to ensured $x is established.

EDIT... after looking at this, this took your design and made it execute without error.... But it does not actually work for what you intended... Your test.sh(my can.sh) is a script executing commands... scripts don't use CPU... the commands they execute will use the CPU... so you won't see that reflected with this method. I will leave it there just in case you can figure out how to get top to give you the called commands CPU usage.

... pidstat would have been better... but still presents the same problem. I won't bother using a script for the example... just a command.

:~$ pidstat 1 -e dd if=/dev/zero of=/home/thecarebears/pinecone.txt bs=1024 count=1000000
Linux 5.4.0-51-generic ()   10/30/2020  _x86_64_    (2 CPU)

07:51:54 PM   UID       PID    %usr %system  %guest   %wait    %CPU   CPU  Command
07:51:55 PM  1000    237570    9.00   90.00    0.00    0.00   99.00     1  dd
07:51:56 PM  1000    237570    8.00   91.00    0.00    0.00   99.00     1  dd
07:51:57 PM  1000    237570    0.00   11.00    0.00    0.00   11.00     1  dd
1000000+0 records in
1000000+0 records out
1024000000 bytes (1.0 GB, 977 MiB) copied, 3.81999 s, 268 MB/s

Average:     1000    237570    5.67   64.00    0.00    0.00   69.67     -  dd

displays cpu statistics, in this case, every 1 second, until the specified command finishes... The command is dd (with parameters), but if i run dd inside can.sh, pidstat 1 -e can.sh will show 0% cpu, because dd is the command that is being executed by the script... I don't know the magic to make a script appear to the OS as a command(if it exists)...

So if you have a command that you want to monitor, instead of a script, both of these will work... If you cannot get around that, then you may have a little more figuring to do... unless someone else chimes in with some additional knowledge.

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.