RabbitMQ开机自启动

liym 2018-05-23

如通过yum安装的话直接通过chkconfigrabbitmq-serveron就可以设置为开机自启动,但公司这边是通过源代码进行安装,如何也通过系统服务来自启动呢?

步骤:

1.增加自启动脚本:

在/etc/init.d目录下新建一个rabbitmq-server文件,内容如下:

#!/bin/sh

#

#rabbitmq-serverRabbitMQbroker

#

#chkconfig:-8005

#description:EnableAMQPserviceprovidedbyRabbitMQ

#

###BEGININITINFO

#Provides:rabbitmq-server

#Required-Start:$remote_fs$network

#Required-Stop:$remote_fs$network

#Description:RabbitMQbroker

#Short-Description:EnableAMQPserviceprovidedbyRabbitMQbroker

###ENDINITINFO

#Sourcefunctionlibrary.

./etc/init.d/functions

exportHOME=/root

PATH=/sbin:/usr/sbin:/bin:/usr/bin

NAME=rabbitmq-server

#DAEMON=/usr/sbin/${NAME}

#CONTROL=/usr/sbin/rabbitmqctl

DAEMON=/server/rabbitmq/sbin/${NAME}

CONTROL=/server/rabbitmq/sbin/rabbitmqctl

DESC=rabbitmq-server

USER=root

ROTATE_SUFFIX=

INIT_LOG_DIR=/var/log/rabbitmq

PID_FILE=/var/run/rabbitmq/pid

START_PROG="daemon"

LOCK_FILE=/var/lock/subsys/$NAME

test-x$DAEMON||exit0

test-x$CONTROL||exit0

RETVAL=0

set-e

[-f/etc/default/${NAME}]&&./etc/default/${NAME}

ensure_pid_dir(){

PID_DIR=`dirname${PID_FILE}`

if[!-d${PID_DIR}];then

mkdir-p${PID_DIR}

chown-R${USER}:${USER}${PID_DIR}

chmod755${PID_DIR}

fi

}

remove_pid(){

rm-f${PID_FILE}

rmdir`dirname${PID_FILE}`||:

}

start_rabbitmq(){

status_rabbitmqquiet

if[$RETVAL=0];then

echoRabbitMQiscurrentlyrunning

else

RETVAL=0

ensure_pid_dir

set+e

RABBITMQ_PID_FILE=$PID_FILE$START_PROG$DAEMON\

>"${INIT_LOG_DIR}/startup_log"\

2>"${INIT_LOG_DIR}/startup_err"\

0<&-&

$CONTROLwait$PID_FILE>/dev/null2>&1

RETVAL=$?

set-e

case"$RETVAL"in

0)

echoSUCCESS

if[-n"$LOCK_FILE"];then

touch$LOCK_FILE

fi

;;

*)

remove_pid

echoFAILED-check${INIT_LOG_DIR}/startup_\{log,_err\}

RETVAL=1

;;

esac

fi

}

stop_rabbitmq(){

status_rabbitmqquiet

if[$RETVAL=0];then

set+e

$CONTROLstop${PID_FILE}>${INIT_LOG_DIR}/shutdown_log2>${INIT_LOG_DIR}/shutdown_err

RETVAL=$?

set-e

if[$RETVAL=0];then

remove_pid

if[-n"$LOCK_FILE"];then

rm-f$LOCK_FILE

fi

else

echoFAILED-check${INIT_LOG_DIR}/shutdown_log,_err

fi

else

echoRabbitMQisnotrunning

RETVAL=0

fi

}

status_rabbitmq(){

set+e

if["$1"!="quiet"];then

$CONTROLstatus2>&1

else

$CONTROLstatus>/dev/null2>&1

fi

if[$?!=0];then

RETVAL=3

fi

set-e

}

rotate_logs_rabbitmq(){

set+e

$CONTROLrotate_logs${ROTATE_SUFFIX}

if[$?!=0];then

RETVAL=1

fi

set-e

}

restart_running_rabbitmq(){

status_rabbitmqquiet

if[$RETVAL=0];then

restart_rabbitmq

else

echoRabbitMQisnotrunnning

RETVAL=0

fi

}

restart_rabbitmq(){

stop_rabbitmq

start_rabbitmq

}

case"$1"in

start)

echo-n"Starting$DESC:"

start_rabbitmq

echo"$NAME."

;;

stop)

echo-n"Stopping$DESC:"

stop_rabbitmq

echo"$NAME."

;;

status)

status_rabbitmq

;;

rotate-logs)

echo-n"Rotatinglogfilesfor$DESC:"

rotate_logs_rabbitmq

;;

force-reload|reload|restart)

echo-n"Restarting$DESC:"

restart_rabbitmq

echo"$NAME."

;;

try-restart)

echo-n"Restarting$DESC:"

restart_running_rabbitmq

echo"$NAME."

;;

*)

echo"Usage:$0{start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}">&2

RETVAL=1

;;

esac

exit$RETVAL

根据实际情况修改上面内容中的:

DAEMON=/server/rabbitmq/sbin/${NAME}

CONTROL=/server/rabbitmq/sbin/rabbitmqctl

DESC=rabbitmq-server

USER=root

2.保存上面的文件并设置执行权限

chmodu+x/etc/init.d/rabbitmq-server

3.配置为开机自启动服务

ln-s$(whicherl)/usr/bin/erl&&chkconfigrabbitmq-serveron

原文出自:

https://blog.csdn.net/weiguang1017/article/details/79065486

相关推荐