0

I am trying to execute a basic script on my server to automatically run a PHP script every 45 seconds through Python:

import os
import time

while(1):                                              
    os.spawnl(os.P_NOWAIT, "twitter_clean.php")
    print "checked twitter"
    time.sleep(45)

The PHP is checking twitter, processing some values and adding them to a DB. The script seems to loop fine, but doesn't seem to be calling/executing the PHP...is os.spawnl(os.P_NOWAIT, "twitter_clean.php") the right command?

Also, once I execute the Python through terminal, will it keep running if I close my SSH session with the server?

alyx
  • 2,593
  • 6
  • 39
  • 64

4 Answers4

2

You should call PHP-Cli passing that file as parameter

import os
import time

while(1):                                              
    os.spawnl(os.P_NOWAIT, "/path/to/php-cli twitter_clean.php")
    print "checked twitter"
    time.sleep(45)

And to keep it running, try to make it an Daemon: How do you create a daemon in Python?

Community
  • 1
  • 1
0xd
  • 1,891
  • 12
  • 18
1

Why not just use crontab for this? Getting it to run every 45 seconds is a little harder, but if you're willing to go to either 1 minute or 30 seconds you should be able to do this much more simply than using a python script.

Jeff Day
  • 3,629
  • 1
  • 18
  • 12
  • Also more reliably. Much more reliably. – rplnt Oct 31 '11 at 13:39
  • Care to share any facts about `Much more reliably`? By facts I mean empirical proof that's not subjective. Otherwise these suggestions aren't worth a dime. – N.B. Oct 31 '11 at 13:48
  • @N.B. First of all, cron will start after a restart. To take care of this problem for the python version you need to put the script to init (or add it to startup cron :)). Another problem could arise with the crash of the script. If it won't be able to access the php executable, create process, access output stream, ... I just managed to crash a pthon instance when calling spawnl with gibberish. And by crash I don't mean that it threw an exception. Another thread of problems could be caused by eating up too much memory (probably not a problem in this case). – rplnt Oct 31 '11 at 13:59
  • @rplnt - it's not difficult to add the script to init.d, and the scripts don't crash out of the blue for no reason, considering they're well written. On the other hand, why would Python script have problems accessing PHP-CLI and crontab wouldn't? As for memory - once more, well written script shouldn't waste memory, and as far as I know Python is very good with memory management. – N.B. Oct 31 '11 at 14:08
  • @N.B. Even the cron tab could have that problem. But it wouldn't matter because it would be run again in a minute. Python would crash and stop until the next boot. And it still adds unnecessary level of complexity to a very simple problem (start a php script). Even if it would do something additional (which it doesn't) I would use cron to start sh/py script as I don't have to handle all the problems that could occur and stop my long-running script. But I agree that well-written script would do the job as well. This one is not the case though... – rplnt Oct 31 '11 at 14:19
1

To leave it running after you close the session use nohup, but if this is anything more than a temporary situation, you'll need to use cron to call the php (as Jeff Day says) or start the Python automatically, try looking at: man chkconfig

Havenless
  • 116
  • 2
0

You could try to solve this with PHP only. Have a look at the "tick" functionality:

http://de.php.net/manual/en/function.register-tick-function.php

http://de.php.net/manual/en/control-structures.declare.php#control-structures.declare.ticks

You could register a tick function. You could write the code about to only execute the function every 45 seconds within the registered function. I think this would be much more easy and you could also implement a functionality much more easy which would prevent executing the function multiple times (if 45 seconds is not enough for executing it).

Than you can use the command "nohup" to start the script as "background"-job easy.

aurora
  • 9,607
  • 7
  • 36
  • 54