Cron job unable to call functions

ferco

Registered
Feb 10, 2014
3
0
1
cPanel Access Level
Root Administrator
Hi! I have a cron job on cPanel calling a php script every 2 minutes, I'm using the following format on the command:

php -q /home/user/public_html/folder/my_script.php

The problem is that I get the following error:
PHP Fatal error: Call to undefined function json_last_error() in /home/user/public_html/my_script.php on line 724

I'm running on PHP Version 5.4.8 and JSON support is enabled, according to phpinfo(). And just before using this function in the code there is a json_decode call, and that one doesn't seem to have any trouble.

If I try to run the script calling it from a web explorer it runs perfectly, with no errors nor warnings. Last week I changed the file's permissions and it got working, but today is not working again, so I checked the permissions and found that they went back to 777 (maybe because some minor changes I did on the file), and tried to set it to 744 like I did before, but this time it didn't work.

Does somebody knows something about this?

I appreciate any help in advance.
 

vanessa

Well-Known Member
PartnerNOC
Sep 26, 2006
833
28
178
Virginia Beach, VA
cPanel Access Level
DataCenter Provider
Permissions wouldn't cause an issue like this. You might want to check your cron environment real quick. Create a new cron job to run this command:

php -m

You might also want to check the version as well:

php -v

(again, these should be run over cron so they get executed in the same environment)

Then when the cron runs, check the output to make sure that json is indeed loaded here and it's reporting the version you expect. This is just confirming that you don't perhaps have a different version of PHP being executed over cron than the version that runs on your website (often times 'php' called via cron will refer to /usr/bin/php instead of /usr/local/bin/php, or vice-versa). And yes, EasyApache installs binaries in both locations. If you find this to be the case, figure out which one has json loaded and specify the full path in your cron, ie:

/usr/local/bin/php -q /home/user/public_html/folder/my_script.php


I suspect that this is the case, being that json_last_error() was introduced in PHP 5.3 and json_decode() is available in earlier versions, hence why an error for the latter would not be present. This is what leads me to believe that your crons are executing a different version.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
If I try to run the script calling it from a web explorer it runs perfectly, with no errors nor warnings. Last week I changed the file's permissions and it got working, but today is not working again, so I checked the permissions and found that they went back to 777 (maybe because some minor changes I did on the file), and tried to set it to 744 like I did before, but this time it didn't work.
I suggest following the steps mentioned in the previous post. In addition, remember the default permissions for PHP files are 0644.

Thank you.
 

ferco

Registered
Feb 10, 2014
3
0
1
cPanel Access Level
Root Administrator
Thanks Vanessa! I wasn't aware of that.

I remember now that I did have some path for calling php when it got working, but I thought that was because of the file's permission, the problem here is that I don't remember what path was it, because I found it in some forum somewhere else.

I ran those commands you recommend and turns out that '/usr/bin/php' and '/usr/local/bin/php' are running version 5.2.9.

Is there any way to make the cron jobs to run the same version as the one available for web explorer?
 

ferco

Registered
Feb 10, 2014
3
0
1
cPanel Access Level
Root Administrator
Found it!

phpinfo() says it's at '/usr/local/php54', so I used '/usr/local/php54/bin/php', and it is working now.

Thanks for your help!