Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Created February 26, 2014 18:43
Show Gist options
  • Save subfuzion/9235741 to your computer and use it in GitHub Desktop.
Save subfuzion/9235741 to your computer and use it in GitHub Desktop.
shell script to deploy and start a node app via ssh

A simple starter script to copy a node app to a remote host and start it over ssh.

Not very robust in terms of order-dependent argument handling. Arguments have defaults, but if you provide a value for one, any preceding args must also be supplied.

Arguments

  1. The source directory. Defaults to current directory.
  2. The destination server for the ssh connection.
  3. The port to run the app on the destination server. Defaults to port 80.
#!/bin/sh

# source
DIR=${1:-`pwd`}
DIRPATH=$(dirname $DIR)

# destination
HOST=${2:-default.server.com}
APPPORT=${3:-80}
TARGETPATH=node
DIRNAME=$(basename $DIR)

# don't want to be prompted by ssh for first time remote hosts.
alias ssh='ssh -o StrictHostKeyChecking=no'

# push app to host
# ssh root@$HOST 'cd node; rm -rf *' 
cd $DIRPATH; tar czf - $DIRNAME | ssh root@$HOST '(cd $TARGETPATH; rm -rf *; tar xzf -)' 

# start app
# ssh root@$HOST "cd $TARGETPATH/$DIRNAME; PORT=$APPPORT; npm start"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment