问题
I have a method that runs when I call a specific URL on my application. It processes rows in a database queue. The interval is set as lowest interval possible with Cron, ie. 1 minute. This needs dropping to 30 seconds, so I'm wondering how best to achieve this.
I was thinking I could build a loop into my script that runs the code twice, with a sleep of say 30 seconds in between, but I'm wondering if there's a cleaner way than this.
Also, is there a way of running this method from the command line, without the need to call an actual URL?
回答1:
This answer is strongly inspired by this one
You can leave your code as it is. To call it twice a minute you can add a second cronjob and let one cronjob sleep 30 seconds before executing the task.
* * * * * /path/to/executable param1 param2
* * * * * ( sleep 30 ; /path/to/executable param1 param2 )
This has two advantages
Your code doesn't need to worry about when and how many times it is executed (e.g. if you call it manually it doesn't need to run twice!)
If your code takes 10 seconds to execute everything you would have a delay when using
sleep
inside of your function. This way you don't.
回答2:
As in similar threads it was stated, cron
is not usable for this, at least not directly. IMHO the most sane approach is to write a shell script, that does your task every 30 seconds, then set up a cron
job to check if your script is running, and if not, it should start it.
By the way, a stored procedure would not be a good solution in your case?
来源:https://stackoverflow.com/questions/27042549/running-a-laravel-method-every-30-seconds