Call flock with node.js?

I have a cron job to run node.js. scripts

Want to use the herd to lock the file to make sure my cron jobs are not overlapping.

Any good module to block files?

Or should I call it in a child process?

Or should I not lock files?

Sorry, I'm new to this and not sure if file locking is good for async env like node. Thanks

+6
source share
2 answers

See the flock function in the fs-ext package: https://github.com/baudehlo/node-fs-ext

+2
source

If you're just trying to keep cron jobs from overlapping, consider using the flock utility in your crontab.

If your cron line looks something like this:

 */10 * * * * /usr/bin/node /usr/local/share/myscript 

You can simply 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 file lockfile / var / lock / myscript. If possible, he will run the command on the rest of the line, and then release the lock; if not (because other work is being done there), this will not work.

This prevents you from adding a lot of dependencies on "fs-ext", etc.

More information at http://linux.die.net/man/1/flock

+4
source

Source: https://habr.com/ru/post/885711/


All Articles