2

I have a database in MySQL which have entries of time. There are more than 1000 entries of time. I want to extract time and run a PHP script exactly at that time..

I have tried to run a PHP script continuously which check the time, but my server does not allow to run the script for more than 60 seconds.

EDIT. I have to check the database every second. Is there any alternative?

4
  • How about running your script via a cron job? Commented Mar 11, 2010 at 19:15
  • Cron job is good idea but I also have to run query to check time from database and current time which will take too much time. Is there any alternate to do so. Commented Mar 11, 2010 at 19:20
  • You can use set_time_limit(0); to make your php script run "forever", however after about 1 day your script will be using more than 1gb of memory. PHP's memory manager is very primitive, if you want to run 1 process for a long time you'll have to use java or write some very anal C++. Commented Mar 11, 2010 at 19:34
  • exactly what i am upto .is there any alternate to do so. my hosting provider will not allow me to do this type of activity. Commented Mar 11, 2010 at 19:43

9 Answers 9

5

Use the pear package, System_Daemon

http://pear.php.net/package/System_Daemon/

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

1 Comment

+1 System_Daemon. AWesome PEAR package, I use it with Gearman Workers
3

Try Unix's cron.

Comments

3

You'll need some external service to execute the script. On a Unix box, that would be cron. On a Windows box, use Task Scheduler.

Comments

1

Have you thought about writing your process as a server daemon. It would start up and run in a while loop forever. Every few minutes or however often you'd like it could check the next x minutes of run times. You queue up your requests and whenever that time comes around you kick off the script you need to run. I don't think cron is what you'd want since you are trying to schedule future events at arbitrary times... And I'm sure it's what you are currently using to try and check the db every second.

2 Comments

yes exactly . I have to check the db every second. is there any alternate solution . because querying again and again will become very cumbersome.
Well you just check every X minutes for every time it needs to run within the next X minutes. So something like: select time from times where time < now() + interval 5 minutes; Run this every 5 minutes and build up a unique queue of times. Check that queue every few seconds or whatever granularity you need to kick off the process whenever it's time has elapsed and remove it from the queue.
0

Write a PHP script to read from the database and add entries to your crontab to make the script run at the desired time

Comments

0

Keeping a process running is not a very good solution, not least because you'll need to ensure it does keep running. Presumably you know when the next occurrence is going to happen - so use the 'atd' to schedule it - when triggered the script should also work when and how to schedule the next job.

This does mean that jobs are chained - and failure of one breaks the chain, also the granularity of most implementations of atd can be rather high.

You might want to look at using a more sophisticated scheduling tool like Nagios or a process monitoring type approach like DJB's daemontools or Oracle's OPMN.

C.

Comments

0

You must use the sleep(arg) function that pauses the PHP script for a given time and then continues.

sleep(50); //Pauses

myFunction(); //Runs after the pause

For example, this pauses the script for 50 seconds.

1 Comment

this solution can work only when the max_execution_time parameter is set to a very high value ( I am not sure what the absolute max value is ..) , and also set_time_limit() function and possibly other variables.in cases where you want the script to run for a whole week or month for example - it is not an efficient solution but it can work in a very delayed loop for short execution times
0

DJB's DAEMONTOOLS is great. So, apparently, is systemd and/or Upstart, though Remnant plays a true wanker.

Comments

0

The recommended Pear package - system_daemon looks great. However, the author now recommends to use new functionality that's readily available in Ubuntu: upstart

See his article on his own blog from 2012

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.