I have cron job to run node.js scripts.
Want to use flock to lock a file to make sure 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With