title: Remote nodejs server deployment with forever
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>This is what my post commit hook looks like:</p>
  
  <pre><code>#!/bin/bash
  
  #Connect to AWS machine and run deploy script
  ssh -i ~/Documents/aa-kp-inst1.pem ubuntu@&lt;remote-ip&gt; "sh /home/app/deploy.sh"
  
  #Done
  exit 0
  </code></pre>
  
  <p>Pretty straightforward. And this is what deploy.sh looks like:</p>
  
  <pre><code>#!/bin/bash
  
  #Navigate to server directory
  cd /home/app/personal_website/server
  
  #Stop currently running server(s)
  forever stop -s 0 &gt;&gt; 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
  </code></pre>
  
  <p>I see a difference in the processes started by running deploy.sh directly and by running it through ssh.</p>
  
  <p>Here's what <code>ps-ef | grep node</code> looks like when I run deploy.sh directly on the remote machine:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And this what I get when I run it from ssh:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Any idea on what could be causing this? Why does only the monitor process start when run from ssh?</p>
  
  <p>As asked in a comment:
  <code>env</code> from the machine directly:</p>
  
  <pre><code>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
  </code></pre>
  
  <p><code>env</code> from ssh:</p>
  
  <pre><code>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
  </code></pre>