× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Best ways to run your Node app continuously

Donald Derek
Oct 07, 2014
<p><strong>PM2</strong></p><pre><code class="language-bash"> #install PM2 globally sudo npm install pm2 -g #start an app with i number of processes pm2 start /some_dir/your_app.js -i max #list node processes pm2 list #restart app pm2 restart /some_dir/your_app.js #stop and remove all apps pm2 stopAll </code></pre><p>Note that you can also use the app ID assigned by pm2 to control (start, pause, remove) a given node process.</p><p> </p><p><strong>Forever.js</strong></p><pre><code class="language-bash"> #install forever.js globally sudo npm install forever -g #start an app forever start /some_dir/your_app.js #list node processes forever list #restart app forever restart /some_dir/your_app.js #stop and remove all apps forever stopall </code></pre><p> </p><p><strong>Other ways for quick testing</strong></p><pre><code class="language-bash"> node /some_dir/your_app.js &amp; </code></pre><p> </p><p><strong>Run deamonize a node process as a service on Ubuntu/Deb</strong></p><p>Running your Node  app as a serivce has lots of benefits. For instance if your server reboots your app will automatically  start. First you need to create your shell script. You can check the template file in /etc/init.d replicating it to look like the following:</p><p> </p><pre><code> #!/bin/sh # description: App Description # exit on first error set -e # user running this script _user="$(id -u -n)" # points to the root for forever config export "FOREVER_ROOT=/home/webdev/.forever" # commands to run on "start" (new line per command) startup=( "/home/webdev/project_dir/node_modules/forever/bin/forever --sourceDir /home/webdev/project_dir/ start your_app.js" ) # commands to run on "stop" (new line per command) stopitems=( "/home/webdev/project_dir/node_modules/forever/bin/forever stop app.js" ) # start function do_start(){ if [ "$_user" == "webdev" ]; then echo "Node.js App Running as a Service!" for i in "${startup[@]}" do $i done else echo "You don't have the privilege to run this service!" for i in "${startup[@]}" do su - brant -c "$i" done fi } # stop function do_stop(){ if [ "$_user" == "webdev" ]; then echo "Node.js App Running as a Service!" for i in "${stopitems[@]}" do $i done else echo "You don't have the privilege to run this service!" for i in "${stopitems[@]}" do su - brant -c "$i" done fi } # Decide what command is being called case "$1" in start) echo "Starting Node App..." do_start echo "done." ;; stop) echo "Stoping Node App..." do_stop echo "done." ;; restart) echo "Restarting Node App..." do_stop do_start echo "done." ;; *) echo "Usage: project {start|stop|restart}" &gt;&amp;2 exit 3 ;; esac exit 0</code></pre><p>Save this file using vi or nano under the name of your new service.  Then you could execute the following commands to control your new node service.</p><pre><code>service nodeService start service nodeService stop service nodeService restart</code></pre><p> </p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus