Three ways of Running a continuous NodeJS Application on Your Server by justyy

View this thread on steempeak.com
· @justyy · (edited)
$56.31
Three ways of Running a continuous NodeJS Application on Your Server
Let's say, I have an application that is written in NodeJs, that process the latest blocks of the [steem blockchain](https://helloacm.com/recursive-algorithm-to-get-proxy-votes-on-steem-blockchain/) and write data into a database. I have three ways of running it on my server.

# Using Screen
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](https://helloacm.com/a-nice-alternative-for-putty-termius-a-very-nice-portable-ssh-connection-tool/) is disconnected, 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.

The advantage of using this method is:
1. you can easily monitor the application (by seeing its output)
2. you can easily terminate or restart the application e.g. Ctrl + C
3. 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:
1. If the application exists abnormaly, it is not restarted automatically - thus you would need to add the error-handling in your code.
2. 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](https://helloacm.com/crontab-generator-secure-fast-handy-tool-to-generate-the-crontab-lines/). However, the finest granularity is every minute i.e. you can specify your application to run every minute.

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

Disadvantages:
1. Lose the ability to sync real-time with the blocks
2. 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.

Advantages:
1. automatically restart your application in case it crashes or some conditions are met e.g. memory constraints, age.
2. easy to monitor the output of each application e.g. you can `pm2 show app`
3. easy to terminate `pm2 delete app` or restart `pm2 restart app`
4. the application runs continuously
5. error-handling is optional - but considered a good practise not to let `pm2` restart your app.

![image.png](https://cdn.steemitimages.com/DQmSTwjoWBZjLzuJ5HeTVvKCddGuyaiyqo2w4EzGshBpLFb/image.png)

<hr/>

Every little helps! I hope this helps!


**Steem On!~**
*Reposted to [Blog](https://helloacm.com/three-ways-of-running-a-continuous-nodejs-application-on-your-server/)*
------------------

If you like my work, please consider voting for me, thanks!
https://steemit.com/~witnesses type in **justyy** and click ***VOTE***
https://steemyy.com/images/vote-for-justyy.jpg
<BR/>
**Alternatively, you could [proxy to me](https://steemyy.com/witness-voting/?witness=justyy&action=proxy)  if you are too lazy to vote!**

Also: you can vote me at the tool I made:  https://steemyy.com/witness-voting/?witness=justyy

### Visit me at:  [https://steemyy.com](https://steemyy.com)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 74 others
properties (23)
post_id86,226,121
authorjustyy
permlinkthree-ways-of-running-a-continuous-nodejs-application-on-your-server
categorywitness-category
json_metadata{"tags":["whalepower","steem","nodejs","pm2","crontab"],"image":["https:\/\/cdn.steemitimages.com\/DQmSTwjoWBZjLzuJ5HeTVvKCddGuyaiyqo2w4EzGshBpLFb\/image.png","https:\/\/steemyy.com\/images\/vote-for-justyy.jpg"],"links":["https:\/\/helloacm.com\/recursive-algorithm-to-get-proxy-votes-on-steem-blockchain\/","https:\/\/helloacm.com\/a-nice-alternative-for-putty-termius-a-very-nice-portable-ssh-connection-tool\/","https:\/\/helloacm.com\/crontab-generator-secure-fast-handy-tool-to-generate-the-crontab-lines\/","https:\/\/helloacm.com\/three-ways-of-running-a-continuous-nodejs-application-on-your-server\/","https:\/\/steemit.com\/~witnesses","https:\/\/steemyy.com\/witness-voting\/?witness=justyy&action=proxy","https:\/\/steemyy.com\/witness-voting\/?witness=justyy","https:\/\/steemyy.com"],"app":"steemit\/0.2","format":"markdown"}
created2020-06-03 17:52:30
last_update2020-06-03 18:07:54
depth0
children0
net_rshares67,503,061,344,072
last_payout2020-06-10 17:52:30
cashout_time1969-12-31 23:59:59
total_payout_value28.818 SBD
curator_payout_value27.493 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3,526
author_reputation2,057,469,156,047,835
root_title"Three ways of Running a continuous NodeJS Application on Your Server"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (138)