I'm trying to use PHP command line (from cron tab). I know how to add arguments like this:
cd /home/users/public_html/; php -f script.php some_value
I would like (or need) to add current date dinamically:
cd /home/users/public_html/; php -f script.php current_date
With wget I did this:
wget "https://mysitecom/script.php?currentdate=`date +\%s.\%N`"
But I can not find any way to do something similar wih a php command line.
I've tried:
cd /home/users/public_html/; php -f script.php `date+\%s.\%N`
And I get the error "Command not found".
I've tried also the solution proposed in one answer:
cd /home/users/public_html/; php -f script.php date+\%s.\%N
And I get the literal string "date+\%s.\%N"
With the other proposed solution:
cd /home/users/public_html/; php -f script.php "$(date +"%s.%N")"
I get these errors in the email sent by the cron:
/usr/local/cpanel/bin/jailshell: -c: line 0: unexpected EOF while looking for matching `"'
/usr/local/cpanel/bin/jailshell: -c: line 1: syntax error: unexpected end of file
When using PHP from the command line (CLI) they are not called GET variables they are called arguments
There are 2 parameters passed to every script run from the command line called $argv and $argc
One thing to remember the first argv[0] occurance holds the name of the script that is being run. Other than that the arguments appear in $argv[] in the order thay appear on the command line
argc is a count of how many variables have been passed to the script
arcv is an array of all the variables passed
If its any help they are just like the "C" equivalents if you have ever written any "C" code
ADDITIONAL INFO
To call your script with todays date use something like this
cd /home/users/public_html/; php -f script.php "$(date +"%s.%N")"
adjust the format as required.
Although if you want todays date in the script I am not sure why you would not get that from within the PHP script itself.
Finally I've made it work. It was a syntax error:
This:
cd /home/users/public_html/; php -f script.php `date+\%s.\%N`
Need to be:
cd /home/users/public_html/; php -f script.php `date +\%s.\%N`
Related
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 trying to run a cronjob in Centos with crontab -e, but I can't figure out when the job runs why it does not find any parameters as -app in the ShellDispatcher. I have read
Cake PHP Cronjobs and have:
Edited .bashrc and added export PATH="$PATH:/home/phonekar/public_html/app/Console"
Added a cronjob to my useraccount with crontab -e
53 23 * * * /home/useraccount/public_html/lib/Cake/Console/cakeshell Tracking -cli "/usr/bin" -console "/home/useraccount/public_html/lib/Cake/Console" -app "/home/useraccount/public_html/app" >> /home/useraccount/public_html/tracking.log 2>&1
Manually running this command works fine, but when I let the job run i get this error on a loop:
PHP Warning: array_search() expects parameter 2 to be array, null given in /home/useraccount/public_html/lib/Cake/Console/ShellDispatcher.php on line 320
PHP Warning: array_splice() expects parameter 1 to be array, null given in /home/useraccount/public_html/lib/Cake/Console/ShellDispatcher.php on line 324
I looked in to the file and it seems like it is expecting the -app value but instead is getting a null value passed to it. I have also verified that the Tracking shell works. Any clarification will help.
Warning: array_splice() expects parameter 1 to be array, null given in ShellDispatcher.php
I hit this error using console of cakephp 2.3.8 but you can encounter this issue on other versions as well.
The solution is to edit (command line arguments are not seen)
app/Console/cake and on on the line:
exec php -q "$CONSOLE"/cake.php -working "$APP" "$#"
after -q add
-d register_argc_argv=1
so the line will look like:
exec php -q -d register_argc_argv=1 "$CONSOLE"/cake.php -working "$APP" "$#"
ref:
http://www.ecommy.com/programming/cakephp/warning-array_splice-expects-parameter-1-to-be-array-null-given-in-shelldispatcher-php
I'm trying to execute a PHP script on Windows through php.exe, while passing parameters to the script. Everywhere I look, it says it should work like this:
php -f "path\to\my\script.php" -- -t 10 -i 5
The -t 10 -i 5 should be passed to script.php, where I can access them through $argv. When I type this in on the command line, everything runs as expected. When I paste the very same line in a .cmd file, the part after script.php gets treated as a seperate command. (and yes, it is a single line in the batch file)
C:\>php -f "path\to\my\script.php" -- -t 10 -i 5
<<<output of the php script as expected>>>
C:\>mybatch.cmd
C:\>php -f "path\to\my\script.php"
<<<output of the php script not receiving the parameters>>>
C:\>-- -t 10 -i 5
'--' is not recognized as an internal or external command, operable program or batch file.
I first thought it might be a problem with the --, but even if I leave out the -- (basically passing the other parameters to php.exe instead of to the script), the same problem occurs.
Any ideas on why this is happening?
Found the problem, appearantly there was a linefeed after the filename of the script, but that was not visible in notepad. No idea how it got there (probably copy/paste related), but removing it fixed the problem.
I am trying to run the following commands from a .sh script at the command line. The problem is that when I try to run them one after another they give the following error:
"not found.batch_control_files/Res" and "not found.batch_control_files/Condo
" ...if I remove all but one of the commands it runs fine? I was hoping I could have each command run and then execute the next command...not sure what it is doing here?
/usr/bin/php -f run_interactive_job.php batch_control_files/Res
/usr/bin/php -f run_interactive_job.php batch_control_files/Condo
/usr/bin/php -f run_interactive_job.php batch_control_files/Land
What's in "run_interactive_job.php"?
If you use absolute paths you can at least be sure it's looking where you expect.
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/