Reputation: 493
I have a php script designed to check a certain folder for xml files and then import the information from each file to a MySQL database.
I would like to set up a cronjob to run every minute so that anytime new files are added they will almost instantly be imported without me having to manually ssh in and run the script.
I have an if statement which checks if files exist and only runs the code if they do, otherwise 'No files' is echoed.
I would like to know if there are any risks to having this run constantly, will excessive resources be taken up? etc
Upvotes: 6
Views: 2828
Reputation: 2283
There's really nothing wrong with running a process every minute ... except the usual pitfalls [which I include with ways to mitigate]. I do want to say that a minute is a really really long time for a modern computer. If you are short of cycles, a few extra system calls per minute is the wrong place to look.
How to solve: make the script grab an exclusive lock to a file. You could write your pid to a file, but that's hacky. If you can't grab the exclusive lock, a previous version is running, so you should just exit.
Here's the PHP interface to flock(): PHP flock()
If something needs to be "done all the time", maybe it should really be "done all the time". You can use the file locking recipe to make sure your script stays up, or you can use something like monit to start it. But you can also just insure it stays up by using cron, and file locking.
Solution: exit after 1000 [or some #] iterations, and then use cron and the file locking model to start a new version [or monit or equivalent].
Upvotes: 5