Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ### BEGIN INIT INFO
- # Provides: neo4j-service
- # Required-Start:
- # Required-Stop:
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- ### END INIT INFO
- # Copyright (c) 2002-2011 "Neo Technology,"
- # Network Engine for Objects in Lund AB [http://neotechnology.com]
- #
- # This file is part of Neo4j.
- #
- # Neo4j 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/>.
- # getconfig <filename>
- # - plain key-value pairs become environment variables
- # - keys have '.' chars changed to '_'
- # - keys of the form KEY.# (where # is a number) are concatenated
- # into a single environment variable named KEY
- function getconfig {
- if [ -e '$1' ]; then
- while read line; do
- if [[ ${line} =~ "^([^#\s][^=]+)=(.+)$" ]]; then
- key=`echo ${BASH_REMATCH[1]} | sed 's/\./_/g'`
- value="${BASH_REMATCH[2]}"
- if [[ ${key} =~ "^(.*)_([0-9]+)$" ]]; then
- key=${BASH_REMATCH[1]}
- if [[ ${!key} ]]; then
- export ${key}="${!key} ${value}"
- else
- export ${key}="$value"
- fi
- else
- export ${key}="${value}"
- fi
- fi
- done < $1
- fi
- }
- NEO4J_SCRIPT=$0
- cd `dirname $NEO4J_SCRIPT`
- NEO4J_SCRIPT=`basename $NEO4J_SCRIPT`
- while [ -L "$NEO4J_SCRIPT" ]
- do
- NEO4J_SCRIPT=$( readlink $NEO4J_SCRIPT )
- cd `dirname $NEO4J_SCRIPT`
- NEO4J_SCRIPT=`basename $NEO4J_SCRIPT`
- done
- NEO4J_HOME=`cd $( dirname $NEO4J_SCRIPT )/.. && dirs -l +0`
- NEO4J_INSTANCE=$NEO4J_HOME
- cd "$NEO4J_HOME"
- getconfig "${NEO4J_INSTANCE}/conf/neo4j-wrapper.conf"
- getconfig "${NEO4J_INSTANCE}/conf/neo4j-server.properties"
- NEO4J_OPTS="-Dlog4j.configuration=file:conf/log4j.properties -Dorg.neo4j.server.properties="${NEO4J_INSTANCE}"/conf/neo4j-server.properties -Djava.util.logging.config.file="${NEO4J_INSTANCE}/conf/logging.properties" -Dneo4j.home="${NEO4J_HOME}" -Dneo4j.instance="${NEO4J_INSTANCE}""
- JAVA_OPTS="-server ${wrapper_java_additional}"
- #NEO4J_SERVER_PORT=`( egrep "^org.neo4j.server.webserver.port" $NEO4J_INSTANCE/conf/neo4j-server.properties || echo 7474 ) | sed -e 's/.*=//'`
- NEO4J_SERVER_PORT=${org_neo4j_server_webserver_port:=7474}
- if [ -z "${wrapper_user}" ]; then
- NEO4J_USER=`id -un`
- else
- NEO4J_USER=${wrapper_user}
- fi
- SCRIPT_NAME="${NEO4J_HOME}/bin/neo4j"
- SERVICE_NAME=${wrapper_ntservice_name:=neo4j-service}
- LAUNCHD_DIR=~/Library/LaunchAgents/
- TIMEOUT=20
- PID_FILE=${NEO4J_INSTANCE}/data/neo4j-service.pid
- # Detect java and set JAVACMD on successful find.
- findjava() {
- # OS specific support. $var _must_ be set to either true or false.
- cygwin=false;
- darwin=false;
- case "`uname`" in
- CYGWIN*) cygwin=true ;;
- Darwin*) darwin=true
- if [ -z "$JAVA_VERSION" ] ; then
- JAVA_VERSION="CurrentJDK"
- else
- echo "Using Java version: $JAVA_VERSION"
- fi
- if [ -z "$JAVA_HOME" ] ; then
- JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
- fi
- ;;
- esac
- if [ -z "$JAVA_HOME" ] ; then
- if [ -r /etc/gentoo-release ] ; then
- JAVA_HOME=`java-config --jre-home`
- fi
- fi
- # For Cygwin, ensure paths are in UNIX format before anything is touched
- if $cygwin ; then
- [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
- [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
- fi
- # If a specific java binary isn't specified search for the standard 'java' binary
- if [ -z "$JAVACMD" ] ; then
- if [ -n "$JAVA_HOME" ] ; then
- if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
- # IBM's JDK on AIX uses strange locations for the executables
- JAVACMD="$JAVA_HOME/jre/sh/java"
- else
- JAVACMD="$JAVA_HOME/bin/java"
- fi
- else
- JAVACMD=`which java`
- fi
- fi
- }
- # Use findjava to set JAVACMD. If that fails ($JAVACMD is not set) then exit with failure
- exitonnojava() {
- findjava
- if [ ! -x "$JAVACMD" ] ; then
- echo "Error: JAVA_HOME is not defined correctly."
- echo " We cannot execute $JAVACMD"
- exit 1
- fi
- }
- # Resolve the os
- detectos() {
- DIST_OS=`uname -s | tr [A-Z] [a-z] | tr -d ' '`
- case "$DIST_OS" in
- 'sunos')
- DIST_OS="solaris"
- ;;
- 'hp-ux' | 'hp-ux64')
- # HP-UX needs the XPG4 version of ps (for -o args)
- DIST_OS="hpux"
- UNIX95=""
- export UNIX95
- ;;
- 'darwin')
- DIST_OS="macosx"
- ;;
- 'unix_sv')
- DIST_OS="unixware"
- ;;
- 'os/390')
- DIST_OS="zos"
- ;;
- 'cygwin')
- DIST_OS="cygwin"
- ;;
- esac
- }
- buildclasspath() {
- # confirm library jars
- LIBDIR="$NEO4J_HOME"/lib
- if [ ! -e "$LIBDIR" ] ; then
- echo "Error: missing Neo4j Library, expected at $LIBDIR"
- exit 1
- fi
- # confirm system jars
- SYSLIBDIR="$NEO4J_HOME"/system/lib
- if [ ! -e "$SYSLIBDIR" ] ; then
- echo "Error: missing Neo4j System Library, expected at $SYSLIBDIR"
- exit 1
- fi
- ALL_JARS=""
- for jar in "$LIBDIR"/*.jar "$SYSLIBDIR"/*.jar ; do
- [ -z "$ALL_JARS" ] && ALL_JARS="$jar" || ALL_JARS="$ALL_JARS":"$jar"
- done
- # add any plugin jars
- for jar in "$NEO4J_HOME"/plugins/*.jar ; do
- if [ -e "$jar" ] ; then
- ALL_JARS="$ALL_JARS":"$jar"
- fi
- done
- if [ -e "$NEO4J_HOME"/system/coordinator/lib ]; then
- for jar in "$NEO4J_HOME"/system/coordinator/lib/*.jar ; do
- if [ -e "$jar" ] ; then
- ALL_JARS="$ALL_JARS":"$jar"
- fi
- done
- fi
- CLASSPATH=${ALL_JARS}
- }
- checkstatus() {
- if [ -e "$PID_FILE" ] ; then
- NEO4J_PID=$( cat "$PID_FILE" )
- kill -0 $NEO4J_PID 2>/dev/null || NEO4J_PID=
- fi
- }
- checkwriteaccess() {
- if [ $UID != 0 ] ; then
- if [ ! -w "$NEO4J_INSTANCE/data" ] ; then
- echo "ERROR: No write access to data/ directory, run either as user $NEO4J_USER or root"
- exit 1
- fi
- fi
- }
- getlaunchdpid() {
- LAUNCHDPID=$(launchctl list | grep org.neo4j.server | cut -f 1)
- if [ -z $LAUNCHDPID ] ; then
- # Not there
- LAUNCHDPID=-1
- elif [ $LAUNCHDPID = "-" ] ; then
- # There but not running
- LAUNCHDPID=0
- else
- # There and running
- LAUNCHDPID=$(($LAUNCHDPID))
- fi
- }
- startit() {
- detectos
- exitonnojava
- checkstatus
- if [ $DIST_OS = "macosx" ] ; then
- getlaunchdpid
- if [ $LAUNCHDPID -eq 0 ] ; then
- echo "Detected installation in launchd, starting it..."
- launchctl start org.neo4j.server
- exit 0
- elif [ $LAUNCHDPID -gt 0 ] ; then
- echo "Instance already running via launchd with PID $LAUNCHDPID"
- exit 1
- fi
- # We fall through here since if there is no launchd install we start manually
- fi
- if [ -z $NEO4J_PID ] ; then
- printf "Starting neo4j-service..."
- buildclasspath
- if [ $UID == 0 ] ; then
- su $NEO4J_USER -c "$JAVACMD -cp '$CLASSPATH' $JAVA_OPTS $NEO4J_OPTS org.neo4j.server.Bootstrapper >> '$NEO4J_INSTANCE/data/log/console.log' 2>&1 & echo \$! > '$PID_FILE' "
- else
- checkwriteaccess
- echo "WARNING: not changing user"
- $JAVACMD -cp "${CLASSPATH}" $JAVA_OPTS -Dlog4j.configuration=file:conf/log4j.properties -Dorg.neo4j.server.properties="${NEO4J_INSTANCE}"/conf/neo4j-server.properties -Djava.util.logging.config.file="${NEO4J_INSTANCE}/conf/logging.properties" -Dneo4j.home="${NEO4J_HOME}" -Dneo4j.instance="${NEO4J_INSTANCE}" org.neo4j.server.Bootstrapper >> "${NEO4J_INSTANCE}/data/log/console.log" 2>&1 & echo $! > "${PID_FILE}"
- fi
- STARTED_PID=$( cat "$PID_FILE" )
- printf " process [$STARTED_PID]... waiting for server to be ready."
- x=0
- while ( [ $x -lt 120 ] ) ; do
- ## wait for start
- ## This could be achieved with filtering using -sTCP:LISTEN but this option is not available
- ## on lsof v4.78 which is the one bundled with some distros. So we have to do this grep below
- newpid=$(lsof -i :$NEO4J_SERVER_PORT -F T -Ts | grep -i "TST=LISTEN" -B1 | head -n1)
- newpid=${newpid:1}
- if [ $newpid ] ; then
- break
- fi
- kill -0 $STARTED_PID 2> /dev/null || break
- printf "."
- sleep 1
- x=$[$x +1]
- done
- if kill -0 $STARTED_PID 2>/dev/null ; then
- if [ "$newpid" != "$STARTED_PID" ] ; then
- rm "$PID_FILE"
- kill -9 $STARTED_PID
- echo " BAD."
- echo " another server-process is running with [$newpid]"
- exit 2
- fi
- echo " OK."
- exit 0
- fi
- echo " BAD."
- echo "Server may have failed to start, please check the logs."
- rm "$PID_FILE"
- exit 1
- else
- echo "Server already running with pid $NEO4J_PID"
- exit 1
- fi
- }
- stopit() {
- detectos
- checkstatus
- checkwriteaccess
- if [ $DIST_OS = "macosx" ] ; then
- getlaunchdpid
- if [ $LAUNCHDPID -gt 0 ] ; then
- echo "Instance running via launchd with PID $LAUNCHDPID, stopping it..."
- launchctl stop org.neo4j.server
- return
- fi
- # We fall through here since if there is no launchd install we stop manually started instance
- fi
- if [ -z $NEO4J_PID ] ; then
- echo "ERROR: server not running"
- [ -e "$PID_FILE" ] && rm "$PID_FILE"
- else
- printf "Stopping neo4j-service [$NEO4J_PID]..."
- x=0
- while [ $x -lt $TIMEOUT ] && [ "$NEO4J_PID" != "" ] ; do
- kill $NEO4J_PID 2>/dev/null
- printf "."
- sleep 1
- checkstatus
- x=$[$x+1]
- done
- [ $x -eq $TIMEOUT ] && ( echo " force shutdown" ; kill -9 $NEO4J_PID >/dev/null ) || echo " done"
- [ -e "$PID_FILE" ] && rm "$PID_FILE"
- fi
- }
- reportstatus() {
- checkstatus
- detectos
- exitonnojava
- if [ $DIST_OS = "macosx" ] ; then
- getlaunchdpid
- if [ $LAUNCHDPID -gt 0 ] ; then
- echo "Instance running via launchd with PID $LAUNCHDPID"
- exit 0
- fi
- # We fall through here since if there is no launchd install we may be still running via script
- fi
- if [ -z $NEO4J_PID ] ; then
- echo "Neo4j is not running"
- else
- echo "Neo4j is running at pid $NEO4J_PID"
- fi
- }
- console() {
- checkstatus
- if [ -z $NEO4J_PID ] ; then
- echo "Starting neo4j-service console-mode..."
- exitonnojava
- buildclasspath
- $JAVACMD -cp "${CLASSPATH}" $JAVA_OPTS -Dlog4j.configuration=file:conf/log4j.properties -Dorg.neo4j.server.properties="${NEO4J_INSTANCE}"/conf/neo4j-server.properties -Djava.util.logging.config.file="${NEO4J_INSTANCE}/conf/logging.properties" -Dneo4j.home="${NEO4J_HOME}" -Dneo4j.instance="${NEO4J_INSTANCE}" org.neo4j.server.Bootstrapper
- else
- echo "Server already running with pid $NEO4J_PID"
- exit 1
- fi
- }
- # Modifies neo4j config to set the effective user when running as a service
- # usage: modify_user_config <username> <created=false>
- # pass in "true" for created to mark that the user was created (instead than pre-existing)
- modify_user_config() {
- created=${2:-"false"}
- if `grep -q "wrapper\.user=" "${NEO4J_INSTANCE}/conf/neo4j-wrapper.conf"` ; then
- sed -i -e "s/^.*wrapper\.user=.*$/wrapper\.user=$1/" "${NEO4J_INSTANCE}/conf/neo4j-wrapper.conf"
- else
- echo "wrapper.user=$1" >>"${NEO4J_INSTANCE}/conf/neo4j-wrapper.conf"
- fi
- if `grep -q "wrapper\.user\.created=" "${NEO4J_INSTANCE}/conf/neo4j-wrapper.conf"` ; then
- sed -i -e "s/^.*wrapper\.user\.created=.*$/wrapper\.user\.created=${created}/" "${NEO4J_INSTANCE}/conf/neo4j-wrapper.conf"
- else
- echo "wrapper.user.created=${created}" >> "${NEO4J_INSTANCE}/conf/neo4j-wrapper.conf"
- fi
- }
- # Creates a user on the system
- create_user() {
- useradd -r $1
- modify_user_config $1 "true"
- echo "Created user \"${1}\"."
- }
- remove_user() {
- if [[ $wrapper_user_created == "true" ]]; then
- read -p "It appears that user \"${wrapper_user}\" was created during install. Remove that user now? [yN] " yn
- case $yn in
- [Yy]* ) deluser $wrapper_user
- ;;
- * ) echo "OK, we'll let that user live, for now."
- ;;
- esac
- else
- echo "Neo4j was running as user \"${wrapper_user}\", which remains."
- fi
- }
- installservice() {
- detectos
- findjava
- if [ ! -x "$JAVACMD" ] ; then
- read -p "It was not possible to determine JAVA_HOME. Do you want to continue with the installation? [yN]" yn
- case $yn in
- [yY]* ) echo "WARNING: Alright, but Neo4j will fail to launch until Java has been properly installed and configured."
- ;;
- * ) echo "Aborting installation."
- exit 1
- ;;
- esac
- fi
- if [[ -d "/etc/init.d" ]]; then
- if [[ -e "/etc/init.d/${SERVICE_NAME}" ]]; then
- echo "${SERVICE_NAME} already installed."
- else
- if [ $UID != 0 ] ; then
- eval echo 'Must be root to perform this action.'
- exit 1
- else
- checkstatus
- if [ ! -z $NEO4J_PID ] ; then
- stopit
- fi
- if [[ $NEO4J_USER == $wrapper_user ]]; then
- default_user=$wrapper_user
- else
- default_user="neo4j"
- fi
- read -p "Graph-like power should be handled carefully. What user should run Neo4j? [$default_user] " proposed_user
- proposed_user=${proposed_user:-$default_user}
- if ! `id $proposed_user &> /dev/null` ; then
- read -p "User \"$proposed_user\" does not yet exist. Shall I create the account for you? [Yn]" yn
- case $yn in
- [Nn]* ) echo "WARNING: Alright, but Neo4j will fail to launch until that user has been created."
- set_user $proposed_user
- ;;
- * ) create_user $proposed_user
- ;;
- esac
- else
- modify_user_config $proposed_user
- fi
- ln -s "${SCRIPT_NAME}" "/etc/init.d/${SERVICE_NAME}"
- update-rc.d ${SERVICE_NAME} defaults
- chown -R $proposed_user: "$NEO4J_HOME/data"
- fi
- fi
- elif [[ $DIST_OS -eq "macosx" ]] ; then
- sed -e "s^NEO4J_INSTANCE^'${NEO4J_INSTANCE}'^" <'${NEO4J_INSTANCE}/bin/org.neo4j.server.plist' >'${LAUNCHD_DIR}/org.neo4j.server.plist'
- launchctl load -w ${LAUNCHD_DIR}/org.neo4j.server.plist
- launchctl start org.neo4j.server
- else
- echo "Sorry, don't know how to install on ${DIST_OS}."
- fi
- if [ -x "$JAVACMD" ] ; then
- if [[ $DIST_OS -ne "macosx" ]] ; then
- startit
- fi
- fi
- }
- removeservice() {
- checkstatus
- if [[ -d "/etc/init.d" ]]; then
- if [[ -e "/etc/init.d/${SERVICE_NAME}" ]]; then
- if [ $UID != 0 ] ; then
- eval echo 'Must be root to perform this action.'
- exit 1
- else
- if [ ! -z $NEO4J_PID ] ; then
- stopit
- fi
- update-rc.d -f ${SERVICE_NAME} remove
- rm /etc/init.d/${SERVICE_NAME}
- fi
- # remove effective user, if created by install-script
- remove_user
- else
- echo "${SERVICE_NAME} not installed."
- fi
- elif [[ $DIST_OS -eq "macosx" ]] ; then
- if [[ -e ${LAUNCHD_DIR}/org.neo4j.server.plist ]]; then
- launchctl unload -w ${LAUNCHD_DIR}/org.neo4j.server.plist
- rm ${LAUNCHD_DIR}/org.neo4j.server.plist
- fi
- else
- echo "Sorry, don't know how to install on ${DIST_OS}."
- fi
- }
- showinfo() {
- reportstatus
- exitonnojava
- buildclasspath
- echo "NEO4J_HOME: $NEO4J_HOME"
- echo "NEO4J_SERVER_PORT: $NEO4J_SERVER_PORT"
- echo "NEO4J_INSTANCE: $NEO4J_INSTANCE"
- echo "JAVA_HOME: $JAVA_HOME"
- echo "JAVA_OPTS: $JAVA_OPTS"
- echo "CLASSPATH: $CLASSPATH"
- }
- case "$1" in
- console)
- console
- ;;
- start)
- startit
- ;;
- stop)
- stopit
- ;;
- restart)
- stopit
- startit
- ;;
- status)
- reportstatus
- ;;
- info)
- showinfo
- ;;
- install)
- installservice
- ;;
- remove)
- removeservice
- ;;
- *)
- echo "Usage: neo4j { start | stop | restart | status | info | install | remove }"
- exit 1;;
- esac
- exit $?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement