node-startup icon indicating copy to clipboard operation
node-startup copied to clipboard

Script doesn't start Node

Open Ponyboy47 opened this issue 10 years ago • 10 comments

root@HomeKitPi:~# /etc/init.d/node-app start
Node app stopped, but pid file exists

I looked at issue #17, and I am using the latest version which includes the change to line 45, but for some reason it still doesn't work

Ponyboy47 avatar Nov 18 '14 01:11 Ponyboy47

@Ponyboy47 perhaps a look into your app.log helps you to understand the reason. I found out, that I had problems with permissions for my node-app.

Unfortunately my node-app doesn't start as well :-)

My Logfile says:

/home/userName/AppDir/app.js: 1: /home/userName/AppDir/app.js: /bin: Permission denied
/home/userName/AppDir/app.js: 2: /home/userName/AppDir/app.js: bin: not found
/home/userName/AppDir/app.js: 3: /home/userName/AppDir/app.js: bin: not found
/home/userName/AppDir/app.js: 4: /home/userName/AppDir/app.js: bin: not found
/home/userName/AppDir/app.js: 5: /home/userName/AppDir/app.js: bin: not found
/home/userName/AppDir/app.js: 6: /home/userName/AppDir/app.js: bin: not found
/home/userName/AppDir/app.js: 7: /home/userName/AppDir/app.js: bin: not found
/home/userName/AppDir/app.js: 8: /home/userName/AppDir/app.js: bin: not found
/home/userName/AppDir/app.js: 9: /home/userName/AppDir/app.js: bin/: Permission denied
/home/userName/AppDir/app.js: 11: /home/userName/AppDir/app.js: /bin: Permission denied
/home/userName/AppDir/app.js: 12: /home/userName/AppDir/app.js: use strict: not found
/home/userName/AppDir/app.js: 15: /home/userName/AppDir/app.js: Syntax error: "(" unexpected

When I start the app by hand like so service node-app start everything works fine... Seems like the start-service doesn't have access to /bin or something.

Can anyone help?

Update: Discovered that running

exec sudo pathToNode pathToApp

instead of line 53

PORT="$PORT" NODE_ENV="$NODE_ENV" NODE_CONFIG_DIR="$CONFIG_DIR" $NODE_EXEC "$APP_DIR/$NODE_APP"  1>"$LOG_FILE" 2>&1 &

helps running the app on startup (yeah :-) ). But boot-process doesn't finish anymore (nooo :-( ) because of using exec. So is there a way of running node as sudo in you script?

Vispercept avatar Nov 21 '14 09:11 Vispercept

@Vispercept Is the app running as your normal user (not root) on startup? I would stick with always using a single user (probably root) for managing the status of the service

ianpgall avatar Dec 09 '14 21:12 ianpgall

@ianpgall how would I know with which user the app runs on startup? I just added node-app to the init.d folder like the description says cp ./init.d/node-app /etc/init.d/. I found out that NODE_EXEC=$(which node) doesn't work in my case, although running which node gives back the right path to node. When I replace NODE_EXEC with the path to node by like so NODE_EXEC="/opt/node/bin/node" everything works fine, also on startup. :-) I have no idea why... But it works...

Vispercept avatar Dec 10 '14 08:12 Vispercept

Can someone explain what is happening in line 53 ?

ravisg avatar Feb 04 '15 22:02 ravisg

PORT="$PORT" NODE_ENV="$NODE_ENV" NODE_CONFIG_DIR="$CONFIG_DIR" $NODE_EXEC "$APP_DIR/$NODE_APP" 1>"$LOG_FILE" 2>&1 &

Its setting some environment variables: PORT, NODE_ENV, NODE_CONFIG_DIG and then calling the script (typically /usr/local/bin/node (NODE_EXEC) and passing it your app file. It also redirects stderr and stdout to a log file.

ralyodio avatar Feb 04 '15 22:02 ralyodio

@Vispercept Thanks for your hint. I'm using raspbian and replacing NODE_EXEC=$(which node) with NODE_EXEC="/usr/local/bin/node" worked for me.

thobach avatar Feb 08 '15 14:02 thobach

In the line 45 , should the ! sign be there ? If I understand correctly you are checking if the process exists or not in the [ ... ] . Why put a ! outside it ?

! [ -z "$(ps aux | awk '{print $2}' | grep "^$PID$")" ]

ravisg avatar Feb 09 '15 21:02 ravisg

@ravisg Yes, the ! should be there.

The ps aux...awk...grep part checks to see if the process' saved PID is found in the list of running processes. The way it's set up, it will return the PID (if found), or an empty string. The -z checks to see if the result of that check is null/empty. The ! negates that check, making it effectively check that a matching PID is not null/empty. Which is the same as checking if it's running.

I may be wrong, but I'm sure there's a way to condense the ! and -z into one thing...I think -n would work.

Does that make sense? Or am I crazy/wrong?

ianpgall avatar Feb 09 '15 22:02 ianpgall

@ianpgall - You are right , sorry :)

ravisg avatar Feb 09 '15 23:02 ravisg

@ravisg No worries, I wanted to make sure I understood it correctly too (I didn't write it originally).

ianpgall avatar Feb 11 '15 21:02 ianpgall