Skip to content

Instantly share code, notes, and snippets.

@ryanrankin
Forked from wwwdata/gist:11217377
Last active January 27, 2016 23:01
Show Gist options
  • Select an option

  • Save ryanrankin/0f882c92d58ada01235c to your computer and use it in GitHub Desktop.

Select an option

Save ryanrankin/0f882c92d58ada01235c to your computer and use it in GitHub Desktop.
FreeBSD service script for NodeJS with pm2
#!/bin/sh
# PROVIDE: pm2
# REQUIRE: NETWORKING SERVERS DAEMON
# BEFORE: LOGIN
# KEYWORD: shutdown
# inspired by http://habrahabr.ru/post/137857/
. /etc/rc.subr
name="myApp"
pm2="/usr/local/bin/node /usr/local/bin/pm2"
workdir="/usr/home/node/www/$name"
script="server.js"
rcvar=myApp_enable
extra_commands="status"
start_cmd="start"
status_cmd="status"
stop_cmd="stop"
restart_cmd="restart"
load_rc_config $name
eval "${rcvar}=\${${rcvar}:-'NO'}"
start()
{
HOME=/usr/home/node USER=node su -m node -c "cd /usr/home/node/www/myApp; exec /usr/local/bin/node /usr/local/bin/pm2 -f start /usr/home/node/www/myApp/server.js -i 0 --name 'myApp'"
}
status()
{
su -m $USER -c "exec ${pm2} show $name"
}
stop()
{
su -m $USER -c "exec ${pm2} stop $name"
}
restart()
{
su -m $USER -c "exec ${pm2} restart $name"
}
run_rc_command "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment