Difference between sentences of cronjobs - php

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.

Related

php CLI mode -f flag

I was reading about specifics of PHP in CLI mode and I can't explain for myself what utility has -f flag.
It's possible execute any php script as "php name_of_script.php" or "php -f name_of_script.php"
I guess this option is just kind of syntactic sugar. Also its existence can be perhaps explained by the fact that it's more obvious for user when he sees -f that file is executed. I can't make up any other explanations. Do someone see any other usage of it?
PHP has a very long history with a lot of the design decisions lost in the mists of time; I'm not sure anyone will be able to tell you for certain why there's both a -f option and the ability to run a file without any options at all.
However, it certainly seems designed for user convenience; most command line users would expect an interpreter to interpret a filename provided as a single parameter, and it's the most common use-case, so making it the quickest to type makes sense. My guess would be that the PHP CLI started off with just the -f option, and the option to run a file by providing just the filename was added later to make people's lives easier. The -f was retained for backwards compatibility.
I can think of one case where the -f option is useful: if the filename starts with a hyphen, for example -.php.
When provided as a single parameter, this will be treated as an option, and fail:
$ php -.php
Usage: php [options] [-f] <file> [--] [args...]
php [options] -r <code> [--] [args...]
php [options] [-B <begin_code>] -R <code
...
However, with -f, it'll work:
$ php -f -.php
<script executes successfully>

Cronjob linux server logging

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

How do I suppress content type information when calling PHP from command line?

I wrote a little PHP script that performs some MySQL transactions and is supposed to be run locally, instead of making it accessible via the web. I run this script by calling it via the prompt
php script.php param1 param2 param3
The script runs fine and returns the expected results as output to stdout. But the output starts with
Content-type: text/html
in the first line, which obviously is unwanted here. I already managed to change this to plain text
header('Content-type: text/plain; charset=utf-8');
But I don't want this information in my output. How can I suppress it?
Add -q argument like
php -q script.php param1 param2 param3
to suppress header information.
Running php -h will show you all arguments that it takes.
Usage: php [-q] [-h] [-s [-v] [-i] [-f <file>] | {<file> [args...]}
-q Quiet-mode. Suppress HTTP Header output.
-s Display colour syntax highlighted source.
-f <file> Parse <file>. Implies '-q'
-v Version number
-C Do not chdir to the script directory
-c <path> Look for php.ini file in this directory
-a Run interactively
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-z <file> Load Zend extension <file>.
-l Syntax check only (lint)
-m Show compiled in modules
-i PHP information
-h This help

bash script that calls php script with cli args

I need to write a bash script that wraps a php script,
I have some variables that needs to be forwarded to the php script and some variables that I need internally for the bash script it self.
The call for the shell script should look like that but the php file can have more params so it needs to be generic :
bash /tmp/test.sh -c -l /tmp/aaa -php aaa.php -d -p 3 -f 2012-10-23
the -php option is mandatory because it contains the php file that needs to be called the -c and -l are optional flag and needs to be used internally for the bash script, everything after the aaa.php are params for the php file.
bash /tmp/test.sh -c -l /tmp/aaa -php "aaa.php -d -p 3 -f 2012-10-23" ?
Don't know how you are passing anythin inside bash script, bu basically everything wrapped in apostrophes is treated as one argument in bash script, passed further to php interpreter is seen as separated parameters, unless of course you don't wrap it again.

Running php script (php function) in linux bash

How we run php script using Linux bash?
php file test.php
test.php contains:
<?php echo "hello\n" ?>
From the command line, enter this:
php -f filename.php
Make sure that filename.php both includes and executes the function you want to test. Anything you echo out will appear in the console, including errors.
Be wary that often the php.ini for Apache PHP is different from CLI PHP (command line interface).
Reference: https://secure.php.net/manual/en/features.commandline.usage.php
First of all check to see if your PHP installation supports CLI. Type: php -v. You can execute PHP from the command line in 2 ways:
php yourfile.php
php -r 'print("Hello world");'
There are two ways you can do this. One is the one already mentioned, i.e.:
php -f filename.php
The second option is making the script executable (chmod +x filename.php) and adding the following line to the top of your .php file:
#!/path/to/php
I'm not sure though if a webserver likes this, so if you also want to use the .php file in a website, that might not be the best idea. Still, if you're just writing some kind of script, it is easier to type ./path/to/phpfile.php than having to type php -f /path/to/phpfile.php every time.
Simply this should do:
php test.php
just run in linux terminal to get phpinfo .
php -r 'phpinfo();'
and to run file like index.php
php -f index.php
php -f test.php
See the manual for full details of running PHP from the command line
php test.php
should do it, or
php -f test.php
to be explicit.
I was in need to decode URL in a Bash script. So I decide to use PHP in this way:
$ cat url-decode.sh
#!/bin/bash
URL='url=https%3a%2f%2f1%2fecp%2f'
/usr/bin/php -r '$arg1 = $argv[1];echo rawurldecode($arg1);' "$URL"
Sample output:
$ ./url-decode.sh
url=https://1/ecp/

Categories