I'm trying to determine, within a Perl script on Linux, whether it's running in a terminal.
That is, I need code that:
- returns true when simply running on the command-line
- also returns true when running
./myscript.pl | lessor even./myscript.pl </dev/null >/dev/null 2>/dev/null - returns false when running in a cron job, or as a CGI script
Especially because of the second bullet, I can't use -t STDOUT and variations, and also IO::Interactive is of no use.
The information does appear to be available. If I run ps, it shows an entry like pts/2 in the TTY column, even when I run ./myscript.pl </dev/null >/dev/null 2>/dev/null, and ? when running as a cron job or CGI script.
Is there an elegant way to determine this in a Perl script? I'd rather not have to parse the output of ps.
isatty(3)function.isattyexists in the POSIX module, yes, but, like-t, checks if a file handle is connected to a tty. Not what I need...ctermiddoes indeed exist in the POSIX module. Unfortunately, it returns/dev/tty/on the command line as well as in a cron job.