1

I am running the top command to see details about specific processes. The output is piped to grep like so:

top -n 1 | grep jre 

The output is usually around 4 lines, and I would like to prefix the current time to each line so it would be something like:

Before:

2772 deleteme 20 0 2832 1156 872 R 2.0 0.1 0:00.01 top 

After:

13:46 25-08-2012 2772 deleteme 20 0 2832 1156 872 R 2.0 0.1 0:00.01 top 

2 Answers 2

5

Look at the ts command from the moreutils package:

NAME ts - timestamp input SYNOPSIS ts [-r] [format] DESCRIPTION ts adds a timestamp to the beginning of each line of input. 

You can e.g. use it as such:

$ top -n 1 | grep init | ts aug 28 17:15:00 1 root 20 0 24448 2272 1340 S 0 0.1 0:01.07 init 
1
  • This is so simple! WOW! Why does TS add a empty line between entries? Any way to stop that? Commented Aug 29, 2012 at 8:27
1

The ps command is better suited for this kind of task. Try something like this:

$ ps -ao bsdstart,fuser,pid,%cpu,%mem,args | grep jre 

From the ps man page:

ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top(1) instead.

In the command I suggested, the option '-a' tells ps to print processes for all users. The -o specifies the output format. In my example (again from the ps man page):

bsdstart : time the command started. If the process was started less than 24 hours ago, the output format is " HH:MM", else it is " Mmm:SS" (where Mmm is the three letters of the month). See also lstart, start, start_time, and stime. fuser : filesystem access user ID. This will be the textual user ID, if it can be obtained and the field width permits, or a decimal representation otherwise. pid : a number representing the process ID (alias tgid). %cpu : cpu utilization of the process in "##.#" format. Currently, it is the CPU time used divided by the time the process has been running (cputime/realtime ratio), expressed as a percentage. It will not add up to 100% unless you are lucky. (alias pcpu). %mem : ratio of the process's resident set size to the physical memory on the machine, expressed as a percentage. (alias pmem). args : command with all its arguments as a string. Modifications to the arguments may be shown. The output in this column may contain spaces. A process marked <defunct> is partly dead, waiting to be fully destroyed by its parent. Sometimes the process args will be unavailable; when this happens, ps will instead print the executable name in brackets. (alias cmd, command). See also the comm format keyword, the -f option, and the c option. When specified last, this column will extend to the edge of the display. If ps can not determine display width, as when output is redirected (piped) into a file or another command, the output width is undefined (it may be 80, unlimited, determined by the TERM variable, and so on). The COLUMNS environment variable or --cols option may be used to exactly determine the width in this case. The w or -w option may be also be used to adjust width. 

You can change this to suit your needs. Have a look at man ps and search for "STANDARD FORMAT SPECIFIERS" (you can use vi-style search in man pages, hit "/" and enter your search pattern, "n" will move to the next match).

8
  • 1
    That's perfect! Anyway to include the headers? I realise grep stops this, but anyother way? Commented Aug 28, 2012 at 14:28
  • Actually, it doesn't work right. if I just ran the top & grep commands together (i.e. without you line) it seems to show more results.... Commented Aug 28, 2012 at 14:33
  • @neildeadman It will show more lines if you are not using the -n1 option. Commented Aug 28, 2012 at 14:38
  • but with the -n 1 and not your additional commands it shows 4 lines, but with your additional lines and the -n 1 it shows 1 line and sometimes 3 or 4. Commented Aug 28, 2012 at 14:41
  • @neildeadman Another problem is that the output of top is strange. It manipulates the terminal somehow. If you tell me what exactly you need to do (what information do you want from top's headers for example), I should be able to give you a working command using ps. Commented Aug 28, 2012 at 14:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.