Scripts run from cron are not run in interactive shells. Neither are startup scripts. The differentiation is that interactive shells have STDIN and STDOUT attached to a tty.
Method 1: check if $- includes the i flag. i is set for interactive shells.
case "$-" in *i*) interactive=1 ;; *) not_interactive=1 ;; esac Method 2: check is $PS1 is empty.
if [ -z "$PS1" ]; then not_interactive=1 else interactive=1 fi referencereferences: http://techdoc.kvindesland.no/linux/gnubooks/bash/bashref_54.html
- https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html
- https://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html
Method 3: test your tty. it's not as reliable, but for simple cron jobs you should be ok, as cron does not by default allocate a tty to a script.
if [ -t 0 ]; then interactive=1 else non_interactive=1 fi Keep in mind that you can however force an interactive shell using -i, but you'd probably be aware if you were doing this...