用node.js调用flock?
我有 cron 作业来运行 node.js 脚本。
想要使用集群锁定文件以确保我的 cron 作业不被锁定 重叠。
有什么好的模块可以进行文件锁定吗?
或者我应该在子进程中调用它?
或者我不应该做任何文件锁定?
抱歉,我是新手,不确定文件锁定是否适合异步 env 类似节点。谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只是想防止 cron 作业重叠,请考虑在 crontab 中使用“flock”实用程序。
如果您的 cron 行看起来像这样:
您可以将其更改为:
这将尝试获取锁定文件 /var/lock/myscript 上的锁定。如果可以,它将在该行的其余部分运行该命令,然后释放锁定;如果没有(因为有另一个作业正在运行),它将失败。
这使您不必添加大量对“fs-ext”等的依赖项。
有关更多信息,请访问 http://linux.die.net/man/1/flock
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:
You can just change it to this:
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
请参阅
fs-ext
包中的flock
函数: https://github.com/baudehlo/node-fs-extSee
flock
function infs-ext
package: https://github.com/baudehlo/node-fs-ext还有一个用于锁定文件的数据包 fcf-framework-lock: https://www.npmjs.com/package/fcf-framework-lock" rel="nofollow noreferrer">https://www.npmjs.com/package/fcf-framework-lock" npmjs.com/package/fcf-framework-lock
包功能描述页面: https://fcf-framework.tech/packages/fcf-framework-lock
There is also a packet fcf-framework-lock for locking files: https://www.npmjs.com/package/fcf-framework-lock
Package feature description page: https://fcf-framework.tech/packages/fcf-framework-lock