74

I have a file.sh with this, when run show : TERM environment variable not set.

smbmount //172.16.44.9/APPS/Interfas/HERRAM/sc5 /mnt/siscont5 -o 
iocharset=utf8,username=backup,password=backup2011,r

if [ -f /mnt/siscont5/HER.TXT ]; then
    echo "No puedo actualizar ahora"
    umount /mnt/siscont5
else 
    if [ ! -f /home/emni/siscont5/S5.TXT ]; then
        echo "Puedo actualizar... "
        touch /home/emni/siscont5/HER.TXT
        touch /mnt/siscont5/SC5.TXT
        mv -f /home/emni/siscont5/CCORPOSD.DBF /mnt/siscont5
        mv -f /home/emni/siscont5/CCTRASD.DBF /mnt/siscont5
        rm /mnt/siscont5/SC5.TXT
        rm /home/emni/siscont5/HER.TXT
        echo "La actualizacion ha sido realizada..."
    else
        echo "No puedo actualizar ahora: Interfaz exportando..."
    fi
fi
umount /mnt/siscont5
echo "/mnt/siscont5 desmontada..."
Marc B
  • 356,200
  • 43
  • 426
  • 500
meyquel
  • 2,134
  • 5
  • 23
  • 42
  • the server is runing ok, Cron call this .sh every 2 minutes, but sometimes show TERM environment variable not set, then .sh dont do nothing. – meyquel Apr 26 '13 at 17:42
  • 1
    I have in the same server some .sh that Cron call in diferent interval of time. – meyquel Apr 26 '13 at 17:51

6 Answers6

107

You can see if it's really not set. Run the command set | grep TERM.

If not, you can set it like that: export TERM=xterm

Vadorequest
  • 16,593
  • 24
  • 118
  • 215
cpr4t3s
  • 1,345
  • 1
  • 11
  • 15
73

Using a terminal command i.e. "clear", in a script called from cron (no terminal) will trigger this error message. In your particular script, the smbmount command expects a terminal in which case the work-arounds above are appropriate.

Josiah DeWitt
  • 1,594
  • 13
  • 15
  • 16
    this was the issue for me – David Aleu Dec 02 '15 at 14:25
  • 7
    `clear` (or other terminal command) if added to `.bashrc` or other scripts called from `.bashrc`, will result to same. – madD7 Aug 27 '17 at 06:59
  • had a clear it in the motd - how could I ever solve this without finding this post! – jamacoe Mar 21 '21 at 19:13
  • Aaah, that was it, thank you! Now I'd like to find a command which tells me that I am in a terminal? Preferable usable with Python? – ullix Jul 26 '23 at 08:02
  • py: os.environ.get("TERM") bash: $TERM – Josiah DeWitt Jul 26 '23 at 17:40
  • Indeed! In Python TERM has the value None when the script is started from e.g. cron job. TERM is e.g. xterm-256color when started from a terminal. So to clear a terminal without annoying error message in cron jobs is: if os.environ.get("TERM") is not None: os.system("clear") – ullix Jul 27 '23 at 07:48
6

You've answered the question with this statement:

Cron calls this .sh every 2 minutes

Cron does not run in a terminal, so why would you expect one to be set?

The most common reason for getting this error message is because the script attempts to source the user's .profile which does not check that it's running in a terminal before doing something tty related. Workarounds include using a shebang line like:

#!/bin/bash -p

Which causes the sourcing of system-level profile scripts which (one hopes) does not attempt to do anything too silly and will have guards around code that depends on being run from a terminal.

If this is the entirety of the script, then the TERM error is coming from something other than the plain content of the script.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • other .sh is calling by cron in diferent interval of time, when this error appear reboot comand not work... – meyquel Apr 30 '13 at 17:11
  • I'm sorry, but I'm finding it very difficult to understand what you're saying in these comments. the shebang line mentioned should be at the start of the script - the very first line. nothing is mentioned about another script, and if reboot is not working, but you're running as root, then you have something else wrong with the system. – Anya Shenanigans May 01 '13 at 08:09
4

You can replace :

export TERM=xterm

with :

export TERM=linux

It works even in kernel with virgin system.

0

SOLVED: On Debian 10 by adding "EXPORT TERM=xterm" on the Script executed by CRONTAB (root) but executed as www-data.

$ crontab -e

*/15 * * * * /bin/su - www-data -s /bin/bash -c '/usr/local/bin/todos.sh'

FILE=/usr/local/bin/todos.sh

#!/bin/bash -p
export TERM=xterm && cd /var/www/dokuwiki/data/pages && clear && grep -r -h '|(TO-DO)' > /var/www/todos.txt && chmod 664 /var/www/todos.txt && chown www-data:www-data /var/www/todos.txt
0

If you are using the Docker PowerShell image set the environment variable for the terminal like this with the -e flag

docker run -i -e "TERM=xterm" mcr.microsoft.com/powershell
Evyatar Saias
  • 698
  • 7
  • 18