开发者

Sleep function on php

开发者 https://www.devze.com 2023-01-25 07:43 出处:网络
As a possible alternative to using cron jobs, I found the sleep function. I have never used this before.

As a possible alternative to using cron jobs, I found the sleep function. I have never used this before.

If I tell my script to run inside a kind of loop, and inside that loop I have an instruction like this

# sleeps for 86400 seconds or one day
sleep(86400);

will my script be launched again after 1 day? even if don't access it on my web browser again within that period开发者_StackOverflow? I think is not possible, but I'm here to ask an expert about it.


The main problem with using PHP this way is, in my experience, not web server timeouts (there are ways to handle that with difficulty varying on the server and the platform) but memory leaks.

Straightforward PHP code tends to leak a lot of memory; most of the scripts I wrote were able to do hundreds of times as many work after I did some analysis and placed some unsets. And I was never able to prevent all the leaks this way. I'm also told there are memory leaks in the standard library, which, if true, makes it impossible to write daemons that would run for a long time in loops.


The script will timeout. You need to set it so that it won't timeout using set_time_limit.


I wouldn't do this I would either use a cron (that is a link) job if it is a regular task or an at (that is a link) job if the job is added at the run time of your script.

cron allows you to run a recurring job every day at 1pm for example whereas at allows you to schedule a job to run once for now +1day for example.

I have written a PHP 5.3 wrapper for the at queue if you choose to go down that route. It is available on GitHub https://github.com/treffynnon/PHP-at-Job-Queue-Wrapper


There is also time_sleep_until(). Maybe more useful to wake up on a specific time...


If you access the script through a web browser, it will be terminated after 30 seconds.

If you start the PHP script on the command line, this could work.


It would work, but your "startup time" will be subject to drift. Let's say your job takes 10 seconds to run, then sleeps 86400, runs another 10, sleeps 86400, etc.. You start it exactly at midnight on day 1. On Day 2 it'll run at 12:00:10am, on day 3 it's 12:00:20am, etc...

You can do some fancy math internally to figure out how long the run took, and subtract that from the next sleep call, but at the point, why not use cron? With cron the script will exit after each run, cleaning up memory and resources used. With your sleep method, you'll have to be VERY careful that you're not leaking resources somewhere, or things will eventually grind to a halt.


I had a similar problem before and found a php cron parsing class that will allow you to execute php similar to running crons. You can tie it to a commonly accessed script on your site if you don't have access to run crons directly.

I actually use this script as part of a larger cron job script:

  1. a cron job runs every hour
  2. an xml file for each sub-cron with a cron-like time component(i.e.- * */2 * * * php /home..)
  3. the sub-cron script that will run if the current time meets the criteria of the sub-cron time component
  4. a user interface is setup so that I don't have to manually add/remove sub-crons from the main cron

The cronParser class is here.


Many correct answers, but: Using sleep() means your script keeps running, and keeps using memory. Raising the default timeout of 30s will work, but again, this is bad idea. I suggest you use crontasks.


This is why legitimate cron jobs were invented. Just use crontab. Using a PHP script to do it will be EXTRAORDINARILY unreliable, buggy, and poorly timed.

Hope this is insightful.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号