0

I wrote this shell script named example.sh on Linux.

#!/bin/sh

TOPLOG=/tmp/top.log
date +%H:%M:%S >> ${TOPLOG}
top -b -c -n 1 |head -n 8 |tail -n 2 >> ${TOPLOG}
echo >> ${TOPLOG}

When I run it with the command "sh /tmp/example.sh", texts in top.log is like

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                
17980 cybereas  20   0 1478m  73m  18m S  2.0  0.5   1360:59 /opt/cybereason/sensor/bin/cybereason-sensor

However, when I run this script with this crontab

*/1 * * * * sh /tmp/example.sh

texts in top.log is shown like

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                
17980 cybereas  20   0 1478m  73m  18m S  2.0  0.5   1360:59 /opt/cybereason/sen

The output of "COMMAND" is omitted when it is a little long.

Is there any solution for this? I appretiate for any idea. Thank you.

leaN5525
  • 11
  • 2

1 Answers1

0

When not printing to a regular file, and not a terminal (>> "$TOPLOG"), top is truncating output to the standard terminal width of 80 columns.

If your top has the -w switch, add -w 512 for a larger maximum width.

dan
  • 4,846
  • 6
  • 15
  • Thank you for your advice. Unfortunately, my `top` doesn't have `-w` option, but I used `COLUMNS=150 top -c` instead. – leaN5525 Feb 24 '23 at 01:58