The top command outputs a kind of live view of processes and their use of resources in a system.

I'd like to start a program on a server installation, which starts a variety of different processes, with non-predictable PIDs, and capture the changing output of top, without having to rely on an ssh connection from a desktop environment. Preferably I'd like to be able to 'replay' this in the same CLI environment, as if it were a video, but could acceptably move the file elsewhere to view it like this.

Is there a way, using command line only, from a direct terminal on a server installation, that I can record this output for a defined time period? Either by entering a value for time, or by manually starting and stopping the recording process.

  • You can script a ps command which can give the same info of top and save the output to a file... or am I misreading your question? – Rmano Dec 2 '15 at 16:28
  • @Rmano I was envisaging a sort of video like representation of it, but saving output to a file with associated time information seems a useful way to view the data. – Arronical Dec 2 '15 at 17:19
  • @Arronical: TREE's answer should accomplish exactly what you want... – psmears Dec 2 '15 at 21:23
  • Related (but different) problem: Stop reading process output in Python without hang? – jfs Dec 2 '15 at 21:55
  • ttyrec -e top – Doorknob Dec 3 '15 at 3:09

You can use the 'script' and 'scriptreplay' utilities to accomplish this.

  1. run script with the --timing option:

    script --timing=top.timing topscript

  2. execute the top command, let it run as long as you want

  3. exit top using q or control-c.

  4. exit the script session with control-D (EOF) or by typing exit

  5. run scriptreplay to replay the script:

    scriptreplay top.timing topscript

| improve this answer | |
  • This is great, thanks! I did find that for some reason the timing file came out as =top.timing using your example, if I renamed it and tried to use it as input to scriptreplay, nothing happened. – Arronical Dec 3 '15 at 9:37
  • I have re-tested with the provided commands, and they work for me. Make sure you include both dashes before timing in the first command. – TREE Dec 3 '15 at 17:46
  • I'd been using -t instead of --timing. With --timing there's no preceeding '='. – Arronical Dec 4 '15 at 9:51

The regular mode of operation of top is not suitable for saving the output in a file.

You can use the -b (batch) mode of top along with appropriate time interval for checking (-d) and the number of iterations for count (-n).

You can also mention specific PIDs (-p) to check only.

For example:

top -b -d 5 -n 3 -p 1089,2345 >output.txt

will run top in batch mode, will do 3 iterations after 5 seconds each, and will only monitor two mentioned PIDs (1089,2345). The output will be saved in output.txt.

Change the values to fit your need.

| improve this answer | |
  • I was hoping to see a video-like representation, but I suppose I could script a loop which saves batch mode output of top into separate files. Then create a viewing loop, which displays the each files contents separated by the same time increment, to recreate the effect of the regular operating mode of top. Thanks for pointing my head in the right direction :) – Arronical Dec 2 '15 at 17:25
  • @Arronical Yeah if that is your plan.. – heemayl Dec 2 '15 at 17:31

This is not exactly the same as top, but you can use this script:

while true; do
echo $(date)
ps -eo pid,user,pri,ni,vsize,rss,stat,pcpu,pmem,time,wchan --sort=-pcpu | head -20
sleep 5 
done  > ps.log

You will have more or less the top info in the file ps.log. For details about the ps command, see man ps.

| improve this answer | |

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.