Sublime Forum

Build system with PHP script always outputs exec()

#1

In my build system using a PHP script I’m trying to capture the output of a command so I can filter the results but ST is always capturing the output somehow and displaying it.

For example the build command:

{
	"cmd": ["php", "$packages/FPC/build.php", "$file", "$project_path"],
}

then a call to exec() in build.php:

exec($command, $results, $exit_code);

Usually this will not print anything because the results are captured in $results but in ST all the output is printed in the console automatically.

Does anyone know why this may be? I tried using popen() and reading the file handle also but the same thing happens, i.e. the output is read by ST bypassing the PHP script.

0 Likes

#2

Do you have a minimal example of a PHP script that exhibits this behaviour? I tried it with the following sample script, but it produces no output unless I specifically echo the $results array that falls out of the command.

<?php
  exec('whoami', $results);
?>
0 Likes

#3

I tested the code you posted and indeed it does not force output like in my script.

What I found out was that a command line option from the compiler is causing this behavior and if I remove that option it works as expected. I’ll go ask the compiler team but do you have any ideas where ST is getting the data from or where the compiler may be writing it? The option is related to verbosity so I suspect the compiler is writing to another pipe besides STDOUT and ST is reading it there.

0 Likes

#4

Sorry for the noise but I figured it out. I had to redistrict the outputs around using 2>&1 >/dev/null. Not exactly sure why that worked or what ST was doing. Thanks.

0 Likes

#5

Sublime captures the stdout and stderr of whatever it’s running and displays them in the build results window (you can look in Default\exec.py to see exactly how it accomplishes that).

If the program being executed runs yet another program that generates the output, that programs output blends with the output of the parent program that ran it. Similarly, if that program ran a child program, the same thing would happen.

The construct 2>&1 > /dev/null redirects stderr (file handle 2) to stdout (file handle 1), and then redirects the whole result to /dev/null to throw the output away entirely, which is why doing that stops the child process from having it’s output appear.

0 Likes