7

I have got the following logging script:

#!/bin/bash
top -b -c -n 1 >> /var/log/toplog/top.log

And the following record in my crontab:

*/1 * * * *     /home/clime/scripts/toplog.sh

The problem is that lines in top.log are being cut to 80 chars, e.g.:

 1512 root      20   0 80756 1436  572 S  0.0  0.1   0:05.92 /usr/libexec/postfi

This does not happen if I run the command directly from console.

I have tried to use COLUMNS variable:

*/1 * * * *     COLUMNS=999 /home/clime/scripts/toplog.sh

But that leads to every line being long exactly 999 characters - unused space is padded with spaces, which is not what I want.

How to fix this strange issue? My system is centos 6.3.

  • I don't know if this is relevant but what's the point of /1? Does it work if you simply use * * * * *? */1 and * both mean run every minute. – terdon Oct 13 '13 at 18:08
  • Ye, /1 does not need to be there. I have changed it. The problem persists though. – clime Oct 13 '13 at 18:10
6

top always displays spaces until the last screen column. You just don't realize it when it's printing to the terminal because you can't visually distinguish a line with trailing spaces from a line without trailing space. You'll notice the spaces if you copy-paste with the mouse or in screen.

If you want to get rid of the spaces, just filter them away.

COLUMNS=9999 top -b -c -n 1 | sed 's/  *$//' >>/var/log/toplog/top.log

Whatever you're running top for, there are probably far better monitoring tools available.

  • I use this to keep information about processes that were running at any time during last five days. These are 1-min snapshots so something might get lost but I think it is good enough for me. I want to have some data available when I discover there was a peak in resource usage (I use munin for that). I haven't found a better way to keep track of past processes (tried psacct). – clime Oct 14 '13 at 17:30
  • Just saw this when running a check script via nrpe. The output of ps was truncated to 80 chars and a grep for the process name was failing. Setting COLUMNS resolved the issue. – phil-lavin Jun 19 '15 at 9:07
0

I had a similiar issue. Bash script worked fine from the command line but the 'top' output was chopped to 80 chars when run from cron.

I first tried the accepted solution here, by setting the COLUMNS environment variable. I could not get this to work (SLES 12.1). I even tried to write to a file, export, source custom profile.

What did work was using top's -w option.
Here is the guts of my script, minus the notification and killing safety stuff.

#!/usr/bin/sh

PROC_FOUND=""
MAX_CPU=0

RETVAL="$(top -bc -n 1 -w 512 | tail -n+8 | awk -v cpulimit=$MAX_CPU '{if ($9>=cpulimit) print $1 " " $9 " " $12; }')"

IFS='
'
set -f
for line in ${RETVAL}; do
{
        echo "found [$line]" > /dev/tty

        IFS=', '
        read -r -a array <<< "$line"
        #echo "array is ... ${array[*]}"
        PID_VAL=${array[0]}
        CPU_VAL=${array[1]}
        PROC_FOUND=${array[2]}

        echo "Monitored a High CPU for process [${PROC_FOUND} ${PID_VAL}] on "${HOSTNAME}" where ${CPU_VAL}% >= ${MAX_CPU}%."
}
done
set +f
unset IFS
exit

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.