A program that constantly runs and check the database.

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
safmaya
Forum Newbie
Posts: 5
Joined: Sun Mar 07, 2010 9:23 am

A program that constantly runs and check the database.

Post by safmaya »

Hi Everyone,

I am working on a final year project that involves a website that constantly checks the database and sending emails to the user if a condition is met.

The problem is I have no idea how I should write the code or even where to put the code, this project should be able to check the database regardless of the user being logged in.


any help would be appreciated.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: A program that constantly runs and check the database.

Post by Weirdan »

Create a command-line script and put it into the crontab. That's the simplest option.
safmaya
Forum Newbie
Posts: 5
Joined: Sun Mar 07, 2010 9:23 am

Re: A program that constantly runs and check the database.

Post by safmaya »

Hi,

Thank you for the solution, but I have to run the server on a windows machine, so use of crontab is not allowed.

tnx
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: A program that constantly runs and check the database.

Post by Griven »

There are Windows programs that simulate Linux's cron, although I have yet to try any of them.

Try this:
1. Make sure that you can run PHP scripts from the command line
2. Write a PHP script that checks the DB and sends emails just like how you describe, and save it somewhere
3. Create a scheduled task using Windows' built-in Task Scheduler that calls up the PHP executable with the script you wrote as an argument

I've never tried that before, but I think it may work.
safmaya
Forum Newbie
Posts: 5
Joined: Sun Mar 07, 2010 9:23 am

Re: A program that constantly runs and check the database.

Post by safmaya »

Thanks for your response, maybe if I explain exactly what I need might help clarifying the problem, I have a database of users that have entered their event into so that they can be notified on the specified time, so basically the job of the server is to constantly look at the database and send emails to the users or invoke skype to send text messges depending on the user's choice.

My gut's tells me not to rely on windwos scheduler, I am looking for a more robust method to do this, of course if such method doens't exist, your option seems reasonable.

Tnx
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: A program that constantly runs and check the database.

Post by John Cartwright »

safmaya wrote:Thanks for your response, maybe if I explain exactly what I need might help clarifying the problem, I have a database of users that have entered their event into so that they can be notified on the specified time, so basically the job of the server is to constantly look at the database and send emails to the users or invoke skype to send text messges depending on the user's choice.

My gut's tells me not to rely on windwos scheduler, I am looking for a more robust method to do this, of course if such method doens't exist, your option seems reasonable.

Tnx
The alternative is to have an inifitely looped PHP script that sleeps for smaller intervals.

Code: Select all

while (1) {
   //do stuff
 
   sleep(5); //sleep for 5 seconds
}
However, these types of daemons aren't really suited for PHP for scalability and performance reasons. I.e., if you need to trigger many events, a threaded environment is more suitable (C#, etc).
safmaya
Forum Newbie
Posts: 5
Joined: Sun Mar 07, 2010 9:23 am

Re: A program that constantly runs and check the database.

Post by safmaya »

John Cartwright wrote:
safmaya wrote:Thanks for your response, maybe if I explain exactly what I need might help clarifying the problem, I have a database of users that have entered their event into so that they can be notified on the specified time, so basically the job of the server is to constantly look at the database and send emails to the users or invoke skype to send text messges depending on the user's choice.

My gut's tells me not to rely on windwos scheduler, I am looking for a more robust method to do this, of course if such method doens't exist, your option seems reasonable.

Tnx
The alternative is to have an inifitely looped PHP script that sleeps for smaller intervals.

Code: Select all

while (1) {
   //do stuff
 
   sleep(5); //sleep for 5 seconds
}
However, these types of daemons aren't really suited for PHP for scalability and performance reasons. I.e., if you need to trigger many events, a threaded environment is more suitable (C#, etc).

Hi,

Your solution seems very interesting, I doubt that it will cause any problems in terms of performance as I only expect it to run a mail script and trigger skype api, what do you think?

also a very supid question is where do I put this file(script) if I put it in the server is it gonna be executed or do I need to configure anything on my server can you please shed some light on this as I am very new to the web based applications.

Thank you.
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: A program that constantly runs and check the database.

Post by Griven »

If you use a script that sleeps for a short period, like John described, then you will have to have it launched on the server at all times either through a browser window, or via a PHP command line script. The reason for this is that PHP is only compiled at runtime, so for it to do anything, the script needs to be active.

You've got a few options here (script with a short wait, windows task scheduler, compiled language daemon), so I recommend that you try each one (probably in the order listed) and see what works best for you.
safmaya
Forum Newbie
Posts: 5
Joined: Sun Mar 07, 2010 9:23 am

Re: A program that constantly runs and check the database.

Post by safmaya »

Thanks All, I managed to use the Windows vista (7) task schdualer to run the script every minute, bear in mind that you cannot do this in windows XP!

The problem is solved.
Post Reply