Jump to content

How long can a program sleep()


Recommended Posts

Are there any practical limits to how long the sleep() function can be set to halt an application?

I’m using Yahoo Small Business as for web hosting and would like to set some applications to sleep for as long as 12 hours.  Is this likely to be a resource problem Yahoo will ding me for?





Link to comment
Share on other sites

There isn't much on their website as to what you can or cannot do technically. Mostly just legal butt-covering.

They provide a toll-free number to call for help. I'd suggest you call and see what they say on the matter.


Besides that, I wouldn't do it. Even on my own machines.

Regardless of whether it can or can't, PHP wasn't designed to run for long periods. There are issues with memory leaks (the longer your script runs the more memory it sucks up on the machine) and garbage collection (might hold a claim to memory that it doesn't actually need anymore), for starters.

The real question is what kinds of things you need to run - and not even that, just sit around idly - for hours and hours on end. So what's up with that?

Link to comment
Share on other sites

If it's something that is to be done once or twice a day or any timed manner, then a cron job would be better.



If using cpanel



or from command line using editor such as vim




Link to comment
Share on other sites

Sure appreciate the heads up about what to look for to schedule task.


Looks like Yahoo Small Business does not offer cron or cpanel.  They probably don't have anything else for task management that I can have access to.  At least I haven't found anyone on their help line with answers.


Guess I'll have to see if sleep() can be made to do what I need.  The application I have in mind is one that fills the role of a traditional answering service for a doctor taking after hour calls that in essence tracks down the doc when needed by calling various phones and building a call logic that at intervals based on what is learned from people answering the phones and punching in what they know figures out the best way to find the person.  Sometime the person being hunted can be at any one 5 or more locations, out of cell phone and beeper contant and maybe even dead asleep.  Dozing off is common for emergency workers at 3am on slow mornings (think air trafic controllers at lonely airports).  So it also becomes the alarm clock in some cases.  Well, that's what docs pay answering services to do and seemed like it could be automated using some PHP and cute services at twilio.com.


By the way, all that Yahoo Small Business offers by way of PHP functions is listed below.


Array functions

BCMath Arbitrary Precision Mathematics functions

Calendar functions

Character type functions

Class/Object functions

CURL, Client URL Library functions

Database (dbm-style) abstraction layer functions

Date and Time functions

Directory functions

DOM functions

DOM XML functions

Error Handling and Logging functions

Exif functions

Filesystem functions

Function Handling functions

HTTP functions

iconv functions

Image functions

JSON functions

Mail functions

Mathematical functions

Mcrypt Encryption functions

Mhash functions

Mimetype functions

Ming functions for Flash

Miscellaneous functions

Multibyte String functions

MySQL functions

Network functions (fsockopen and pfsockopen are disabled)

Object property and method call overloading

OpenSSL functions

Output Control functions

PHP options & information

Pspell functions

Regular Expression functions (Perl-compatible)

Regular Expression functions (POSIX Extended)

Session handling functions

Stream functions

String functions

Tokenizer functions

URL functions

Variable handling functions

WDDX functions

XML parser functions

XML-RPC functions

XSLT functions

Zip File functions (read-only access)

Zlib Compression functions


Link to comment
Share on other sites

Then another suggestion would be to make a script that cycles each of your customers in loops, and do a select to mysql or even sqlite going by date, basically a "last time date checked".


If last date checked less than 12 hours, run another script to do any actions.

At the same time update the last date checked to the current time.


sleep() is not good to do unless for a very small amount and only when absolutely necessary.

Link to comment
Share on other sites

I would expect Yahoo to cruelly kill your processes in their sleep.  But it doesn't hurt to try.


One option, assuming you have a pc on 24/7 and a reliable internet connection, is to run something on your pc that regularly hits a url on your webserver.  Or you can run a cron job on a hosting service that DOES allow cron jobs, and have it poll your Yahoo site.  That's a bit roundabout but it can work.


QuickOldCar's idea is good - the database can remember what's been done already, and your script's strategy can be "Check what needs doing in the database, do it, then update the database and exit".

Link to comment
Share on other sites

what you should do, buy a cheap $2 webhosting that supports cron, run the cron job for every 2 seconds or 3 seconds


the php file would simply be to check if there are any queued calls... then dispatch them, then wait to be activated again..


whichever company you work for wouldn't have a problem with paying the extra $24/year for this to work out..


thats the easiest sollution, aswell as most practical.


however, if you want to use sleep(), for over 12 hours, you're most likely gonna want to double check with Yahoo! Small Business if its okay

Link to comment
Share on other sites

Sounds like the best suggestion is to find a more accomodating web hosting service.  Till recently all I wrote were desktop applications and now it seem I'm finding more interesting uses for web applications almost every day.  Guess I need a web hosting service that gives me more opportunity to explore and try new things.  Anyone have experience with a web hosting service that meets that goal?





Link to comment
Share on other sites

run the cron job for every 2 seconds or 3 seconds

The minimum interval for a cronjob is 1 minute.




yes, I know, but that means 1 script is being ran every (24*60*60) times per day, to do something that doesn't need to be completely automatic :) 3 seconds is perfectly fine I thought, but you could run it every 1 second for sure..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.