Cakephp ShellDispatcher error in Centos Cronjob - php

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

Related

PHP Script unlinks files interactively in browser, but not as cPanel cron job

The following execution syntax actually runs in cron job:
/usr/local/bin/php -q /home/pbjwbh0mgv9o/public_html/buildlistings.php
Yet I get the following in my error_log:
[05-May-2018 21:53:00 UTC] PHP Warning: unlink(property_a.xml): No such file or directory in /home/pbjwbh0mgv9o/public_html/buildlistings.php on line 63
[05-May-2018 21:53:05 UTC] PHP Warning: unlink(property_map.xml): No such file or directory in /home/pbjwbh0mgv9o/public_html/buildlistings.php on line 215
Once again the following part of script executes perfectly in browser, and attached below is screenshot of directory:
unlink('property_a.csv');
unlink('property_a.xml');
unlink('property_map.xml');
Am I formatting the cron job command incorrectly, or missing something within my script related to Linux?
When you execute the script via the web server, the CWD (current working directory) is what you expect.
When cron executes that job, it's not happening from the same location. You need to make sure it calls cd to change to wherever you expect the files to be.
Edit:
Here's an example for your cron job:
cd /home/pbjwbh0mgv9o/public_html && /usr/local/bin/php -q buildlistings.php

can't execute gnu parallel when called from cron, but works from command line

I have this command in cron on an Amazon EC2 ami linux (centos) instance:
10 9 * * * php -f /var/scripts/schtask/schtask.php
Inside this program, it runs this line using the exec() function calling gnu parallel:
parallel -j-1 < /var/scripts/_working_files/schtask/schtask/_output/micros_schtask__2017-02-22__09.10.01__4ec2d995fd6fb5e7c866e13502714f36.txt
I get a return code of:
127
when this executes. I can't find this error on the gnu parallel exit status list:
https://www.gnu.org/software/parallel/man.html#EXIT-STATUS
The contents of the text file looks similar to this:
#comment 1
#comment 2
php -f /var/scripts/micros/scripts/micros.php /store_list:"s1"
php -f /var/scripts/micros/scripts/micros.php /store_list:"s2"
php -f /var/scripts/micros/scripts/micros.php /store_list:"s3"
However, if I run:
parallel -j-1 < /var/scripts/_working_files/schtask/schtask/_output/micros_schtask__2017-02-22__09.10.01__4ec2d995fd6fb5e7c866e13502714f36.sh
directly on the command line, it works. What does that exit code mean, and why would it not run when being called from cron?
Thanks!
I think your php command is not found. Run which php and replace it.
It might look like this: /usr/bin/php.
10 9 * * * /usr/bin/php -f /var/scripts/schtask/schtask.php
Also do this with parallel.

Why is $this->argument() Showing the Name and the Value?

In Laravel, I am making a console command. Per the docs, you should be able to get the value of the argument using $this->argument():
$userId = $this->argument('user');
I have an argument that is an integer 1. However, $this->argument('some_name') is returning a string such as some_name=1, instead of simply 1
Is there a setting or something that I missed?
Arguments don't get named, unlike options. For example:
$ php artisan command argument1 argument2 argument3
$ php artisan command --option1=foo --option2=bar
So, I'd either change the definition of your argument to an option so that you can run:
$ php artisan command --some_name=1
Or, you can keep using this as an argument and run:
$ php artisan command 1
Note: artisan and command in the above examples are arguments of the php executable.

PHP not working with CRON

I'm using crontab to call a php script.
In this script there is
error_log('test');
When the script is executed from http or direct command line like
php -f script.php
Everything is fine, my error is log.
But when called from cron it's not working.
Here is my cron
* * * * * -u www-data /full_path_to/php -f /full_path_to/script.php
Here is what I tried :
error_log with arguments :
error_log('test', 3, '/full_path_to/error.log');
changing error reporting :
error_reporting(E_ALL);
ini_set('display_errors','On');
ini_set('error_log', '/full_path_to/error.log');
cron call ending with > /full_path_to/error.log 2>&1 (don't know if useful)
For http the error_log path is set from htaccess.
I'm lost with php cli...
I can see the cron execution working every minute (syslog), so it should be a PHP config problem ?
Thanks a lot if you can help.
Edit : Cron is executed with "-u www-data"
Here is the call I see in syslog :
CRON[13921]: (www-data) CMD (-u www-data /usr/bin/php -f /fullpath/script.php > /fullpath/error.log 2>&1)
I was facing the same problem but was able to fix it after reading the manual entry for php.
Initially I had something set like:
/usr/bin/php -f /path/to/php/script.php -c 1426 >> /tmp/script.php.log 2>&1
I was able to fix it by changing the line to:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
As per the manual entry the syntax is:
php [options] [ -f ] file [[--] args...]
Also,
args... Arguments passed to script. Use '--' args when first argument starts with '-' or script is read from stdin
Going by that, my cron command becomes:
/usr/bin/php -f /path/to/php/script.php -- -c 1426 >> /tmp/script.php.log 2>&1
and it works!
You have to be aware that there are user specific crontabs and a system wide crontab.
When you are logged in as user foo and type crontab -e in console this will allow you to edit your own user specific crontab. All defined cron tasks will be executed as user foo. This is even true for user root. AFAIK this way you simply can not change the user under which a cron task will be run.
Quite different is the file /etc/crontab. This is the system wide crontab. Within that file you can change the user of a cron task like this:
* * * * * www-data /full_path_to/php -f /full_path_to/script.php
I had in fact two problems :
1) My cron was executed with php.ini for php-cli. I used the -c flag to load the good one.
2) I was relying on $_SERVER to declare important constants using variables that do not exist in cli-mode. Now if these variables are not set I parse commande line vars with getopt()

Add current date to PHP command line argument

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`

Categories