persistent Ghost on Ubuntu Server with Forever

This entry addresses how to make the Ghost application persist: Forever.

Forever is a programme that can be used to start Ghost and keep it alive as a background process (i.e. allowing you to continue to use--even close--your terminal without it stopping Ghost). Also, Forever will monitor Ghost and restart automatically it if it crashes.

Installing Forever

Forever should be installed globally via the Node Package Manager:

npm install -g forever

Basic Use Case: Starting and Stopping Ghost with Forever

You have to use Forever from your Ghost-directory using the index.js (as opposed the usual npm start --production).

Starting Ghost

Per default, forever start index.js will start Ghost in development mode. To use Ghost in production mode, all you need to do is to specify an environment variable:

NODE_ENV=production forever start index.js

Stopping Ghost via Forever

Stopping Ghost is as simple as replacing the start command:

forever stop index.js

or
NODE_ENV=production forever stop index.js

Be sure that you use the same user and environmental parameters when attempting to stop Ghost!

Advanced Use Cases

Logging

Forever Stream Logs to stdout

Streaming Logs to stdout is easy:

forever -f start index.js

Forever Log to File

To use different files, all you need is to specify the log location and to use the -l flag:

forever -l /path/to/ghost/forever.log start index.js

Starting Ghost on Reboot

To ensure that Ghost persists also across reboots, one has a few options. I wanted to get it working as a service under /etc/init.d/, but couldn't manage it (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.

For this to work, you'll need first to create your script file:

touch /usr/bin/ghost

Then you'll need to 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 created/tested on a Rasberry Pi
# running Raspbian.  It was then re-test 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