Jump to content

How can I have some code executed every, say, 30 seconds or so?


Recommended Posts

I'm writing a CLI script that serves as a chat server. Most of it is coming together quite nicely now, but I'd like to have a chunk of code executed every 30 seconds to perform some cleanup tasks like removing unused chat rooms, writing user databases to file, and so on.

 

As I understand it, PHP does not have support for multi-threading. Is there any way to do this without blocking the execution of the program?

 

My program has one main loop that loops every time something gets sent to it via socket, so I could do something like check a "last executed" timestamp to see if it's been 30 seconds since the last timed execution and then run my code based on that. The problem with this approach is that it's neither accurate nor reliable, if the server goes for a long while without being sent anything, the commands will not be executed. Is there any other way to do this?

If on UNIX / LINUX look into a CRON job http://en.wikipedia.org/wiki/Cron

 

Yeah I'm running Linux but unfortunately I'm designing this script to be distributed to other users. Doing this would make installing the script much more complex and rule out cross-platform capability (and realistically most of my users will be on Windows).

You can run background apps in windows, however.

There are tools similar to *nix cron on windows as well.

 

But if you can avoid running a background app with an intrepretive language, do so.

as an interpretive language, you need the scripting engine, and php is kinda large.

 

anyways, when your talking about running a task at specific intervals or background tasks, you rule out a lot of os's, as each os may use something different.

so you usually end up writing stub files to handle each one.

 

do you mind using a small bit of javascript? you can use a simple ajax script to call a php script on interval.

 

This is a CLI script, it will not ever be run from a web browser. I don't know of any way to use JS/Ajax from a CLI script.

oh right could you check the time stamp and tell php to call a function if the timestamp is older then the current stamp by 30 seconds  then loop back up and ask again would be a recursive function? i'm not sure if recursion is allowed in php.

  • 2 weeks later...

oh right could you check the time stamp and tell php to call a function if the timestamp is older then the current stamp by 30 seconds  then loop back up and ask again would be a recursive function? i'm not sure if recursion is allowed in php.

 

I believe what you are suggesting is the idea I initially had. The way my script is designed is that it loops forever, and upon each new loop it hangs until it sees socket activity, in which case it processes that activity then finishes the loop, starts another iteration, and hangs until something else happens. The problem the whole timestamp method is that overnight it could very easily happen that no input is received for like 6 hours or longer, so all these events would have to go unexecuted until somebody connects at which point they would fire at once. This is acceptable, just not ideal. It's looking like this is the route I will go unless somebody has other suggestions.

 

is this, cli an app. that is meant to continually run?

it runs until a user request to exit?

Does this task you need done, required only when its running?

 

Yes, it is a CLI app. It is a "chat server" of sorts so it will always be running and just sits there waiting for socket connections when nothing is going on. It will run pretty much continuously unless specifically shut down (I have shutdown procedures implemented but really it will always be running). I have several different tasks that need done and yes they have to be done while the program is running.

Sorry for double post.

 

Look up autoit. It has the things you need.

http://www.autoitscript.com/autoit3/downloads.shtml

 

I can help you with the coding actually.

Or teach you how to make one.

PM me if you're interested.

 

the OP is running linux (see post 3)

how is a windows ap going to help?

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.

Guest
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.