Nathan P
Nathan P

Reputation: 1006

/usr/bin/env php shebang prints headers when called via sudo or cron

I am using #!/usr/bin/env php to invoke a PHP script at the command line, which has been working great. PHP correctly detects the command line and suppresses HTTP headers.

But I started calling my script via sudo or via a cron job, it has started print HTTP headers.

>> ./test 
Hello world!

>> sudo -u nathan ./test 
Content-type: text/html

Hello world!

./test just contains the following code:

#!/usr/bin/env php
<?php

echo 'Hello world!' , "\n";

I think this has something to do with whether the script is passed a tty, but I am unsure. Is there a way to prevent these headers from being printed? I can't use the "-q" argument, I don't think, since I am calling it via env.

Upvotes: 3

Views: 1415

Answers (1)

Gergo Erdosi
Gergo Erdosi

Reputation: 42043

Your script prints HTTP headers because /usr/bin/env php calls the CGI version of the PHP executable instead of CLI. You can verify this by running which php first to find out where /usr/bin/env php points to. Then run /usr/bin/php -v (or whatever path which php returns) to see if it is CGI or CLI.

By default, PHP is built as both CLI and CGI. So either fix your PHP setup, or use hardcoded path with the -q option instead of env, because you are right, you can't use options with env. I would do the first.

Upvotes: 6

Related Questions