[linux] TERM environment variable not set

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..."

This question is related to linux sh

The answer is


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

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.


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.