Skip to content

Instantly share code, notes, and snippets.

@honzahommer
Last active September 20, 2017 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save honzahommer/dbb1a44a80a9b6becacb4da437ad59c8 to your computer and use it in GitHub Desktop.
Save honzahommer/dbb1a44a80a9b6becacb4da437ad59c8 to your computer and use it in GitHub Desktop.
FreeBSD scripts

Install nvm and create hotfix for nvm install command on FreeBSD.

Install packages, require root (wheel) privileges.

echo "y" | pkg install bash git gmake wget

There is an issue with autodetection C++ and C compiler, export paths manunally.

cat <<EOT >> $HOME/.profile
CC=$(which cc)
export CC
CXX=$(which c++)
export CXX
EOT

Close and reopen your terminal to start using nvm or run the following to use it now.

. $HOME/.profile

Run nvm install script. The script clones the nvm repository to ~/.nvm and adds the source line to your profile.

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

Install Node.js latest version via npm. There is no binary for FreeBSD, so build takes a while.

nvm install `nvm version-remote`

Print Node.js and npm version

node -v
npm -v

Run PM2 as service on FreeBSD

Create PM2 startup script

npm install -g pm2

cat <<EOT > /usr/local/etc/rc.d/pm2
#!/bin/sh

# PROVIDE: pm2
# REQUIRE: NETWORK
# KEYWORD: shutdown
#
 
. /etc/rc.subr
 
name=pm2
rcvar=pm2_enable
 
PM2=$(which pm2)
 
load_rc_config \$name
 
start_cmd="pm2_start"
stop_cmd="pm2_stop"
restart_cmd="pm2_restart"
 
: \${pm2_enable="NO"}
: \${pm2_user="root"}
 
pm2_start()
{
        CMD="\$PM2 resurrect"
        su -l \${pm2_user} -c "\${CMD}"
 
}
 
pm2_stop()
{
        CMD="\${PM2} dump && \${PM2} delete all && \${PM2} kill"
        su -l \${pm2_user} -c "\${CMD}"
}
 
pm2_restart()
{
        CMD="pm2_stop && pm2_start"
        su -l \${pm2_user} -c "\${CMD}"
}
 
pm2_reload()
{
        CMD="\$PM2 reload all"
        su -l \${pm2_user} -c "\${CMD}"
}
 
run_rc_command "\$1"
EOT

Make the file executable with:

chmod +x /usr/local/etc/rc.d/pm2

Safely edit system rc.conf file

sysrc pm2_enable="YES"
sysrc pm2_user="root"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment