I have several shell scripts (bash) which are started by cron. Every time a script is executed I get an email saying “stdin: is not a tty”. Can someone please tell me how to fix this? All scripts run successfully but these mails are killing my email folder.
I use Debian Wheezy. “/root/.bashrc” is empty. Cron entries are like:
/bin/bash --login /root/script.sh > /dev/null
Even this script produces the error message:
#!/bin/bash ls
Content of .profile:
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Something in your .bashrc is assuming that the shell is running on a terminal. That’s perfectly fine: .bashrc is supposed to run only in interactive shells, and interactive shells are supposed to run only on terminals.
The problem is that you’re systematically including .bashrc from .profile. That’s wrong: you should only include .bashrc in interactive shells.
Change your .profile to
# Bash doesn't load its interactive initialization file if it's invoked as # a login shell, so do it manually. case $- in *i*) if [ -n "$BASH" ]; then . ~/.bashrc;; fi esac
Move mesg n into .bashrc: it’s a terminal-related command, not a session-related command.
If you have environment variable definitions in your .bashrc, move them to .profile. The .profile file is for things that are executed when your session starts, typically mainly environment variable definitions, used by any application that you’ll run during the session. The .bashrc file is the configuration file for bash when running interactively, it typically contains terminal setup, alias definitions, shell options and completion settings, and other things related to the interactive use of the shell.
For background, see:
- Difference between Login Shell and Non-Login Shell?
- How to check if a shell is login/interactive/batch
- Is there a “.bashrc” equivalent file read by all shells? (and follow the links)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0