start Ghost on boot as a service on Ubuntu

I was looking for an easy way to start Ghost on boot up and keep it running even if the applicaiton crashes. To do this, one has a few options. I initially wanted to get it working as a service under /etc/init.d/, but accomplishing this turned out to be a bit more tricky than I had anticipated. In the end, I couldn't manage it via init.d (even with the new and fancy-looking script provided by the folks at Ghost).

Fortunately for me, there's a good alternative that accomplishes my objectives: including a script in /usr/bin/ and then executing it on reboot via crontab.

Starting Ghost as a Service [persistent across reboot]

First, create your script file:

touch /usr/bin/ghost

Then ensure that the file can be executed:

chmod +x /usr/bin/ghost

After that, just past in the script:

#!/bin/bash 
    ###########################################################
# This script is made to control Ghost as if it were a
# service running on your system.
#
# Place in your path ie /usr/bin/
#
# This script was tested on Ubuntu 12.04.2 LTS.
# It should, however, be *NIX independent
#
# Make sure you alter the variables to your directories
#
# Original Author: Paul Williams
# www.infinitepercent.com
# Modifications for Forever: Aaron Dennis
# http://teknik.categori.se
 ###########################################################

# Variables:
GhostDIR=/path/to/ghost/ #BE SURE TO ADJUST THIS TO YOUR GHOST DIRECTORY  
NodeDIR=/path/to/node #BE SURE TO ADJUST THIS TO YOUR NODE DIRECTORY

# Functions:

# start function will test if Ghost is running, if not it will start it with forever
start()  
{
    PID=$(ps -ef | grep node | grep -v grep | grep -v ps | awk '{print $2}')
    if [ "$PID" = "" ]; then
        cd $GhostDIR && sudo forever start index.js
    else
        echo "Ghost is already running!"
    fi
}

# stop function will test if Ghost is running, it if is it will stop it with forever
stop()  
{
    PID=$(ps -ef | grep node | grep -v grep | grep -v ps | awk '{print $2}')
    if [ "$PID" = "" ]; then
        echo "Ghost isn't running!"
    else
       sudo kill $(ps -ef | grep node | grep -v grep | grep -v ps | awk '{print $2}')
    fi
}

# restart function calls stop function and then calls start function
restart()  
{
    stop
    start
}

# WARNING, DO NOT EVER MANUALLY ENTER BOOT
# YOU MAY END UP WITH MULTIPLE INSTANCES OF GHOST
# THIS OPTION IS MEANT TO BE USED FOR A CRON @REBOOT
boot()  
{
        cd $GhostDIR && sudo forever start index.js
}

# status function is used to check on the status of Ghost
status()  
{
    PID=$(ps -ef | grep node | grep -v grep | grep -v ps | awk '{print     $2}')
    if [ "$PID" = "" ]; then
        echo "Ghost isn't currently running!"
    else
        echo "Ghost is currently running!"
    fi

    echo "Here is the tail of the log:"
    sudo cat $GhostDIR/nohup.out | tail
}

# monitor function is used to monitor the Ghost log
monitor()  
{
    PID=$(ps -ef | grep node | grep -v grep | grep -v ps | awk '{print $2}')
    if [ "$PID" = "" ]; then
        echo "Ghost isn't currently running!"
    else
        echo "Please exit monitor mode with ^C"
        echo "Sleeping for 5 seconds to allow you to read how to exit"
        sleep 5
        sudo tail -f $GhostDIR/nohup.out
    fi
}

cd $GhostDIR  
NodeDIR=$NodeDIR/bin/npm

if [ "$1" = "start" ]; then  
    start
elif [ "$1" = "stop" ]; then  
    stop
elif [ "$1" = "restart" ]; then  
    restart
elif [ "$1" = "status" ]; then  
    status
elif [ "$1" = "monitor" ]; then  
    monitor
elif [ "$1" = "boot" ]; then  
    boot
else  
    echo "$0 {start|stop|restart|status|monitor}"
fi  

Credits:


View or Post Comments