check if running from cron or an interactive shell?

check if running from cron or an interactive shell?

Method 1:
sub I_am_interactive {
return -t STDIN && -t STDOUT;
}
but this wont work, if your script is called from within another script. it will mistakenly report that it is likely not from interactive shell.

so if your system support POSIX, try method 2 below

Method 2:
use POSIX qw/getpgrp tcgetpgrp/;

sub I_am_interactive {
local *TTY; # local file handle
open(TTY, “/dev/tty”) or die “can’t open /dev/tty: $!”;
my $tpgrp = tcgetpgrp(fileno(TTY));
my $pgrp = getpgrp();
close TTY;
return ($tpgrp == $pgrp);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

7 − 5 =