Fix Ghost init script not starting automatically in FreeBSD jail

Posted by Orville Bennett on 5 May 2017
Read time: about 3 minutes

I've been redoing the infrastructure for the web servers here at the bennett project—migrating from Ubuntu 12.04 to FreeBSD 11. I've shared the reasons for that elsewhere. Today I wanted to document an issue I ran into when setting up the Ghost blogging platform inside a FreeBSD Jail.

I wanted the ghost application to start automatically on boot; to do this I needed to create a custom FreeBSD init script. I found some examples of FreeBSD init scripts for ghost online and modified a few to suit my purposes.

After I was done this is what my init script looked like:

#!/bin/sh

# PROVIDE: ghost
# KEYWORD: shutdown

PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"

. /etc/rc.subr

name="ghost"
rcvar="ghost_enable"
extra_commands="status"
load_rc_config ${name}
: ${ghost_enable:="NO"}

status_cmd="ghost_status"
start_cmd="ghost_start"
stop_cmd="ghost_stop"
restart_cmd="ghost_restart"

ghost="/usr/local/www/ghost"

ghost_start() {
su ghost -c "NODE_ENV=production forever start -al $ghost/ghost.log $ghost/index.js"
#su ghost -c "NODE_ENV=production pm2 start $ghost/index.js --name $name"
}
ghost_stop() {
su ghost -c "NODE_ENV=production forever stop $ghost/index.js"
#su ghost -c "NODE_ENV=production pm2 stop $name"
}

ghost_status() {
su ghost -c "NODE_ENV=production forever list"
#su ghost -c "NODE_ENV=production pm2 list"
}

ghost_restart() {
ghost_stop;
ghost_start;
#su ghost -c "NODE_ENV=production pm2 restart $name"
}

run_rc_command "$1"

After testing it out using service ghost start I rebooted the jail, but my service didn't start up. After much searching around on the internet I did not find a solution. That's when I decided to Read The FreeBSD Manual for RC scripting.

Turns out I needed to add # REQUIRE: LOGIN cleanvar to the init script in order to get the service started the way I wanted. The final ghost init script then, looked like this:

#!/bin/sh

# PROVIDE: ghost
# REQUIRE: LOGIN cleanvar
# KEYWORD: shutdown

PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"

. /etc/rc.subr

name="ghost"
rcvar="ghost_enable"
extra_commands="status"
load_rc_config ${name}
: ${ghost_enable:="NO"}

status_cmd="ghost_status"
start_cmd="ghost_start"
stop_cmd="ghost_stop"
restart_cmd="ghost_restart"

ghost="/usr/local/www/ghost"

ghost_start() {
su ghost -c "NODE_ENV=production forever start -al $ghost/ghost.log $ghost/index.js"
#su ghost -c "NODE_ENV=production pm2 start $ghost/index.js --name $name"
}
ghost_stop() {
su ghost -c "NODE_ENV=production forever stop $ghost/index.js"
#su ghost -c "NODE_ENV=production pm2 stop $name"
}

ghost_status() {
su ghost -c "NODE_ENV=production forever list"
#su ghost -c "NODE_ENV=production pm2 list"
}

ghost_restart() {
ghost_stop;
ghost_start;
#su ghost -c "NODE_ENV=production pm2 restart $name"
}

run_rc_command "$1"

FreeBSD has good documentation. When in doubt, always read the FreeBSD manuals.