WebFaction
Community site: login faq

Hello, I have setup a node.js app, not the default - although I tried that and learned a few things first. I have the whole application working when I go to the correct folder and run "npm run start" - the app starts and everything is fine. Later, I revisit the site, and the site is down. I SSH in, re-edit my $PATH variable and again run "npm run start" and everything is fine.

Why does my app stop working after a while and is it required that I have a cron setup to start my app again?

Further more. I tried to read the documentation on setting up the cron and it says right in the documentation:

Note Cron jobs run under different conditions than scripts run from a shell. ... do not rely on environment variables like PATH. Changes to the environment by .bashrc and other “dot” files are unlikely to be available to your cron task.

Does this mean that even if I tried to setup a cron, I couldn't get my "npm run start" to run properly?

Cheers

asked 25 Feb '15, 01:00

longandshort
112
accept rate: 0%

edited 25 Feb '15, 01:00


The problem is that you're running the node server in your current session. When you terminate that session, it dies. The documentation about cron not knowing about your environment variables is also correct.

However, no - despite this, a script which runs from cron is exactly what you need.

Call that script every 10 minutes or so. If your application isn't running, it will be started; otherwise, the script exits without doing anything. More importantly, your environment variables would be set directly in that script, so it doesn't matter that cron doesn't know about them. All cron needs to do is to call the script, and all the script needs to do is to correctly start your application when needed.

Hope that helps!

permanent link

answered 25 Feb '15, 01:30

ryans ♦♦
5.0k93360
accept rate: 43%

Thanks so much. I'll get started trying to modify the "start" script file that came with the basic OneCLick Node.js install from the webfaction dashboard system.

(25 Feb '15, 06:08) longandshort

Is this all I need for a script? First time bash scripter:

#!/bin/sh
export PATH=$PATH:/home/tackapp/webapps/newnode/bin

npm run start

the PATH is whats needed for the Node and NPM to work the npm run start starts the script

(25 Feb '15, 07:07) longandshort

Yes, that may be simple enough.

The way to test is try running the script a few times, and then check your process listing with:

ps jf -u $USER

What you want to see is that only one instance of node is running, even though you ran the script several times. If you run it five times and end up with five (broken) node instances running in memory, then that's a problem, and you'll want to use the more intelligent watchdog script linked earlier.

Hope that helps!

(25 Feb '15, 09:13) ryans ♦♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×81
×67
×50

question asked: 25 Feb '15, 01:00

question was seen: 5,489 times

last updated: 25 Feb '15, 09:13

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM