You can run PHP with the -q command line switch. The manual only say:
Quiet-mode. Suppress HTTP header
output (CGI only).
What does that actually mean in practical terms?
This only concerns the PHP interpreter built against the CGI SAPI. This version sends a few basic HTTP headers before any actual output:
X-Powered-By: PHP/5.3.3-1ubuntu9.3
Content-type: text/html
"(echo) What I actually wanted to have"
So basically the -q commandline flag prevents any header() from being written to stdout.
The purpose is to use the php-cgi binary in lieu of the php CLI variant for console scripts. Usually you see following shebang in such scripts to force php-cgi to behave like the -cli version:
#!/usr/bin/php-cgi -qC
As you can see with -q key php suppresses to send headers (added some new lines in the output though to make it more readable):
zerkms#l12 ~ $ cat file.php
<?php
header('Location: http://stackoverflow.com');
echo 42;
zerkms#l12 ~ $ php file.php
Status: 302 Moved Temporarily
X-Powered-By: PHP/5.2.17
Location: http://stackoverflow.com
Content-type: text/html
42
zerkms#l12 ~ $ php -q file.php
42
Related
I am writing a toy Zend extension.
It works just fine when I run my script:
$ php -z /path/to/my/extension.so script.php
HELLO, TOY EXTENSION!
However, it does not work when I run the built-in development server:
$ php -z /path/to/my/extension.so -S localhost:8080 script.php
[Wed Aug 18 15:25:12 2021] PHP 7.4.21 Development Server (http://localhost:8080) started
As a sanity check, I tried making the extension write to a file instead of stdout, and still nothing happens.
Why isn't the local server using the extension? Naively it seems like this would Just Work. How can I get it to work? This would be very useful for testing and iterative development.
In case it's relevant, the hook where I put my printf is zend_extension.activate, although I have tried others as well (including module_startup).
I am attempting to backround a php script since it will take more than a minute to complete and I do not want the user to wait.
my exec command is as follows:
exec ('php -f path/to/file.php > path/to/output.log 2>&1 &');
first of all the script in the file didnt do what i programmed it to do however, the output file still recieves this output:
X-Powered-By: PHP/5.6.24
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
Content-Type: text/html; charset=UTF-8
Link: <https://example.com/wp-json/>;
rel="https://api.w.org/"
Link: <https://example.com/?p=687>; rel=shortlink
....
This output is not at all what my script is supposed to make, it makes no sense to me.
the rest of the output is a html document with differnet links to my website and such.
can anyone clue me into why this is happening and not simply running the script?
BTW
I have used different commands like /usr/bin/php with the same affect
UPDATE
I noticed that after changing the first path/to/file.php paremeter to gibberish i.e.
exec ('php -f asdfjaskldfj > path/to/output.log 2>&1 &');
that the output remains the same, not sure what this means but i believe it to be noteworthy
After must trial and error I found that
usr/bin/php
pointed to a php command that only outputted documentation on my current server, and when I changed it to
usr/bin/php5
it worked. Very hard to find documentation on the linux php command, and I still have yet to find anyone else with the same problem, but it has been resolved nonetheless.
What is the difference between these two cron commands:
/usr/local/bin/php -f /home/username/public_html/...
/usr/local/bin/php -q /home/username/public_html/...
the first one is "-f" and the second one "-q"
Cronjob works fine with both of them. I just don't know what is the difference between them.
Thanks.
From the PHP manual:
f:
-f --file
Parse and execute the specified file. The -f is optional and may be omitted - providing just the filename to execute is sufficient.
q:
-q --no-header
Quiet-mode. Suppress HTTP header output (CGI only).
Since -f is optional and -q only applies to the CGI-version of PHP (you are running the regular command line interpreter, however), this leaves you with the same command twice:
/usr/local/bin/php /home/username/public_html/...
To explicitly answer your question: In this case, there is no difference between those two commands!
The two options are of PHP command.
--no-header
-q Quiet-mode. Suppress HTTP header output (CGI only).
and
--file file
-f file Parse and execute file
are shown in the help doc of PHP, you can check them with man php in your terminal.
Also the synopsis contains
php [options] [ -f ] file [[--] args...]
where the -f seems not to be necessary.
Im using an ajax call to execute "shell_exec" on the server (centos).
The line that im executing is the following
echo shell_exec("php -q /websockets/timedactions.php");
This is the server response:
X-Powered-By: PHP/5.5.16
Access-Control-Allow-Origin: http://myIpAddress
Access-Control-Allow-Credentials: true
Content-type: text/html
0
After running this command it seems that the process that it supposed to activate is not running.
Calling the same command on shell with root access
php -q /websockets/timedactions.php
works perfectly.
How can i make the script work using shell_exec ?
There can be errors, while the command is executed. You can redirect STDERR to STDOUT, to see if there are any, like this(Sample 3.5).
echo shell_exec("php -q /websockets/timedactions.php 2>&1");
Also there could be something preventing shell_exec from printing (link).
I have some cronjobs running on my linux server. These cronjobs are just executing some PHP scripts. What I want to do is to log any possible outputs these scripts would have given.
I want to use the output as given by the command:
wget -O /logs/logfile /pathofthefolder/script.php
The problem is that this command overwrites the previous logfile, thus the logfile only contains the output of the last execution, which is kinda useless for logging.
I tried adding an -a for appending instead of overwriting, but that didn't work.
I also tried with only an -a like this:
wget -a /logs/logfile http://example.com/script.php
But also that didn't work, I get the information of the download in the logfile as such:
-2014-05-27 21:41:01-- http://example.com/script.php
Resolving example.com (example.com)... [ip address of my site]
Connecting to example.com (example.com)|[ip address of my site]|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `script.php.4'
0K
So the information of the HTTP request is being stored in the logfile, and the output of every request is saved in a seperate file with increasing numbers, script.php.1, script.php.2 and so on. Which isn't quite what I want, I'd prefer to have it all in one file, I don't need the HTTP info.
Update:
So I know that it would be easier via the php or lynx command, but those commands are not installed on the server. I'm kinda stuck with the wget.
You can use this:
wget -qO- "http://example.com/script.php?parameter=value&extraparam=othervalue" >> /logs/logfile
You might want to try rewriting your cronjobs to use the php interpreter instead of wget:
php -f /path/to/file
This will execute a local (!) php file and write the output to command line.
You can easily redirect this output to apppend to a file:
php -f /path/to/file >> /logs/logfile
(to overwrite instead of append, use a single >)
If you need the error messages as well, you need to redirect both stdout and stderr:
php -f /path/to/file >> /logs/logfile 2>&1
In case you don't have/cannot install the php executable, you can use (if installed) curl to get a similar result as with wget (see other answers):
curl http://localhost/your/file.php >> /logs/logfile