The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Cron job unable to call functions

Discussion in 'General Discussion' started by ferco, Feb 10, 2014.

  1. ferco

    ferco Registered

    Joined:
    Feb 10, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    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.
     
  2. vanessa

    vanessa Well-Known Member
    PartnerNOC

    Joined:
    Sep 26, 2006
    Messages:
    817
    Likes Received:
    22
    Trophy Points:
    18
    Location:
    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.
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,776
    Likes Received:
    663
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    I suggest following the steps mentioned in the previous post. In addition, remember the default permissions for PHP files are 0644.

    Thank you.
     
  4. ferco

    ferco Registered

    Joined:
    Feb 10, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    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?
     
  5. ferco

    ferco Registered

    Joined:
    Feb 10, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    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!
     
  6. cPanelPeter

    cPanelPeter Technical Analyst III
    Staff Member

    Joined:
    Sep 23, 2013
    Messages:
    569
    Likes Received:
    15
    Trophy Points:
    18
    cPanel Access Level:
    Root Administrator
    Twitter:
    Hello,

    Glad to hear that it's working. Thank you for updating this thread with your solution.
     
Loading...

Share This Page