Help with writing a CRON job

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ibizconsultants
Forum Commoner
Posts: 35
Joined: Tue Sep 07, 2004 12:07 pm

Help with writing a CRON job

Post by ibizconsultants »

Hello All,

Can anyone help me in writing a CRON job that requires to be running 24/7 to send out mail alerts. Can you also help me with best practicies that will help me write this job better. Also how do I ensure that the PHP script does not time out before all my records in the CRON are processed? (I know that I can use the ignore_user_abort(true); and use set_time_limit(30) inside a while loop for processing individual records... but is this the correct method)

The basic idea behind the script is that the site pulls data from a source via HTTP connection in every 15 minutes and matches the data against a list of customers that wish to receive an alert via mail for the data received.... I am expecting the customer base to reach around 3000 by the end of this year.... And the second script reads the mail content from the database any sends emails.

The website is currently on Windows Platform, soon to be shifted to Linux (Linux is the best)

Any suggestions :)

iBizConsultants
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this may be one of the best threads we have on cron: viewtopic.php?t=2737

As for changing the timeout limits, I'd suggest having the script only do enough work such that the emails are sent out below the server limit. Your host likely has a restriction as to the number of emails that can be sent in a given hour. If you're expecting the site to get popular I personally wouldn't allow the script to exceed about half that limit if emails could be fired on demand by a user. If it can't, then 80% is about as high as I'd go, just to be safe.
ibizconsultants
Forum Commoner
Posts: 35
Joined: Tue Sep 07, 2004 12:07 pm

Post by ibizconsultants »

Hi Feyd,

Thank you for that link.... the example below really worked...

Code: Select all

#!/path/to/php
<?php
//This should be run as a cgi, so that it will not tie up a web server process
define("DAEMON_WAIT_MINUTES",25);

while(! $daemon_finished){
  // Give the code within this while loop 1 minute to parse per pass.
  set_time_limit((DAEMON_WAIT_MINUTES + 1 ) * 60);

  // Do the guts of each pass.
  print("Do Stuff");

  // Tell the daemon to wait for specified number of minutes before it does
  // the while loop again.
  sleep(DAEMON_WAIT_MINUTES * 60);
}
?>
Now my question is that this becomes a demon program that will run 24/7 on the server....

1. Will this occupy a lot of system resources (My script will connect to mysql and send mailers - every 15 minutes)
2. How do you make this run as a service (I am aware of the at command, is there another way)
3. If this gets registered as a service, how do you make it stop... is kill the only way in which you could stop it?

iBizConsultants
Post Reply