8

I am trying to run a bash script I have via cron, and I am getting the following error at the beginning of the execution:

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

Here is what is in my crontab:

0 8 * * 1-5 cd /var/www/inv/ && /var/www/inv/unitTest run all 2>&1| mail -r "[email protected]" -s "Daily Inventory Unit Test Results" [email protected]
ComputerLocus
  • 183
  • 1
  • 5

2 Answers2

10

Your unit test script probably calls tput in order to generate pretty output showing which tests pass and fail. Under cron there is no terminal and thus no terminal type ($TERM), so tput cannot control the nonexistent terminal.

Your unit test script needs to have 2 modes:

  • running on a terminal: it can call tput to generate pretty-looking output
  • not running on a terminal: it should not call tput and instead generate a generic text-only output format that is suitable for piping into an email as you are doing here.

The easiest way for the unit tests to know whether or not they are running on a terminal is to test of the stdio file descritors refer to a terminal. If it's a shell script, then:

if [ -t 1 ]; then
    tput bold; echo pretty; tput sgr0
else
    echo ugly
fi

Basically: do not call tput unless you are running on a terminal, and you will thus avoid the error you are getting, plus produce reasonable output in whichever mode you happen to be running under.

Celada
  • 43,173
  • 5
  • 96
  • 105
  • Realized I had this in my script `sep=$(printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =)`. I guess this is where the tput issue is coming from. Thanks for the information, I'll try and apply it. – ComputerLocus Jun 11 '15 at 13:06
0

Expanding on the answer given by Celada above (I have to post an answer rather than comment since I have yet to have a reputation score high enough to comment on others answers)...

Per advice on TLDP.org, it is possible that [ -t 0 ] can work on a local terminal but fail when invoked remotely over ssh.

For this reason, you also need to check for the presence of a socket. Here is an example of a check for both terminal or socket.

if [[ -t 0 || -p /dev/stdin ]]
then
  echo interactive
else
  echo non-interactive
fi