环境
系统:Linux(CentOS 7.2)
Erlang环境:21.1(安装参考【Erlang】源码安装)
RabbitMQ:3.7.9(安装参考【RabbitMQ】 RabbitMQ安装)
配置开机启动
1、增加自启动脚本:
在/etc/init.d目录下新建一个rabbitmq-server文件,内容如下:
1 #!/bin/sh 2 # 3 # rabbitmq-server RabbitMQ broker 4 # 5 # chkconfig: - 80 05 6 # description: Enable AMQP service provided by RabbitMQ 7 # 8 9 ### BEGIN INIT INFO 10 # Provides: rabbitmq-server 11 # Required-Start: $remote_fs $network 12 # Required-Stop: $remote_fs $network 13 # Description: RabbitMQ broker 14 # Short-Description: Enable AMQP service provided by RabbitMQ broker 15 ### END INIT INFO 16 17 # Source function library. 18 . /etc/init.d/functions 19 export HOME=/root 20 PATH=/sbin:/usr/sbin:/bin:/usr/bin 21 NAME=rabbitmq-server 22 #DAEMON=/usr/sbin/${NAME} 23 #CONTROL=/usr/sbin/rabbitmqctl 24 DAEMON=/server/rabbitmq/sbin/${NAME} 25 CONTROL=/server/rabbitmq/sbin/rabbitmqctl 26 DESC=rabbitmq-server 27 USER=root 28 ROTATE_SUFFIX= 29 INIT_LOG_DIR=/var/log/rabbitmq 30 PID_FILE=/var/run/rabbitmq/pid 31 32 START_PROG="daemon" 33 LOCK_FILE=/var/lock/subsys/$NAME 34 35 test -x $DAEMON || exit 0 36 test -x $CONTROL || exit 0 37 38 RETVAL=0 39 set -e 40 41 [ -f /etc/default/${NAME} ] && . /etc/default/${NAME} 42 43 ensure_pid_dir () { 44 PID_DIR=`dirname ${PID_FILE}` 45 if [ ! -d ${PID_DIR} ] ; then 46 mkdir -p ${PID_DIR} 47 chown -R ${USER}:${USER} ${PID_DIR} 48 chmod 755 ${PID_DIR} 49 fi 50 } 51 52 remove_pid () { 53 rm -f ${PID_FILE} 54 rmdir `dirname ${PID_FILE}` || : 55 } 56 57 start_rabbitmq () { 58 status_rabbitmq quiet 59 if [ $RETVAL = 0 ] ; then 60 echo RabbitMQ is currently running 61 else 62 RETVAL=0 63 ensure_pid_dir 64 set +e 65 RABBITMQ_PID_FILE=$PID_FILE $START_PROG $DAEMON \ 66 > "${INIT_LOG_DIR}/startup_log" \ 67 2> "${INIT_LOG_DIR}/startup_err" \ 68 0<&- & 69 $CONTROL wait $PID_FILE >/dev/null 2>&1 70 RETVAL=$? 71 set -e 72 case "$RETVAL" in 73 0) 74 echo SUCCESS 75 if [ -n "$LOCK_FILE" ] ; then 76 touch $LOCK_FILE 77 fi 78 ;; 79 *) 80 remove_pid 81 echo FAILED - check ${INIT_LOG_DIR}/startup_\{log, _err\} 82 RETVAL=1 83 ;; 84 esac 85 fi 86 } 87 88 stop_rabbitmq () { 89 status_rabbitmq quiet 90 if [ $RETVAL = 0 ] ; then 91 set +e 92 $CONTROL stop ${PID_FILE} > ${INIT_LOG_DIR}/shutdown_log 2> ${INIT_LOG_DIR}/shutdown_err 93 RETVAL=$? 94 set -e 95 if [ $RETVAL = 0 ] ; then 96 remove_pid 97 if [ -n "$LOCK_FILE" ] ; then 98 rm -f $LOCK_FILE 99 fi 100 else 101 echo FAILED - check ${INIT_LOG_DIR}/shutdown_log, _err 102 fi 103 else 104 echo RabbitMQ is not running 105 RETVAL=0 106 fi 107 } 108 109 status_rabbitmq() { 110 set +e 111 if [ "$1" != "quiet" ] ; then 112 $CONTROL status 2>&1 113 else 114 $CONTROL status > /dev/null 2>&1 115 fi 116 if [ $? != 0 ] ; then 117 RETVAL=3 118 fi 119 set -e 120 } 121 122 rotate_logs_rabbitmq() { 123 set +e 124 $CONTROL rotate_logs ${ROTATE_SUFFIX} 125 if [ $? != 0 ] ; then 126 RETVAL=1 127 fi 128 set -e 129 } 130 131 restart_running_rabbitmq () { 132 status_rabbitmq quiet 133 if [ $RETVAL = 0 ] ; then 134 restart_rabbitmq 135 else 136 echo RabbitMQ is not runnning 137 RETVAL=0 138 fi 139 } 140 141 restart_rabbitmq() { 142 stop_rabbitmq 143 start_rabbitmq 144 } 145 146 case "$1" in 147 start) 148 echo -n "Starting $DESC: " 149 start_rabbitmq 150 echo "$NAME." 151 ;; 152 stop) 153 echo -n "Stopping $DESC: " 154 stop_rabbitmq 155 echo "$NAME." 156 ;; 157 status) 158 status_rabbitmq 159 ;; 160 rotate-logs) 161 echo -n "Rotating log files for $DESC: " 162 rotate_logs_rabbitmq 163 ;; 164 force-reload|reload|restart) 165 echo -n "Restarting $DESC: " 166 restart_rabbitmq 167 echo "$NAME." 168 ;; 169 try-restart) 170 echo -n "Restarting $DESC: " 171 restart_running_rabbitmq 172 echo "$NAME." 173 ;; 174 *) 175 echo "Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}" >&2 176 RETVAL=1 177 ;; 178 esac 179 180 exit $RETVAL
2、根据实际情况修改上面内容中
1 # 增加了Erlang环境路径 2 PATH=/sbin:/usr/sbin:/bin:/usr/bin:/data/soft/erlang_21.1/bin 3 4 # 服务脚本 5 DAEMON=/server/rabbitmq/sbin/${NAME} 6 7 CONTROL=/server/rabbitmq/sbin/rabbitmqctl 8 9 DESC=rabbitmq-server 10 11 USER=root 12 13 # 日志目录 14 INIT_LOG_DIR=/data/soft/rabbitmq_server-3.7.9/var/log/rabbitmq 15 16 # pid文件,查找命令命令:find / -name rabbit*.pid 17 PID_FILE=/data/soft/rabbitmq_server-3.7.9/var/lib/rabbitmq/mnesia/rabbit@H__D.pid
3、设置执行权限,命令:chmod a+x /etc/init.d/rabbitmq-server
4、添加开机启动项
a、添加命令:chkconfig --add /etc/init.d/rabbitmq-server
b、查看启动项,命令:chkconfig --list
rabbitmq-server启动项状态:rabbitmq-server 0:off 1:off 2:off 3:off 4:off 5:off 6:off
c、需要设置rabbitmq-server启动命令:chkconfig rabbitmq-server on
rabbitmq-server启动项状态:rabbitmq-server 0:off 1:off 2:on 3:on 4:on 5:on 6:off
d、关闭命令为:chkconfig rabbitmq-server off
e、删除命令为:chkconfig --del rabbitmq-server
5、执行命令reboot,重启服务器即可验证rabbitmq-server开机启动