1

I have the following script:

#!/bin/bash
/usr/bin/echo q | /usr/bin/htop -C | /usr/bin/aha --line-fix | /usr/bin/html2text -width 999 | /usr/bin/grep -v "F1Help\|xml version=" > htop.txt

It just captures the htop output.

It works fine if I run the script via command line but then if I run it via crontab as root:

15 15 * * 1-5 /bin/bash /root/collect_system_stats.sh

htop.txt will have just 1 byte and hexdump shows:

0000000 000a                                   
0000001

What I have done wrong?

2

You should use grep with the --line-buffered flag, otherwise grep exits after the first match. I don't have a full qualified explanation for this, but that's what made my script work in a similar case.

Found this answer to line-buffer for grep.

Installing a cronjob as root user with htop can result in a error message:

/usr/bin/htop -C Error opening terminal: unknown

Setting TERM=xterm in the script can resolve this issue.

  • Thanks for your reply. My understanding is that grep will grep line by line regardless of the match result. I just added --line-buffered, and I will report back in 20 hours. Thanks! – HCSF Oct 17 '18 at 13:33
  • The link I pasted says "[...] most standard commands, include grep, buffer the output, meaning it does not write data immediately to stdout. It collects large amount of data (depend on OS, in Linux, often 4096 bytes) before writing." – eblock Oct 17 '18 at 14:00
  • I thought before exit, it would flush everything. I thought --line-buffered was for people to see quick result (e.g. grepping 1 character in few terabyte of data and such character exists at the beginning). – HCSF Oct 18 '18 at 1:18
  • The cronjob just kicked in. Didn't work :( Thanks tho :) – HCSF Oct 18 '18 at 1:19
  • I just saw that you use only > htop.txt, this way your file will get replaced everytime you run the script. In addition to the missing --line-buffer the result is exactly what I would expect. You should just append the new output to file: >> htop.txt should do it. For tests you could just add a new line in the cronjob to run every minute or so, so you don't have to wait a day for the results. – eblock Oct 18 '18 at 6:58

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.