Best way to run script continuously?

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
Ravanol
Forum Newbie
Posts: 3
Joined: Sun Jan 27, 2008 5:56 pm

Best way to run script continuously?

Post by Ravanol »

Hi all,

I have a system where I need to send thousands of small text files to a webserver - all day, every day using HTTP POST (cURL).

I have decided to take the load away from my database and store each XML document within a directory on the server - open file, http post file, delete file. Read file, http post file, delete file....

I wish for this to happen constantly.

I could use a php script - running an infinite loop to "get next file" - read, http post, delete.... but I have a gut feeling that this would be a bad idea because:

1) If a script is in an infinite loop - would there be a huge performance impact on the rest of the scripts running on the server?
2) If I close my web browser - the script will terminate?

So.. what is the best thing to do?

a) Should I have a CRON job that calls the PHP script every minute (with a max execution time of say 55 seconds)?
b) Should I run the php script containing an infinite loop from the command line (which I understand will keep running because it has not been called by a client browser)? if so, how would I terminate the script?

Do you guys have any other ideas?

Many thanks,

AM.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Best way to run script continuously?

Post by Chris Corbyn »

You can run scripts on command line the background them too ;) Adding calls to sleep(5) within your loop for instance can help to prevent consumption of your CPU.

However, yes I would agree that a cron job would suit best :)
Ravanol
Forum Newbie
Posts: 3
Joined: Sun Jan 27, 2008 5:56 pm

Re: Best way to run script continuously?

Post by Ravanol »

Hey Chris,

Cool... so I would type something like : php myscript.php to run from a linux prompt.

How would I stop the script if I needed to?

... this is assuming that i don't go the route of CRON ;-)

cheers. :)

Chris Corbyn wrote:You can run scripts on command line the background them too ;) Adding calls to sleep(5) within your loop for instance can help to prevent consumption of your CPU.

However, yes I would agree that a cron job would suit best :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Best way to run script continuously?

Post by Chris Corbyn »

Well if you typed that exact command it would run until you hit "CTRL + C", or until you close the terminal window. This is because it will be running in the foreground.

Run this:

Code: Select all

php your-script.php &
You'll get a process ID returned, note it down.

Then to end it, just run

Code: Select all

kill <the pid here>
I'm still saying cron would be better however ;)
Ravanol
Forum Newbie
Posts: 3
Joined: Sun Jan 27, 2008 5:56 pm

Re: Best way to run script continuously?

Post by Ravanol »

That's great! Thanks Chris.

I will start with the CRON job - probably setting the max execution time to 5 mins and include the sleep(5) command after - say - each batch of 100 files has been processed or if no files have been found in 100 cycles. I see many hours of trial & error getting these values perfect... will be worth it though :-)

Thanks for your help.

Chris Corbyn wrote:Well if you typed that exact command it would run until you hit "CTRL + C", or until you close the terminal window. This is because it will be running in the foreground.

Run this:

Code: Select all

php your-script.php &
You'll get a process ID returned, note it down.

Then to end it, just run

Code: Select all

kill <the pid here>
I'm still saying cron would be better however ;)
Post Reply