0

I have a crontab like this for root user:

9 17 * * * TERM=xterm /bin/bash /root/htop_stat.sh

htop_stat.sh:

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

If I manually run htop_stat.sh as root, it works fine.

However, when the above cronjob kicks in, htop.txt only has the first 23 lines of htop's output.

Any idea?

  • Have you try to remove set of TERM in cron? – Romeo Ninov Nov 20 '18 at 10:10
  • Yes, I actually started without and it would make /tmp/htop.txt empty because htop relies on the environment variable TERM. Thanks. – HCSF Nov 20 '18 at 10:15
  • Have you consider using top, ps? htop by definition is "interactive process viewer" – Romeo Ninov Nov 20 '18 at 10:19
  • htop gives me a bit more detail about all the processes. That's why I choose htop instead. And yes, htop is interactive. But what I don't get is, "bash htop_stat.sh > /tmp/htop.txt" works perfectly. – HCSF Nov 20 '18 at 10:23
  • It work fine because when you open terminal you have TERM variable set. Which is not the case with cron. In your shell script exec something like: stty rows 50 cols 132 before the command and check the result – Romeo Ninov Nov 20 '18 at 10:33
1

The prefix TERM=xterm is not allowed for a cron command, it is not a shell prompt.

Either put it as a separate line in the crontab file:

TERM=xterm
9 17 * * * /bin/bash /root/htop_stat.sh

or include it in your htop_stat.sh.

See man 5 crontab for details.

  • Thanks! You are indeed right that environment variable should be in a separate line per manpage. Tho, it is weird that putting in a separate line also has the same result as putting it in the same line preceding to the command. – HCSF Nov 20 '18 at 14:27
  • I think why environment variable can be set preceding my script. link – HCSF Nov 21 '18 at 1:46

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.