0

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?

3
  • Set set_time_limit(60*10) and make sure your cron is not retried after error Commented Sep 21, 2017 at 12:28
  • 1
    Sounds like your script has no ending and the browser only stops because it hits max execution time. Can you show your code? Commented Sep 21, 2017 at 12:30
  • 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 it means there is issue in script. it only stop because of request time out. So you need to show your code Commented Sep 21, 2017 at 12:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

1) should I leave -q in parameters? 2) should I add -0 ?
@moonvader The -O Parameter just specifies where the result will be stored. So i would definitly leave the -O /dev/null, as you do not want the result to be saved. The -q just tells wget to use it in "quiet" mode. Wget will not create any output. so, -O und -q in your specifications just tells the cronjob to produce or save any output.To fully understand and see all possible options for wget you can check the documentation
Great answer! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.