Vijay
Vijay

Reputation: 5433

How to run a php program continuously in the server?

i want to run a php program constantly in the server which would download some things and store them..

I was thinking of creating a script and running that in cron..

But i wonder is there any other simple method or light weight component which takes less memory in the server while running continuously ??

I thought of another simple thing,

creating a php script with infinite max_execution time and running the code inside a while(true) loop (indefinite loop) with some sleep and then starting the program using php..

Which would be the better available method to do this?

Upvotes: 3

Views: 5617

Answers (6)

joza
joza

Reputation: 1

IF you are just downloading stuff from the internet, wouldn't it be wiser to go with something like wget? On the other hand, if you are crawling/web scraping i guess doing a cronjob would be better

Upvotes: 0

Kendall Hopkins
Kendall Hopkins

Reputation: 44124

I've created a library called LooPHP which helps abstract out all the while(1) craziness and provides the ability to add event sources (more or less it's a basic runloop you'd fine in GLib, Cocoa, node.js). There's a few of examples which can let you know if you like the style of writing (event based vs loop driven).

I'd suggest reading some of the other questions with PHP and daemon tags. Here is a few of my answers.

But honestly CRON is fairly lightweight and unless it needs to be real-time (or whatever that means to you) writing PHP into a daemon is more work than it's work.

Upvotes: 3

leepowers
leepowers

Reputation: 38318

You can daemonize a PHP script just as you can create Perl or Python daemon. Starting and stopping the script is operating-system dependent. There's a PEAR class System_Daemon that helps with writing a PHP daemon.

Upvotes: 0

timdream
timdream

Reputation: 5922

  1. Why not SSH into the machine and run php in the console?
  2. If not, do popen() to invoke php-cli in your php script.
  3. Use corntab for programs that need to be run periodically -- a never finish script is never a good thing.

Upvotes: 0

nonopolarity
nonopolarity

Reputation: 151214

using a cron job is pretty good... if you use a script, won't you have to invoke it using a browser, or invoke it inside of a shell every time? If it is to download things, you can set it to run every hour or something like that. If you use it as a script on the webserver, you have the risk of running it already and then invoking it again so there are two instances running at the same time.

Upvotes: 0

alex
alex

Reputation: 490597

Run a cron - that is what they are there for.

Your idea of looping while(TRUE) and sleep()ing is flaky and probably not as clear to any other developers then running a cron job.

Upvotes: 1

Related Questions