Forum Moderators: bakedjake

Message Too Old, No Replies

Execute a PHP Script 100 times

Execute a PHP Script 100 times with 5-minute interval

         

anshul

7:08 am on May 31, 2005 (gmt 0)

10+ Year Member



How could I configure cron job on remote server?
In control panel, they provide fields like 'Minute', 'Hour', 'Day', 'Month', 'Weekday' and 'Command' to fill for a cron job.

I need a cron job for managing e-mail subscriptions.
So, I need execute a .php script 100 or 1000 times per 5-minutes or 10-minutes. So script'll exhaust in some definite time. I don't wanna run it all the time. But definite number of times and with definite intervals. Please help me. I'm trying my first cron.

MattyMoose

4:00 pm on May 31, 2005 (gmt 0)

10+ Year Member



So, I need execute a .php script 100 or 1000 times per 5-minutes or 10-minutes. So script'll exhaust in some definite time. I don't wanna run it all the time. But definite number of times and with definite intervals. Please help me. I'm trying my first cron.

Well, a few things...

Since you don't know how many times it actually needs to run, there's really no point in writing a crontab for this kind of script, witha preset number of times to run. Since, after all, if in one instance it needs to run 100 times, but the number of iterations is a minimum of 1000, then you're going to run a script that doesn't need to run an additional 100 times.

Secondly, I think you'd be able to run this cron at most once before your host comes to you and tells you to stop, if you're running on a shared host. If you're on a dedicated one, then whatever, it's your CPU cycles you're wasting. If you're on a shared host, your script iterating 1000 times over a 5 minute period could potentially annoy the other users and the BOFH.

Additionally, the maximum granularity of cron is per-minute. Meaning that you can at most execute something each minute. The reason for this is simple: There are other ways of doing things than having cron wake up every second and executing something. It'll kill your server pretty quick.

My recommendation would be to look at your php script. There has to be a way to rewrite it so that it doesn't need to be executed that many times in a 5 minute period. Is there a way, for example, to have your web app (or whatever) write the email subscriptions to a plain-text file, and every 5 minutes, your cron will run your PHP script, which reads the plain-text file and fires off emails to those email addresses? There's lots of people willing to help in the PHP forum as well. :)

Looking at Cron was a good place to start looking at a solution, for sure, but I don't think it'd be the best solution for this case.

HTH!
MM

wheel

9:30 pm on May 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On linux, I think 'atd' does something like cron,but on a one up basis, so it might be useful in this case.

anshul

9:18 am on Jun 1, 2005 (gmt 0)

10+ Year Member



>> Since you don't know how many times it actually needs to run, there's really no point in writing a crontab for this kind of script.

To be more specific, I'm using PEAR MAIL/MIME/MAIL_QUEUE.
Yes I've the option to call PHP, until it pulls and sends all e-mails from db. But, it's better if Cron do it all the time, as I may go out and forgot to call script many times in my browser. May be I'm on a vacation or tour.

Please help me.

Burner

4:04 am on Jun 21, 2005 (gmt 0)

10+ Year Member



If I understand you correctly, you need to poll a database up to a thousand times in a 5 minute period? Does each instance of the script running send an email from the database? We would need more details as to what exactly your script is doing to help with code examples.

You mention that it's managing email subscriptions and you should know Majordomo or Mailman handles email subscription tasks much more efficiently than a script that needs to be called that many times in such a short period of time. Majordomo and Mailman are also free and require little admin intervention after they are installed.

Burner

ABliss

9:47 pm on Jun 25, 2005 (gmt 0)

10+ Year Member



*/5 * * * * /path/to/php-bin /path/to/php-file.php

Runs every five minutes. Cron doesn't keep track of how many times its run, so you'll have to do that yourself inside the script.

jamesa

11:47 am on Jun 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't understand why you need to do that (pops a red flag: there may be a better solution). Regardless, here's one way to do what you asked:

Have cron fire off a wrapper script every 5-10 minutes. The wrapper just fires off your script, then sleeps, fires it off again, sleeps, etcetera until it reaches 100 iterations (and/or some other condition is met).

Matt McInvale

5:23 am on Jul 7, 2005 (gmt 0)

10+ Year Member



Have your PHP script loop XXXX amount of times and then just run it every YY minutes from cron.