0

I just created a script to output htop automatically every n minutes using this method → https://askubuntu.com/a/726661/253251, I put the script on crontab so it can be executed daily. But the output produced by cron was incomplete.

enter image description here

However, if I execute the command manually, there will be no truncated text.

enter image description here

Is there any settings that I can tweak to achieve full output in cron?

UPDATE

I have already tried Waltinator suggestion, it doesn't work and it stretch the output so long which is not what I want.

enter image description here Screenshot

How to make output similar to the one executed manually ?

Liso
  • 15,677

1 Answers1

1

It seems to me that you just have to pick the "right" value for COLUMNS. In a way, your manual method did that as well since you widened the window to some suitable value before you ran htop.

I don't think there is a "magical way" to do it, unless you ran ps or some similar command, find out the length of the longest command being run, and then set that to the value of COLUMNS (plus some more columns for everything that appears to the left of it), and then run htop.

I guess this could be achieved with this code:

ps -aef | awk -v OFS='\t' '{ $1=$1; print }' | cut -f 8 | awk 'length > max_length { max_length = length; longest_line = $0 } END { print longest_line }' | wc -c

Credit for this goes to these two pages: 1 and 2, which I just copied from.

The part on the left isn't variable in length, so you can add it on as a constant. Then have your script set COLUMNS using this value. I'm not sure if this will work... Good luck!

Ray
  • 2,200