neo4j 启动脚本 centos 7 x64

centos 7 x64, neo4j 3.1.2 community
下载的 neo4j 默认不带启动脚本,neo4j文档只给了几个示例教程链接。其中源码里有打包用的链接示例 neo4j.service 3.1, neo4j.init 3.1

对照写出脚本:

neo4j.service /etc/systemd/system/neo4j.service

[Unit]
Description=Neo4j Graph Database
After=network.target

[Service]
Type=forking
User=neo4j
Group=neo4j
ExecStart=/home/software/neo4j-community-3.1.2/bin/neo4j start
ExecStop=/home/software/neo4j-community-3.1.2/bin/neo4j stop
ExecReload=/home/software/neo4j-community-3.1.2/bin/neo4j restart
RemainAfterExit=no
Restart=on-failure
PIDFile = /home/software/neo4j-community-3.1.2/run/neo4j.pid
Environment="NEO4J_CONF=/home/software/neo4j-community-3.1.2/conf" "NEO4J_HOME=/home/software/neo4j-community-3.1.2"
LimitNOFILE=60000
TimeoutSec=600

[Install]
WantedBy=multi-user.target

neo4j /etc/init.d/neo4j

#!/bin/sh

### BEGIN REDHAT INFO
# chkconfig: 2345 99 20
# description: Neo4j Graph Database server
### END REDHAT INFO
### BEGIN INIT INFO
# Provides:          neo4j
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Neo4j Graph Database server
# Description:       Neo4j is a Graph Database, which is a compelling
#                    alternative to an RDBMS. http://www.neo4j.org
### END INIT INFO

# Author: Julian Simpson <julian.simpson@neotechnology.com>
#
# Copyright (c) 2002-2016 "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/>

SCRIPTNAME=$0

NEO4J_CONF=/home/software/neo4j-community-3.1.2/conf
NEO4J_HOME=/home/software/neo4j-community-3.1.2
NEO_USER=neo4j
NEO4J_ULIMIT_NOFILE=60000

PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=neo4j
DAEMON=${NEO4J_HOME}/bin/${NAME}
PIDDIR=${NEO4J_HOME}/run
PIDFILE=${PIDDIR}/neo4j.pid
SCRIPTNAME=/etc/init.d/${NAME}

SYSTEMCTL_SKIP_REDIRECT=1

[ -x "$DAEMON" ] || exit 0
#[ -r ${NEO4J_CONF}/${NAME}.conf ] && . ${NEO4J_CONF}/${NAME}.conf
[ -n "${NEO_USER}" ] || NEO_USER=${NAME}

# Debian distros and SUSE
has_lsb_init()
{
  test -f "/lib/lsb/init-functions"
}

# RedHat/Centos distros
has_init()
{
  test -f "/etc/init.d/functions"
}

if has_lsb_init ; then
  . /lib/lsb/init-functions
elif has_init ; then
  . /etc/init.d/functions
else
  echo "Error: your platform is not supported by ${NAME}" >&2
  exit 1
fi
do_start()
{
  do_ulimit
  [ -d "${PIDDIR}" ] || mkdir -p "${PIDDIR}"
  chown "${NEO_USER}:" "${PIDDIR}"

  if has_lsb_init ; then
    start-stop-daemon --chuid ${NEO_USER} --start --quiet --oknodo --pidfile ${PIDFILE} --exec ${DAEMON} -- start
  else
    daemon --user="${NEO_USER}" --pidfile="${PIDFILE}" "${DAEMON} start > /dev/null 2>&1 &"
  fi
}

do_stop()
{
  ${DAEMON} stop
}

do_status()
{
  if has_lsb_init ; then
    status_of_proc -p "${PIDFILE}" "${DAEMON}" "${NAME}"
  else
    status -p "${PIDFILE}" "${NAME}"
  fi
}

do_ulimit()
{
  if [ -n "${NEO4J_ULIMIT_NOFILE}" ]; then
    ulimit -n "${NEO4J_ULIMIT_NOFILE}"
  fi
}

case "$1" in
  start)
    do_start
    ;;
  stop)                                                          
    do_stop
    ;;
  status)
    do_status
    ;;
  restart|force-reload)
    do_stop && do_start
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
    exit 3
    ;;
esac

注意:centos 7 默认使用 systemctl进行管理,执行 service start neo4j 或者 /etc/init.d/neo4j 会默认重定向给 systemctl。其它程序都会重定向,例如 service httpd start 会提示 Redirecting to /bin/systemctl start httpd.service,实际是通过 systemclt的脚本启动 httpd。上面 /etc/init.d/neo4j脚本重定向在执行了 . /etc/init.d/functions后生效。从这个文件内容也可以看出:

/etc/init.d/functions:

if [ $PPID -ne 1 -a -z "$SYSTEMCTL_SKIP_REDIRECT" ] && \
        ( /bin/mountpoint -q /cgroup/systemd || /bin/mountpoint -q /sys/fs/cgroup/systemd ) ; then
        case "$0" in
        /etc/init.d/*|/etc/rc.d/init.d/*)
        _use_systemctl=1

因此要只使用 /etc/init.d/neo4j脚本的内容,要使用 SYSTEMCTL_SKIP_REDIRECT=1 跳过重定向语句。

但是 centos 7 应推荐使用 systemctl的方式, /etc/init.d/是为了兼容性而存在。

[1] https://github.com/neo4j/neo4j/issues/9095
[2] https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html
[3] http://unix.stackexchange.com/questions/160244/service-command-does-not-work-in-centos-7
[4] https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet
[5] https://www.graphgrid.com/systemd-neo4j-ubuntu/
[6] http://neo4j.com/docs/operations-manual/3.1/installation/linux/tarball/
[7] https://www.linux.com/learn/managing-linux-daemons-init-scripts
[8] /usr/share/doc/initscripts-9.49.30/sysvinitfiles
[9] https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值