Question

Output from php script run by cronjob differs from manually executing it

Posted June 3, 2020 560 views
PHPUbuntu 18.04

I am getting a different result from a php script when I run it as a cronjob as when I execute it manually. Have tried to look for information but can’t figure out what is happening. Please help!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @fabriziocastrob6,

It is quite possible that when you run the PHP script via cron it might use a different php.ini file.

I could suggest 2 things:

  • In your crontab make sure to specify the exact path to the php binary, to find out the path run:
  • which php
  • If the script is in a specific directory, you could add a cd command before the php command in cronjob, so it would look something like this:
cd /path/to/your/directory && /usr/bin/php script.php
  • If this still does not work, try specifying the path to your custom php.ini file if you use one, you can do that by adding the following flag:
php -c /path/to/your/custom/php.ini

Let me know how it goes!
Regards,
Bobby

  • Hi Bobby!

    I tried everything you just said… still doesn’t work :/
    I don’t understand, works flawlessly when executed manually..

    • Hi there @fabriziocastrob6,

      What I could suggest in this case is to save the output of the command to a log file and then check the output for more information.

      It would look something like this:

      • cd /path/to/your/directory && /usr/bin/php script.php >> /path/to/log_file.txt 2>&1

      The output should give you more information on why the command could be failing.

      Let me know how it goes!
      Regards,
      Bobby

      • Hey Bobby,

        I’m already logging the output into a file in /tmp/.
        Let me give you some info about the script I’m running and what the problem is.
        I have a script that pulls data from a broker..this broker only offers 1m, 5m, 1h and 1d period candles, so if I want to have 15m candles I have to pull 3 x 5m candles and “build” de 15m candles myself…right?
        So then when I execute the script manually it works perfectly, but somehow when cron runs it, the results change, as in the values of the candles change…
        What do you make of this?

        • Hi there @fabriziocastrob6,

          This is quite interesting, is there anything in the output of the script which could point you to any direction?

          There are a couple of things that come to my mind:

          • Check the environment variables and make sure that there are no important variables missing, you can do that with the printenv command directly in your terminal and then setup a cronjob to run once and log the output of the printenv command

          • Similar to the above approach, check the output of the php -i and check the values and make sure that there are no major differences

          • What is the user that you run the command as in your terminal? Does it match the one that you run the crontab as?

          • Do you use sudo when running the script manually?

          Let me know how it goes!
          Regards,
          Bobby

          • Hey @bobbyiliev

            The output I get belongs to a 5m candle when using cron, but when I do manual it gives me the 15m candle value as its supposed to.

            Im logged in as root to my ubuntu droplet and no need to use sudo to execute the script manually.

            I will try comparing the outputs of printenv and php -i and get back to you..

            thanks!

          • Hi there @fabriziocastrob6,

            I see, how often is your cronjob set to run? Have you tried setting it to a longer period of time like 1 hour or so? That way it does not overwrite the content of the file which you keep the results in.

            Let me know how it goes!

            Regards,
            Bobby

          • hey @bobbyiliev

            Any ideas as to what I should be looking for here?
            Im still a noob.. I could show you the output of diff for all 4 files in a pastebin maybe?
            There are some differences indeed.

          • Hi there,

            Yes, feel free to share the output of the files and I could try to have a look as well and see if I can spot anything that could be causing the issue.

            Regards,
            Bobby

          • hey @bobbyiliev

            this is the output for diff printenv printenv(cron) and diff php-i php-i(cron)

            [https://pastebin.com/huiF8XUn]

            The crontab needs to run every 15m

          • Hi there @fabriziocastrob6,

            One thing that I’ve noticed at a first glance is that you are using bash as your shell but for the cronjob the Shell is set to sh.

            What you could try doing is defining the following on top of your crontab:

            SHELL=/bin/bash
            

            Then let the cron run as normal.

            Let me know how it goes!
            Regards,
            Bobby

          • hey @bobbyiliev

            I tried the SHELL=/bin/bash on top of the crontab but that didn’t work either.

            Also tried making a bash script file, running it and I get the same output..

          • Hi there @fabriziocastrob6,

            Regarding the bash script, I would recommend running it like this:

            • First make the script executable:
            • chmod +x /full/path/to/script.sh
            • Then run the script by providing the full path to the script like this:
            • /full/path/to/script.sh

            Regarding the PHP issue, what I could suggest is maybe adding some debug messages in the script itself and logging those messages to the log file so that you could see where the problem could be coming from.

            Let me know how it goes.
            Regards,
            Bobby

          • hey @bobbyiliev

            Yeah the bash script works alright but it still outputs a different result.

            I still can’t get it to work as its supposed to, no idea why it works manually and not with cron.

            I’m giving up on this for now.

            Anyway thanks for your help!

            best

          • Hey @bobbyiliev

            I’ve now switched the cron to be every 1 hour, but still have the same issue.. I think it could be the time. If I wait 1 minute after every hour before executing the script with cron it gives me the result its supposed to (the data changes every period, so I was getting the “old” data from last period instead). I think the clock on the server may be a bit early? How could I look into this?

            Thanks

          • Hi there @fabriziocastrob6,

            Yes indeed this might be the case, you can check the time by running this command here:

            • date

            Also, do you write to the same file everytime you run the cron job? What I could suggest is using a temp file for every run, so that you don’t have multiple job writing to the same file, but files for every run.

            Let me know how it goes!
            Regards,
            Bobby

          • @bobbyiliev output from date seems to be the same as on my pc… for now I’m waiting 1m longer before executing the script with cron, but I hope I can fix this eventually…

            I’m appending the output to the same file so I can always check previous results

Submit an Answer