创建一个/etc/init.d/mongo-connector.start文件,内容如下:

# path to mongo-connector
mc="/usr/bin/mongo-connector"
# options to pass to mongo-connector
mongo_host="$(ifconfig | grep -A 1 'eth0' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1)"
mongo_port=27017
elastic_host="$(ifconfig | grep -A 1 'eth0' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1)"
elastic_port=9200
log_file=/var/log/mongodb/mongo-connector.log
mongo_dbname=laravel_project_db
mongo_collection=logs
mc_options="-m $mongo_host:$mongo_port -t http://$elastic_host:$elastic_port -d elastic2_doc_manager -n $mongo_dbname.$mongo_collection"
# log path
mc_log="/var/log/mongodb/mongo-connector.log"

pidfile=/var/run/mongo-connector.pid

nohup $mc $mc_options > $mc_log 2>&1 &
echo $! > $pidfile

创建一个/etc/init.d/mongo-connector文件,内容如下:

#!/bin/bash
#
# mongo-connector               Start Mongo Connector
#
# chkconfig: 345 90 25
# description: Mongo Connector replicates data from MongoDB to external
#              database systems.

### BEGIN INIT INFO
# Provides: mongo-connector
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start up Mongo Connector
# Description: Mongo Connector replicates data from MongoDB to external
#              database systems.
### END INIT INFO

# path to wrapper script
wrapper=/etc/init.d/mongo-connector.start

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0
lockfile=/var/run/mongo-connector.lock
pidfile=/var/run/mongo-connector.pid

start()
{
    [ -x $mc ] || exit 5

    action "starting mongo-connector: " daemon --pidfile $pidfile $wrapper
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        echo "done."
    else
        echo "failed. Please check exit code and logs for more information"
    fi
    return $RETVAL
}

stop()
{
    action "stopping mongo-connector: " killproc -p $pidfile
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        echo "done."
        rm -f $lockfile
    else
        echo "failed. Please check exit code and logs for more information"
    fi
    return $RETVAL
}

restart() {
    $0 stop
    $0 start
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status -p $pidfile mongo-connector
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=2
esac
exit $RETVAL

设置权限:

[root@distribute01 ~]# chmod +x /etc/init.d/mongo-connector.start
[root@distribute01 ~]# chmod +x /etc/init.d/mongo-connector

添加到系统服务:

[root@distribute01 ~]# chkconfig --add /etc/init.d/mongo-connector

测试:

[root@distribute01 ~]# service mongo-connector start