TERM environment variable not set

LinuxSh

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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.

Solution 3 - Linux

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.

Solution 4 - Linux

You can replace :

> export TERM=xterm

with :

> export TERM=linux

It works even in kernel with virgin system.

Solution 5 - Linux

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

Solution 6 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmeyquelView Question on Stackoverflow
Solution 1 - Linuxcpr4t3sView Answer on Stackoverflow
Solution 2 - LinuxJosiah DeWittView Answer on Stackoverflow
Solution 3 - LinuxPeteshView Answer on Stackoverflow
Solution 4 - LinuxAkram BEN GHANEMView Answer on Stackoverflow
Solution 5 - Linuxdavidjimenez75View Answer on Stackoverflow
Solution 6 - LinuxEvyatar SaiasView Answer on Stackoverflow