Web Hosting Talk







View Full Version : Run a script every second?


Alan - Vox
12-02-2001, 06:11 PM
Does anyone know if its possible to run a script every second? as far as i know with cron you can only do it ever minute.

cperciva
12-02-2001, 06:38 PM
#!/usr/bin/perl
while(1) {if(fork()==0) {exec '/path/to/script';} else {sleep(1);}}

bobcares
12-03-2001, 10:38 AM
Originally posted by cperciva

#!/usr/bin/perl
while(1) {if(fork()==0) {exec '/path/to/script';} else {sleep(1);}}



Cool. That is lovely.
BTW you can also write a shell script for the same purpose.

Have a great day :)

regards
amar

Varun Shoor
12-03-2001, 01:59 PM
The same can be achieved via PHP using the following code:

#!/usr/bin/php
<?php
for ($i = 0; $i = 1; $i++) {
// Do your stuff here
sleep(1);
}
?>


Also bobcares, I have a feeling you are trying to up your post count :(

bobcares
12-03-2001, 02:41 PM
Hi varun,
Can you tell me the benifits of a higher count... If I get some benifit out of it.... it would be cool.... :)
The only benifit is that in the last few days I have made many cool friends around the world who think alike..... That's a real nice thing...... :)

Have a great day :)

regards
amar

cperciva
12-03-2001, 02:55 PM
Originally posted by VarunShoor
The same can be achieved via PHP using the following code:


No it can't. Think about what happens if your code takes 0.5 seconds to run.

Varun Shoor
12-03-2001, 03:23 PM
cperciva,
It doesnt really bog the system down because after the for loop it will do a sleep for 1 seconds, this is the exact way web://cp executes its commands and I havent seen any problem with it.

bobcares,
err.. no benefits.. but still I have seen you make some posts with irrelevant information, it seems like you just like to post too much, hehe :D

bobcares
12-03-2001, 03:32 PM
Varun,
If I reply to this post it becomes an irrelevent post.... ;)
Anyway, I'm unable to access your site
http://www.kayako.com .

Any ideas....

Have a great day :)

regards
amar

cperciva
12-03-2001, 03:45 PM
Originally posted by VarunShoor
It doesnt really bog the system down because after the for loop it will do a sleep for 1 seconds, this is the exact way web://cp executes its commands and I havent seen any problem with it.


I didn't say that it bogs down the system. I said that it doesn't work.

If your code takes 0.5 seconds to run, then on each pass through the loop it runs for 0.5 seconds, then sleeps for 1 second, meaning that it is running every 1.5 seconds.

Varun Shoor
12-03-2001, 04:14 PM
oh, thats true.. sorry i didn't understand what you meant first :)

Alan - Vox
12-03-2001, 05:39 PM
When i use that perl code it creates a whole load of zombie processes

cperciva
12-03-2001, 06:04 PM
Ok, I'm an idiot, I forgot that perl doesn't auto-reap its children.
Add the line local $SIG{CHLD}='IGNORE'; and it should work.

Alan - Vox
12-03-2001, 06:08 PM
add that in the loop?

cperciva
12-03-2001, 06:19 PM
Originally posted by SplashHost.com
add that in the loop?

No, you only need that once. Thus
#!/usr/bin/perl
local $SIG{CHLD}='IGNORE';
while(1) {if(fork()==0) {exec '/path/to/script';} else {sleep(1);}}


or of course from a command line,
/usr/bin/perl -e 'local $SIG{CHLD}="IGNORE";while(1){if(!fork){exec {$ARGV[0]} @ARGV;}else{sleep(1);}}' /path/to/script -and -its -arguments