call flock with node.js?
I have cron job to run node.js scripts.
Want to use flock to lock a file to make sure 开发者_JS百科my cron jobs are not overlapped.
Any good module for doing file locking ?
Or I should call that in child process ?
Or I should not do any file locking ?
Sorry, I am new to this and not sure file locking is good for async env like node. Thanks
If you're just trying to keep cron jobs from overlapping, consider using the "flock" utility in your crontab instead.
If your cron line looks something like this:
*/10 * * * * /usr/bin/node /usr/local/share/myscript
You can just change it to this:
*/10 * * * * /usr/bin/flock -n /var/lock/myscript /usr/bin/node /usr/local/share/myscript
This will try to get the lock on the lockfile /var/lock/myscript. If it can, it will run the command on the rest of the line and then release the lock; if not (because there's another job running), it will fail.
This keeps you from having to add a lot of dependencies on 'fs-ext' and so on.
There's more information at http://linux.die.net/man/1/flock
See flock
function in fs-ext
package: https://github.com/baudehlo/node-fs-ext
精彩评论