Ghost Blog Sys-V init Linux Startup Script

Installing nodejs init script for Ghost Blog.

If you followed my guide on: Hosting your own blog with Ghost & Nginx on Linux

The following Linux init script should work as is, on
RHEL, Oracle Linux, SL, CentOS Linux distributions.

For other Linux distributions, minor adjustments should be expected.

Installing Ghost Blog init script

$ sudo vi /etc/init.d/ghostblog

^Add the below script to the file and save.

Ghost Blog Linux init script:

#!/bin/sh
#
# ghostblog   Startup script for nodejs Ghost Blog
#
# Copyright (C) 2015 Lars Bjaerris lars<at>bjaerris.com
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# -------------------------------------------------------------------
# chkconfig: 2345 80 10
#
### BEGIN INIT INFO
# Provides: ghostblog
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop ghostblog
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

user="ghostblog"
prog="ghostblog"
exec="/usr/bin/npm"
run_env="NODE_ENV=production"
option="start"
daemon_dir="/home/ghostblog/ghost/"
piddir="/home/$user/ghost/run/"
pidfile="$piddir$prog.pid"
prockill='node index'

[[ -d $piddir ]] || su $user -c " mkdir -p $piddir"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting $prog: "
    su $user -c "cd $daemon_dir; export $run_env; nohup $exec $option > /dev/null 2>&1 &"  
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile && sleep 2;echo $(/usr/bin/pgrep -u ghostblog -f 'node index') > $pidfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    pkill -u $user -f "$prockill"
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile && rm -f $pidfile 
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
status $prockill
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

-

 $ sudo chmod 0750 /etc/init.d/ghostblog

^Make it executable and set permissions.

$ sudo chkconfig --add /etc/init.d/ghostblog

^Add the init scipt to services

$ sudo chkconfig ghostblog on

^Set the startup flag to on.

-

Testing:

$ sudo service ghostblog start
Starting ghostblog: 
$ sudo service ghostblog status
node (pid 24909) is running...
$ sudo service ghostblog stop
Stopping ghostblog: 
$ sudo service ghostblog start
Starting ghostblog: 
$ sudo service ghostblog status
node (pid 24963) is running...
$

ghostblog is configured to start and stop as a service.

Hope you enjoyed it!
Lars Bjaerris