大网数据广告活动
服务器运维问答

Cron job vs NodeJS setInterval for optimal performance

· 52CE 我爱测

I'm building a simple nodejs script that updates a DNS record based on my current IP. The script works fine but I have some concerns regarding how should I run it. The check for my IP must be performed every 5 minutes and I'm fac…

问题原文

原帖链接 · Meletis · CC BY-SA 3.0

I'm building a simple nodejs script that updates a DNS record based on my current IP. The script works fine but I have some concerns regarding how should I run it. The check for my IP must be performed every 5 minutes and I'm facing a dilemma.

Should I use Node's setInterval or should I create a cron job? Which will consume the least RAM and CPU? Please keep in mind that the script runs on a Raspberry Pi Zero with 512MB ram and only 1 core.

I know that the cron seems a better option but how much better is it? Does it matter given my specs?

已采纳解答

原帖链接 · chicks · CC BY-SA 3.0

Advantages of cron solution

  • less memory consumption for 90% of the time
  • memory leaks are effectively eliminted
  • the code is reloading all libraries every time so updates take effect on the next run without additional complexity

Advantages of daemon solution

  • load time only happens once so disk I/O and CPU are lower for subsequent runs since you won't be parsing your source code or pulling in all of the libraries
  • the lower CPU and disk I/O for subsequent runs also means that there is more of those resources available for whatever your main application for the Raspberry may be.
  • Linux should swap out the memory you're not using. This could be really slow depending on your storage though.

Conclusion

I agree with Alexander T that cron seems likely to be better, but if your main app is sensitive to background processes it might be better to go with the daemon.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-06-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。