2

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
aa333
  • 2,556
  • 16
  • 23

1 Answers1

2

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:

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
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I made the modifications you suggested. But that didn't help. The server still doesn't start when run from ssh, but runs fine if the script is run directly on the remote machine. :( – aa333 Jul 12 '14 at 04:56
  • @aa333 then put an echo, to check that at least the hook is called. – VonC Jul 12 '14 at 04:56
  • Yup the hook is called. The server gets stopped but doesn't start again as it should, and if I put an echo to a file it works as expected. – aa333 Jul 12 '14 at 04:58
  • @aa333 does it start again if you don't do the `git pull` at all in your hook script? – VonC Jul 12 '14 at 05:11
  • Just tried that. Having or not having the git pull has no effect on the behavior as I had suspected. I believe this has more to do with forever than git, going by the differences in processes started in the two different cases. I've put them in the question, the ps-ef | grep node outputs. – aa333 Jul 12 '14 at 05:14
  • @aa333 strange, like if https://github.com/nodejitsu/forever was creating a sub-shell – VonC Jul 12 '14 at 05:18
  • @aa333 can you add `env` in the hook? The environment variables when launching the script directly, and the ones when launching thorugh the hooks must differ somehow. – VonC Jul 12 '14 at 05:52
  • May be. Can you elaborate on how should I do that? Which env variables are we talking about? – aa333 Jul 12 '14 at 05:58
  • @aa333 I don't know, but I was hopping to get a diff between the two env outputs, and see what is different. – VonC Jul 12 '14 at 05:59
  • I have put the two `env`s in the question. Nothing jumps out to me though, except the absence of `NODE_PATH` in ssh env. But I'm not sure how that affects this. – aa333 Jul 12 '14 at 06:12
  • @aa333 it might influence the execution of the node command `forever`, consider it is done in an ssh session. – VonC Jul 12 '14 at 08:04
  • I got it to work. Had to export NODE_PATH before running the rest of the script. Great call for diffing the `env`s. :) – aa333 Jul 12 '14 at 16:37
  • @aa333 Great! I have included your conclusion in the answer for more visibility. – VonC Jul 12 '14 at 17:37