32

We have a shell script which is run by CRON. The shell script in turn runs a python script which downloads files from an FTP server and then runs a java log processor on those files. The process runs just fine, except that I keep on getting CRON emails even if there is no error. At least, I think there is no error. The cron email has two lines, out of which one of the lines is

tput: No value for $TERM and no -T specified

After researching a bit, I found that it's something to do with setting the $TERM variable. I am not sure, how to do that. Any help would be appreciated. Thanks!

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Seagull
  • 2,219
  • 6
  • 25
  • 33

3 Answers3

17

Something in the script is calling the tput binary. tput attempts to inspect the $TERM variable to determine the current terminal so it can produce the correct control sequences. There isn't a terminal when cron is running so you get that error from tput.

You can either manually assign a TERM value to the cron job (likely dumb or something similar to that) or (and this is likely the better solution) you can find out what is calling tput and remove that call.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • I call other libraries, which might be calling tput. I'll try to assign a TERM value and see whether that fixes it – Seagull May 01 '15 at 17:42
  • 1
    Thank you! I just set the TERM variable to "dumb" as you suggested, and this worked! – Seagull May 08 '15 at 15:31
  • @Seagull Whatever is calling `tput` is assuming that a terminal is available for output. Cron jobs run without a terminal. If setting `TERM=dumb` works, that's great, but you *might* want to consider investigating why `tput` is being called. I can imagine that incorrect assumption causing other problems. – Keith Thompson Jun 03 '21 at 23:50
16

The cron daemon is run by 'root' user in its own shell. By default, cron will append a system mail sent to the user running the script (that's why you see the sender as 'root' in the system mail). The 'user' is the user you were logged in as when setting the crontab. The mail will contain console and error messages. On Ubuntu, the mail file is viewable at /var/mail/<username>.

If no $TERM variable is set, cron will emit a tput: No value for $TERM and no -T specified error in the mail file. To stop these errors, set the $TERM variable using TERM=dumb (or another available terminal in your system, like xterm) in the crontab. The toe command will show you the terminfo definitions on the current system. If you lack that command, you can see the raw data in /usr/share/terminfo on most Linux systems.

Even though you have stopped the errors, you may still get appended system mail with console messages. This file will fill up like a log over time, so you may want to stop these messages. To stop cron system mail, set the MAILTO variable using MAILTO=""

So your crontab might look like:

MAILTO=""
TERM=xterm

* * * * * sh /path/to/myscript.sh

You can view the crontab (for the user you are signed in as) with 'crontab -l'.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
Peter Bloom
  • 1,302
  • 14
  • 21
2

--> MY WORKING SOLUTION FOR THE TPUT-PROBLEM:


# when $TERM is empty (non-interactive shell), then expand tput with '-T xterm-256color'
[[ ${TERM}=="" ]] && TPUTTERM='-T xterm-256color' \
                  || TPUTTERM=''

declare -r    RES='tput${TPUTTERM} sgr0'       REV='tput${TPUTTERM} rev'
declare -r    fRD='tput${TPUTTERM} setaf 1'    bRD='tput${TPUTTERM} setab 1'
declare -r    fGN='tput${TPUTTERM} setaf 2'    bGN='tput${TPUTTERM} setab 2'
...
echo ${fRD}" RED Message: ${REV} This message is RED REVERSE. "${RES}
echo ${fGN}" GREEN Message: ${REV} This message is GREEN REVERSE. "${RES}
...

this way it makes no sense if there's an interactive or a non-interactive shell - tput still works fine.

6a5h4

6a5h4
  • 29
  • 1