Search code examples
phpcroncron-task

PHP script executes infinitely when called using Cron


I faced very strange problem on my hosting. I have script and it can be triggered using URL like this

https://mywebsite.com/script.php

I need this script to be executed one time in two days.

So I created Cron job just like my hosting provider advised me.

wget -O /dev/null -q 'https://mywebsite.com/script.php'

It uses wget because script requires some extra scripts - so my hosting provider said that I need to create task like this.

It worked fine for about a month but for few weeks I have a problem. For some reason that I and my hosting provider can't understand when I run script by opening URI in browser - script executed fine (I know it because of emails that are sended in 4 different steps of execution). But when Cron execute this scripts it executes infinitely - so I continue to receive emails for numerous times until I rename script or delete it.

Script execution time is about 2-3 minutes. So when I run it from URL and wait till it finishes - I get error on the screen that time of request (60 sec) is over. But I know that scripts executes fine till the last step.

What is the problem?


Solution

  • Wget

    I had the same problem at some point with a php based cronjob. The Problem was, that wget itself can have a timeout. If this timeout is reached, wget will try again and again.

    Try to use some wget options to make sure it runs as you want it to run. Example:

    wget -O /dev/null --tries=1 --timeout=600 'https://mywebsite.com/script.php'
    
    • --tries tells how many times it will try to execute if a timeout appears.
    • --timeout specifies the max exec. time in seconds. Those options can be specified at cronjob level as well.

    PHP Cronjobs

    If possible it will may be a betther choice to let PHP run your cronjob directly. If you know the servers php directory you could create a cronjob like

    /usr/bin/php /srv/www/yousite/html/script.php
    

    In this case you have no third party programm like wget to rely on. If this helps depends on how the cronjob is built. If your cronjobs uses $_SERVER variables for example, this would not work.

    There are some settings you want to check, before you use any PHP file as cronjob.

    Keep in mind that the php configuration set within the php.ini could also have an impact on unwanted errors on PHP Cronjobs in general. In the php.ini there is a value called "max_execution_time" where the max seconds to process a php request is defined.

    An other setting you might want to get your eye on is the "memory_limit" wich is also defined within the php.ini configuration. This configuration defines the max. memory a php request can use. As your cronjob seems to run for 2-3 minutes, that could mean that maybe a lot of data is stored in memory while you use it.

    Be aware, that any request uses those limits. If you set them to high it may will cause problems with CPU load on your server, or with too many spawned php processes.

    If you have a shared hosting service or something similar, you may not be able to change any of those settings.