Replace CRON

Asked

Viewed 360 times

1

I made a script PHP which has to be executed at all times, usually from 1 to 1 seconds or at most 2 to 2 seconds. The problem is that I have configured several command lines in CRON to run every 1 second, but CRON changes the configuration automatically because I think it overloads the server.

My question is that I would like to put the script to run from X in X seconds without even having any user on the site. It would have a way without using CRON ?

  • Run the PHP by the console infinite loop, with a 2-second Sleep, much simpler.

  • Just one detail: running the console is not that: http://answall.com/a/121742/ - here you are calling the page server with Curl. It’s the same as someone accessing the page. For constant tasks, you have to run PHP through the console. php nomedoscript.php

2 answers

0

The path is the same cron tasks.

Start rethinking whether it really takes 1 in 1 second or if you can change something in the code to be a little longer, after all, there is no one on the site at the moment, an ajax on the client side is acceptable, but cron tasks next to the server one in a second even overloads and many hosts don’t even allow execution, usually the minimum is 1 minute.

  • It’s funny how they raised that flag of viability, I don’t see how information can change 60 times in a minute, poor operator. In a large bank the return of this requisition could be disastrous for the project. And as I said depending on the host they don’t even release, the code will run and then it will be blocked for misuse of resources.

0

The shortest interval that cron accepted is one minute, that is, you cannot use cron to run processes with seconds of difference, directly.

However what you can do is schedule a "trigger" program that runs for a minute, and that program fires the other with seconds interval.

A somewhat more stable solution is to make this "trigger" never stop, schedule the trigger to run on @reboot, and also schedule also to run by normal scheduling, say, every 15 minutes. It has to put some form of monitoring or latch to avoid running in parallel, but this solution ensures that the program "recovers" if something goes wrong and unexpected.


The solution with infinite loop, without reboot, would look something like this: No cron tab:

@reboot "/caminho/do/script/disparador.sh" &

And the file disparador.sh something like that:

#!/bin/bash

while true; do curl "LINK"; sleep 2; done
  • 1

    I had made an infinite loop and at the end of it I put sleep(1). Every 1 minute it ran this, but it started giving error 500 on the entire site.

  • Probably because of the repetition. Runs every minute, infinite loop, so in a few minutes you’ll have hundreds of copies running in parallel, which probably knocks down the server. That’s why the first solution runs only one minute. At least it gives the server chance to release resources.

  • @Alissonacioli the infinite loop is better in these cases, but has to run PHP through the console, not the page server.

  • @Bacco how could I do that ? I’m a layman at this part

  • @Andrélfsbacci and in my case it is a link ? Because I use Codeigniter and if I call only the controller it will not run, I need to call the link, as I would in the case of reboot ?

  • @Alissonacioli instead of you running the curl [endereco], you rotate the php [seu arquivo]

  • @Andrélfsbacci that file disparador.sh that you gave example would be programmed in Cron every 1 minute (* * * * * *) but actually when cron runs it will make requests every 2 seconds ?

  • One has nothing to do with the other. The cron has limitation per minute, can not be faster than this. But the disparador.sh does not have this limitation. It can fire another command every second, every two seconds (as in the example above). What you do is solve the problem in two parts. First you create your PHP that, alone, does what you need to do, second by second. Then you use cron to "just" raise this program. You have to put locks in PHP to avoid rotating two at the same time.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.