20

I have a script that is designed to be run both as a web page, and via the console.

Detecting which method was used to invoke the script seems pretty straight forward, but when the script is being run from the console, I need to know if the script is being run interactively or not (user typing commands, or input redirected from a file).

php script.php versus php script.php < input_file

Is this possible?

3 Answers 3

37

I also needed a slightly more flexible solution than posix_isatty that could detect:

  • Is the script being run from the terminal
  • Is the script receiving data via a pipe or from a file
  • Is the output being redirected to a file

After a bit of experimenting and digging around through libc headers, I came up with a very simple class that can do all of the above and more.

class IOMode
{
    public $stdin;
    public $stdout;
    public $stderr;

    private function getMode(&$dev, $fp)
    {
        $stat = fstat($fp);
        $mode = $stat['mode'] & 0170000; // S_IFMT

        $dev = new StdClass;

        $dev->isFifo = $mode == 0010000; // S_IFIFO
        $dev->isChr  = $mode == 0020000; // S_IFCHR
        $dev->isDir  = $mode == 0040000; // S_IFDIR
        $dev->isBlk  = $mode == 0060000; // S_IFBLK
        $dev->isReg  = $mode == 0100000; // S_IFREG
        $dev->isLnk  = $mode == 0120000; // S_IFLNK
        $dev->isSock = $mode == 0140000; // S_IFSOCK
    }

    public function __construct()
    {
        $this->getMode($this->stdin,  STDIN);
        $this->getMode($this->stdout, STDOUT);
        $this->getMode($this->stderr, STDERR);
    }
}

$io = new IOMode;

Some example usage, to show what it can detect.

Input:

$ php io.php
// Character device as input
// $io->stdin->isChr  == true

$ echo | php io.php
// Input piped from another command
// $io->stdin->isFifo == true

$ php io.php < infile
// Input from a regular file (name taken verbatim from C headers)
// $io->stdin->isReg  == true

$ mkdir test
$ php io.php < test
// Directory used as input
// $io->stdin->isDir  == true

Output:

$ php io.php
// $io->stdout->isChr  == true

$ php io.php | cat
// $io->stdout->isFifo == true

$ php io.php > outfile
// $io->stdout->isReg  == true

Error:

$ php io.php
// $io->stderr->isChr  == true

$ php io.php 2>&1 | cat
// stderr redirected to stdout AND piped to another command
// $io->stderr->isFifo == true

$ php io.php 2>error
// $io->stderr->isReg  == true

I've not included examples for links, sockets, or block devices, but there's no reason they shouldn't work, as the device mode masks for them are in the class.

(Not tested on Windows - mileage may vary)

8
  • 2
    Very nice, +1 - Under what circumstances will S_IFLNK and S_IFSOCK return true?
    – DaveRandom
    Jul 4, 2012 at 10:43
  • 1
    @DaveRandom: I've never actually found out (or needed them), I included them for completeness when I was getting the masks from the C headers. They're obviously related links and sockets, but when I was experimenting I couldn't get anything to trigger them.
    – Leigh
    Jul 4, 2012 at 10:49
  • Thank you so much. That does exactly what I want! :)
    – Fania
    Jul 4, 2012 at 10:50
  • @DaveRandom: They are for symlinks (when specifically opened as a link) and UNIX sockets respectively. These should never be true for the STD* streams that get setup, they are for 'proper' file streams. The same goes for block devices (typically harddrives of some type). Jul 19, 2012 at 0:23
  • 1
    @LukasS this could be an artifact of how debians cron system executes the script? You'd have to check the source for that. Traditionally cron output is sent as a mail, so it could be being piped to sendmail?
    – Leigh
    Jan 5, 2016 at 15:56
5

posix_isatty()

if (posix_isatty(0)) {
  // STDIN is a TTY
} else {
  // STDIN is a pipe or has no associated TTY
}

Obviously this only works on POSIX compliant operating systems, where PHP has the posix extension installed. I am not aware of a Windoze equivalent.

3

For Windows, you can use this:

$b = stream_isatty(STDIN);
if ($b) {
   echo "php script.php\n";
} else {
   echo "php script.php < input_file\n";
}

https://php.net/function.stream-isatty

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.