Fix Ghost init script not starting automatically in FreeBSD jail

PublishedMay 05, 2017
Read Timeabout 2 min.

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

We wanted Ghost to start automatically after bootup; to do this we needed to create a custom FreeBSD init script. We found some examples of FreeBSD init scripts for ghost online and modified a few to suit our purposes.

After we were done this is what our 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 we rebooted the jail, but our service didn't start up. After much searching around on the internet we did not find a solution. That's when we decided to Read The FreeBSD Manual for RC scripting.

Turns out we needed to add # REQUIRE: LOGIN cleanvar to the init script in order to get the service started the way we 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.