Skip to content

Instantly share code, notes, and snippets.

@wbzyl
Created September 15, 2011 18:55
Show Gist options
  • Save wbzyl/1220123 to your computer and use it in GitHub Desktop.
Save wbzyl/1220123 to your computer and use it in GitHub Desktop.
CouchDB init.d script for Linux / Fedora 15
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: couchdb
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Apache CouchDB init script
# Description: Apache CouchDB init script for the database server.
### END INIT INFO
SCRIPT_OK=0
SCRIPT_ERROR=1
DESCRIPTION="database server"
NAME=couchdb
SCRIPT_NAME=`basename $0`
#COUCHDB=/usr/bin/couchdb
COUCHDB=/home/wbzyl/.nosql/bin/couchdb
#CONFIGURATION_FILE=/etc/default/couchdb
CONFIGURATION_FILE=/home/wbzyl/.nosql/etc/default/couchdb
#RUN_DIR=/var/run/couchdb
RUN_DIR=/home/wbzyl/.nosql/var/run/couchdb
LSB_LIBRARY=/lib/lsb/init-functions
if test ! -x $COUCHDB; then
exit $SCRIPT_ERROR
fi
if test -r $CONFIGURATION_FILE; then
. $CONFIGURATION_FILE
fi
log_daemon_msg () {
# Dummy function to be replaced by LSB library.
echo $@
}
log_end_msg () {
# Dummy function to be replaced by LSB library.
if test "$1" != "0"; then
echo "Error with $DESCRIPTION: $NAME"
fi
return $1
}
if test -r $LSB_LIBRARY; then
. $LSB_LIBRARY
fi
start_couchdb () {
# Start Apache CouchDB as a background process.
command="$COUCHDB -b"
if test -n "$COUCHDB_STDOUT_FILE"; then
command="$command -o $COUCHDB_STDOUT_FILE"
fi
if test -n "$COUCHDB_STDERR_FILE"; then
command="$command -e $COUCHDB_STDERR_FILE"
fi
if test -n "$COUCHDB_RESPAWN_TIMEOUT"; then
command="$command -r $COUCHDB_RESPAWN_TIMEOUT"
fi
if test -n "$COUCHDB_OPTIONS"; then
command="$command $COUCHDB_OPTIONS"
fi
mkdir -p "$RUN_DIR"
if test -n "$COUCHDB_USER"; then
chown $COUCHDB_USER "$RUN_DIR"
if su $COUCHDB_USER -c "$command" > /dev/null; then
return $SCRIPT_OK
else
return $SCRIPT_ERROR
fi
else
if $command > /dev/null; then
return $SCRIPT_OK
else
return $SCRIPT_ERROR
fi
fi
}
stop_couchdb () {
# Stop the running Apache CouchDB process.
command="$COUCHDB -d"
if test -n "$COUCHDB_OPTIONS"; then
command="$command $COUCHDB_OPTIONS"
fi
if test -n "$COUCHDB_USER"; then
if su $COUCHDB_USER -c "$command" > /dev/null; then
return $SCRIPT_OK
else
return $SCRIPT_ERROR
fi
else
if $command > /dev/null; then
return $SCRIPT_OK
else
return $SCRIPT_ERROR
fi
fi
}
display_status () {
# Display the status of the running Apache CouchDB process.
$COUCHDB -s
}
parse_script_option_list () {
# Parse arguments passed to the script and take appropriate action.
case "$1" in
start)
log_daemon_msg "Starting $DESCRIPTION" $NAME
if start_couchdb; then
log_end_msg $SCRIPT_OK
else
log_end_msg $SCRIPT_ERROR
fi
;;
stop)
log_daemon_msg "Stopping $DESCRIPTION" $NAME
if stop_couchdb; then
log_end_msg $SCRIPT_OK
else
log_end_msg $SCRIPT_ERROR
fi
;;
restart)
log_daemon_msg "Restarting $DESCRIPTION" $NAME
if stop_couchdb; then
if start_couchdb; then
log_end_msg $SCRIPT_OK
else
log_end_msg $SCRIPT_ERROR
fi
else
log_end_msg $SCRIPT_ERROR
fi
;;
status)
display_status
;;
*)
cat << EOF >&2
Usage: $SCRIPT_NAME {start|stop|restart|status}
EOF
exit $SCRIPT_ERROR
;;
esac
}
parse_script_option_list $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment