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,377
  • 3
  • 51
  • 80
  • 1
    Telling us which remote procedure (RP) you "followed" doesn't help us help you for N reasons: 1) It's remote. Will the link exist tomorrow? 2) Reading the RP doesn't tell us how accurately you "followed" it. Did you suffer typos or missed lines? We have. 3) Reading the RP omits the error messages you got on your system. These error messages (and the commands that caused them) are key elements in any diagnosis. – waltinator Apr 23 '21 at 02:45
  • Try export COLUMNS=500 before your htop command. – waltinator Apr 23 '21 at 02:47
  • @waltinator The bar became extremely long https://i.imgur.com/ezrnUta.png – Liso Apr 23 '21 at 03:04
  • If you want to replicate the second image (which also appears truncated), I guess export COLUMNS=120 or similar would work. Or any other number you deem appropriate. I can't imagine you want unlimited width. – sancho.s ReinstateMonicaCellio Apr 27 '21 at 11:39
  • @sancho.sReinstateMonicaCellio You're right, I just noticed that. Thanks for pointing it out ! – Liso Apr 28 '21 at 02:18
  • @Liso please add exactly the commands you used for each screenshot. Otherwise it is quite difficult to guess... – maholtz Apr 30 '21 at 09:26

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,071