71

Is there an option for the Linux top command where I can filter processes by name and write the CPU usage of that process every second to a log file?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
BalaB
  • 3,687
  • 9
  • 36
  • 58

4 Answers4

117

top & pgrep

To filter the output of top by process name, you can use pgrep to get a list of PIDs by process name then pass them to the -p option of top.

For example:

top -p $(pgrep -d',' http)

Note: the -d',' option delimits the PIDs with commas, which is what is expected by the top -p. Note 2: top will return a failure message if there are no running processes that match the name you specify in pgrep.

To write the results of top to a file, use the -n 1 option (only one iteration) and redirect the output to your log file.

top -p $(pgrep -d',' http) -n 1 >> your_log_file

To do that every second, perhaps a while loop with a sleep would do?

while :; do top -p $(pgrep -d',' http) -n 1 >> your_log_file; sleep 1; done

To timestamp each entry, you can append the output of date. E.g.

while :; do top -p $(pgrep -d',' http) -n 1 >> log.txt; date >> log.txt; sleep 1; done
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
  • Thanks Shawn, how to log date & time for each 1 second., So that it would help me isolate at what time and how much CPU was used by specific process. – BalaB Jan 03 '12 at 10:24
  • @melihcelik which task are you referring to? – Shawn Chin Jul 03 '12 at 14:41
  • I thought all of them would print with similar format, sorry. I was referring to this command: top -p $(pgrep -d',' http). I tried to read the output in Windows, didn't work. Tried to read it with more (less) on linux, didn't work either. I can only open it with vi. – melihcelik Jul 04 '12 at 11:46
  • The command "`top -p $(pgrep -d',' http)`" prints its output to stdout so you should not need any additional command to read the output. If there are no output, that just means there isn't a process that contains `http`. The following commands redirect that output to a file (note the "`>> filename`" statement) in which case you will need to read the file. – Shawn Chin Jul 04 '12 at 12:17
  • Excellent! I added also the "-b" option to avoid useless output confusing the file – lucaferrario Dec 04 '14 at 16:32
  • I am getting `-p requires argument` – adrianTNT Mar 04 '17 at 14:22
  • 1
    **Invalid argument "-p".** – IgorGanapolsky Sep 07 '18 at 18:58
  • I too get a `Illegal variable name.` on FreeBSD 11.2. – Basil Bourque Dec 09 '18 at 23:57
5

Another option is:

top -b -d 1 -p $(pgrep -d',' java) -n 120 > log.txt
  • The option -d allows to set the frequency used by top to refresh the data.
  • The option -b means that the traditional interface of top is not used. Instead, it sends everything to the standard output and then you can use a pipe (|) or a redirection (>).
  • The option -n informs about the number of iterations top should execute.

After that you can type:

cat log.txt | grep USER_OF_PROCESS

You will see the execution time of the process and also %CPU, Memory and all that.

2
#You can run following script as ./cpurecorder.sh pid filename
#It will generate output file with memory usage and cpu utilisation.
#You can log other variable by searching man for ps.

`enter code here`filepath=/home/rtcsadm              # modify as desired
interval=20                         # reports per minute
timelimit=6000                      # how long to run, in seconds

mydate=`date "+%H:%M:%S"`           # the timestamp
freq=$((60/$interval))              # for sleep function

while [ "$SECONDS" -le "$timelimit" ] ; do
  ps -p$1 -opid -opcpu -opmem -ocomm -c | grep $1 | sed "s/^/$mydate /" >> $filepath/$2.txt
  sleep 3
  mydate=`date "+%H:%M:%S"`
done
0

By using the linux command "top"...

Then press the 'o' or 'O' to activate filter prompt. It will show a line indicating the filter format like this -

        add filter #1 (ignoring case) as: [!]FLD?VAL

Then enter a filter like this and hit Enter.

               COMMAND=<pattern>

Now top will show only those processes whose COMMAND field contains the "<pattern>" value

(Solution taken from ... https://techantidote.com/filter-top-using-process-name-in-linux/ )