ralyodio / node-startup

Startup script for Linux-based systems for running node app when rebooting using an /etc/init.d script.
MIT License
781 stars 169 forks source link

Script doesn't start Node #19

Open Ponyboy47 opened 9 years ago

Ponyboy47 commented 9 years ago
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

Vispercept commented 9 years ago

@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?

ianpgall commented 9 years ago

@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

Vispercept commented 9 years ago

@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...

ravisg commented 9 years ago

Can someone explain what is happening in line 53 ?

ralyodio commented 9 years ago

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.

thobach commented 9 years ago

@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.

ravisg commented 9 years ago

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$")" ]

ianpgall commented 9 years ago

@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?

ravisg commented 9 years ago

@ianpgall - You are right , sorry :)

ianpgall commented 9 years ago

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