skip to Main Content

So I’ve been trying to run the following command to check the consumption by the firefox process.

top -b | grep "firefox"

Now I can see the desired output in terminal for it. But now when I try to export it to a file, I’m not able to.
The command I’m running is:

top -b | grep "firefox" >> filename or top -b | grep "firefox" > filename

Please help.

2

Answers


  1. You need the -n parm for top. For example,

    top -b -n 1 | grep "firefox" >> firefox.out
    

    Without -n 1 top will keep processing and will never make it to grep..
    From the man page for top:

       -n  :Number-of-iterations limit as:  -n number
            Specifies  the  maximum  number of iterations, or frames, top
            should produce before ending.
    

    Updated code with a while loop. It will loop forever unless you use
    something like cntr variable. Remove the cntr code if you want
    continuous monitoring:

    #!/bin/sh
    #
    not_done=1
    cntr=0
    # Look for process firefox every 1 seconds
    while [ "$not_done" -eq 1 ]
    do
      top -b -n 1 | grep "firefox" >> /tmp/firefox.out
      sleep 1
      ((cntr=cntr+1))
      # Addition logic to break out of loop after 10 iterations
      if [ "$cntr" -eq 10 ]
      then
        not_done=0
      fi
      echo "cntr=$cntr"
    done
    
    Login or Signup to reply.
  2. You have to add in the command the flag -n
    Change the number of processes to show. You will be prompted to enter the number.

    To take a snapshot of a specific process in top utility, execute command with the PID (-p) flag.

    Also i recommed if you want to take snapshots for the process one (PID), and get taking three snapshots of the PID:
    top -p 678 -b -n3 > monitoring.out

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search