Skip to main content
Updated link to bash man pages - new manual splits the references, so added the second link too
Source Link
Tim Kennedy
  • 20.2k
  • 5
  • 42
  • 58

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

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

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 

reference: http://techdoc.kvindesland.no/linux/gnubooks/bash/bashref_54.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...

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 

references:

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

Updated URL for bashref_54.html
Source Link
Tim Kennedy
  • 20.2k
  • 5
  • 42
  • 58

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 

reference: http://theory.uwinnipeg.ca/localfiles/infofiles/bash/bashref_54.htmlhttp://techdoc.kvindesland.no/linux/gnubooks/bash/bashref_54.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...

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 

reference: http://theory.uwinnipeg.ca/localfiles/infofiles/bash/bashref_54.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...

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 

reference: http://techdoc.kvindesland.no/linux/gnubooks/bash/bashref_54.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...

Source Link
Tim Kennedy
  • 20.2k
  • 5
  • 42
  • 58

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 

reference: http://theory.uwinnipeg.ca/localfiles/infofiles/bash/bashref_54.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...