Remote nodejs server deployment with forever

廉价感情. 提交于 2019-12-22 17:41:43

问题


I'm trying to deploy a node js server on a remote machine by a post-commit hook, which would call a script on the remote machine that does the actual deployment.

The problem I'm facing is if I run the remote script on the machine it works fine, but when I try to execute the same script by an ssh command the server doesn't start.

This is what my post commit hook looks like:

#!/bin/bash

#Connect to AWS machine and run deploy script
ssh -i ~/Documents/aa-kp-inst1.pem ubuntu@<remote-ip> "sh /home/app/deploy.sh"

#Done
exit 0

Pretty straightforward. And this is what deploy.sh looks like:

#!/bin/bash

#Navigate to server directory
cd /home/app/personal_website/server

#Stop currently running server(s)
forever stop -s 0 >> forever.log

#Pull latest code
unset GIT_DIR
git -C /home/app/personal_website/server pull --quiet

#Restart server
forever start -a -l forever.log -o out.log -e err.log server.js

#End
exit 0

I see a difference in the processes started by running deploy.sh directly and by running it through ssh.

Here's what ps-ef | grep node looks like when I run deploy.sh directly on the remote machine:

ubuntu   14058     1  3 04:26 ?        00:00:00 /usr/bin/nodejs      /usr/lib/node_modules/forever/bin/monitor server.js
ubuntu   14064 14058  2 04:26 ?        00:00:00 /usr/bin/nodejs /home/app/personal_website/server/server.js
ubuntu   14071 10791  0 04:27 pts/0    00:00:00 grep --color=auto node

And this what I get when I run it from ssh:

ubuntu   13435     1 14 04:19 ?        00:00:00 /usr/bin/nodejs /usr/lib/node_modules/forever/bin/monitor server.js
ubuntu   13444 10791  0 04:19 pts/0    00:00:00 grep --color=auto node

Any idea on what could be causing this? Why does only the monitor process start when run from ssh?

As asked in a comment: env from the machine directly:

XDG_SESSION_ID=31
TERM=xterm-256color
SHELL=/bin/bash
SSH_CLIENT=97.83.204.67 58752 22
SSH_TTY=/dev/pts/2
USER=ubuntu
MAIL=/var/mail/ubuntu
PATH=/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PWD=/home
LANG=en_US.UTF-8
NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
SHLVL=1
HOME=/home/ubuntu
LOGNAME=ubuntu
SSH_CONNECTION=97.83.204.67 58752 172.31.7.96 22
LESSOPEN=| /usr/bin/lesspipe %s
XDG_RUNTIME_DIR=/run/user/1000
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/printenv
OLDPWD=/home/exps

env from ssh:

XDG_SESSION_ID=32
SHELL=/bin/bash
SSH_CLIENT=97.83.204.67 58966 22
USER=ubuntu
MAIL=/var/mail/ubuntu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PWD=/home/ubuntu
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/ubuntu
LOGNAME=ubuntu
SSH_CONNECTION=97.83.204.67 58966 172.31.7.96 22
XDG_RUNTIME_DIR=/run/user/1000
_=/usr/bin/printenv

回答1:


In this case, the environment variable was slightly different between a local call and a call through ssh.

The Op aa333 confirms in the comments:

Had to export NODE_PATH before running the rest of the script.

That allows for the node command forever to run through the ssh session.


More generally, for a hook, I always recommend:

Add before your git pull:

unset GIT_DIR

If you don't unset GIT_DIR, then, when executed in a hook, it will still operate as it was in the git repo where you push (and not the one where you change directory to).

See for instance:

  • "getting “fatal: not a git repository: '.'” when using post-update hook to execute 'git pull' on another repo"
  • "Git - post-receive hook with git pull “Failed to find a valid git directory”"

As in the second link, I would, to be sure, do a:

git --git-dir=/home/app/personal_website/server/.git --work-tree=/home/app/personal_website/server pull --quiet

Or, since git 1.8.5:

git -C /home/app/personal_website/server pull --quiet


来源:https://stackoverflow.com/questions/24709717/remote-nodejs-server-deployment-with-forever

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!