Three ways of Running a continuous NodeJS Application on Your Se

  • 时间:2020-09-09 13:08:38
  • 分类:网络文摘
  • 阅读:113 次

Let’s say, I have an application that is written in NodeJs, that process the latest blocks of the steem blockchain and write data into a database. I have three ways of running it on my server.

Using Screen Command

As you probably know, when you are disconnected from a SSH session, the programs that are launched in the session will be terminated. There is any easy way to fix this. You can start a session by using command screen -S name before you run any long-running program e.g. system updates or a program that needs to run forever.

In case the SSH session is disconnected for some reasons e.g. network connectivity issues, the programs will be still running. And you can use command screen -r name to re-connect to the session. You can screen -ls to list the current sessions, and screen -d name to detach a session. You can exit the current screen session using the short-cut Ctrl+A,D which will set the session status to detached.

The advantages of using this method are:

  • you can easily monitor the application (by seeing its output)
  • you can easily terminate or restart the application e.g. Ctrl + C
  • the application runs continuously, meaning that you can keep up to the latest blocks and provide a less than 3 second sync time.

Some disadvantages:

  • If the application exists abnormaly, it is not restarted automatically – thus you would need to add the error-handling in your code.
  • Long-running code may tend to have more problems: e.g. memory-leaks, crash at some point.

Putting it in Crontab

Another way is to run your program in crontab. However, the finest granularity is every minute i.e. you can specify your application to run every minute.

Advantages are:

  • You probably don’t need to do error-handling. In case it fails, it fails. You can just wait for next minute (restart automatically).
  • Not to worry about memory leaks. It is less likely to go wrong due to memory issue.

Disadvantages:

  • Lose the ability to sync real-time with the blocks. For example, you will be 60 seconds behind the real-time blocks.
  • Not easy to monitor the output (you can redirect the errors to files though)

Using a Process Manager

You can combine the both advantages using a Process Manager. For example, if it is a NodeJs application, you can use pm2 utility.

Some Advantages of using pm2 are:

  • automatically restart your application in case it crashes or some conditions are met e.g. memory constraints, age.
  • easy to monitor the output of each application e.g. you can pm2 show app
  • easy to terminate pm2 delete app or restart pm2 restart app
  • the application runs continuously
  • error-handling is optional – but considered a good practise not to let pm2 restart your app.
pm2-process-manager-for-node-js Three ways of Running a continuous NodeJS Application on Your Server linux

pm2-process-manager-for-node-js

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
数学题:一艘船在静水中每小时行驶40千米  数学题:在AB两点之间等距离安装路灯  数学题:一种商品随季节出售  数学题:一个底面半径是6厘米的圆柱形玻璃器皿里装有一部分水  数学题:已知点D、E、F分别是BC、AD、BE上的中点  数学题:21个同学来取水果  数学题:四一班买了30只红气球和黄气球装点教室  数学题:三组都参加的有多少人  数学题:一个梯形,如果底延长6厘米  数学题:五星村计划由10名工人16天修一条道路 
评论列表
添加评论