views:

329

answers:

6

I am working on a site that require a php script running on a server without any request, it is a bot script that keeps (not full time but at least once a day) checking client accounts and send alert messages to clients when something happens.

any ideas are appreciated.

+2  A: 

Use a cron job to do it http://www.cronjobs.org/

You can automatically call a script at any interval you like indefinitely. Your hosting provider should support them if they are good.

You should also consider putting a unique key on the end of the page

ie. www.yoursite.com/cronjob.php?key=randomstring

and then only run the script if the key is correct, to prevent bots and other users from running the script when you don't want it run.

Evernoob
I think that to use a cron job I have to get access to the server which I have no way to access it so I need to do it all in php
Ayoub
Since PHP is a scripting language, you can't write code to get it to automatically run at intervals. It needs to be called in some way and the best way to call it is to run Cron.
Evernoob
@Evernoob - "Since PHP is a scripting language" - so you can do that in "C" for example? How's that?:)
Quamis
+7  A: 

Assuming you need to do this on linux, you may run any php script from the browser and from the CLI as well.

You may run a simple php script: <? echo "Ana are mere"; ?>

like this: php -f ./index.php

Be careful about file-permissions, and any bug that may creep inside your code, memory leaks or unallocated variables will become VERY visible now, as the process will run continuously.

If you dont want it running in the background all the time, take a look at crontab (http://unixgeeks.org/security/newbie/unix/cron-1.html) to be able to start jobs regularly.

-- edit--

take a look at http://stackoverflow.com/questions/45953/php-execute-a-background-process and http://stackoverflow.com/questions/122834/php-how-to-return-information-to-a-waiting-script-and-continue-processing

Basically you want to start a background process, and you may do this by either using exec() or fsockopen() or a file_get_contents() on your own script probably in this order, if don't have access to exec, or socket functions. Also take a look at http://us2.php.net/manual/en/function.session-write-close.php so the "background script" won't "block" the request and http://us2.php.net/manual/en/function.ignore-user-abort.php

Quamis
+1  A: 

If you can't create a cron job, then create a page that does what you want and create a scheduled task on another machine (maybe your PC?) that just goes out and hits that page at a certain time every day.

It's really a hack, but if you absolutely can't set up a cron job, it would be an option.

Eric Petroelje
+1  A: 

As Evernoob and Quamis said, you want to have a cron job (UNIX/Linux/Mac OS) or a scheduled task (MS Windows). Furthermore, you can either have the PHP script run using the PHP command line interface (CLI), in which case you can invoke the PHP executable and then your script name. As an alternate, you can use a tool like wget (availble on all platforms) to invoke the PHP script as if someone had typed the URL in the location bar of a web browser.

Tony Miller
+1  A: 

A php script could not be used like you imagine here. Because it's executed through apache after a request from somewhere.

Even if you do while(1) in your script, apache/php will automaticly stop your script. Responding to your comment, yes you'll need ssh access to do this, except if your web interface allow you to add cronjob.

Maybe you can write a service which can be executed with a program on another server and do the job.

Boris Guéry
+1  A: 

If you have no access to the server the easiest way would probably be to hit it through the browser, but that would require you or an external script hitting the URL at the same interval each day when you wanted it to one. You may also be able to setup a Selenium test suite that runs locally on a schedule and hits the page. I'm not 100% if that's possible with Selenium though, you may need some 3rd-party apps to make it happen.

Something else you could try would be to see about using PHP's Process Control Functions (link). These will let you create a script that is a deamon and runs in the background. You may be able to do this to keep the script running on the server and firing off commands at programmed intervals. You will still need some way to get it running the first time (browser request or via command line) though.

Steven Surowiec