Skip to content

Instantly share code, notes, and snippets.

@tcha-tcho
Created February 15, 2010 06:21
Show Gist options
  • Save tcha-tcho/304452 to your computer and use it in GitHub Desktop.
Save tcha-tcho/304452 to your computer and use it in GitHub Desktop.
#! /bin/bash
NAME=mongodb
TRIGGER='/etc/init.d/mongodb start'
PIDFILE=/var/run/$NAME.pid
DAEMON=/usr/bin/mongod
ensure_running() {
if ! running ; then
echo "Starting at: `date`"
bash -c "$TRIGGER"
fi
}
running_pid() {
# Check if a given process pid's cmdline matches a given name
pid=$1
name=$2
[ -z "$pid" ] && return 1
[ ! -d /proc/$pid ] && return 1
cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
# Is this the expected server
[ "$cmd" != "$name" ] && return 1
return 0
}
running() {
# Check if the process is running looking at /proc
# (works for all users)
# No pidfile, probably no daemon present
[ ! -f "$PIDFILE" ] && return 1
pid=`cat $PIDFILE`
running_pid $pid $DAEMON || return 1
return 0
}
ensure_running
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment