Search code examples
bashdaemonamqp

BASH daemonize script amqp-consumer not persistant


First, sorry for my english :) I'm trying to daemonize a script, which uses a bash AMQP client library (amqp-tools), to allow message forward through a RabbitMQ broker.

Here is a part of the script encrypter_mom_mailconsumer.sh

write_log INF "Forward broker message to encrypter_mom_mailencrypter.sh"
/usr/bin/amqp-consume \
    --server broker1.rmq:5672 \
    --vhost "/" \
    --username ub1v1 --password aze \
    --exchange amq.direct \
    --routing-key to_encrypt \
    --declare \
    --queue q_encrypt \
    . /home/rabbit/encrypter_mom_mailencrypter.sh

When i launch that script within a shell (within my vm), everything is fine ! amqp-consume freeze the script and awaits for a AMPQ message.

But when the script starts on boot, as a daemon, amqp-consume just skip that step. the scripts ends its execution... and neither TCP connection opened (netstat -tap) nor processus (ps aufx)

here is the daemon script with start-stop-daemon usage

DAEMON="/home/rabbit/encrypter_mom_mailconsumer.sh"
DAEMON_OPT=""
DAEMON_USER="rabbit"
DAEMON_NAME="encrypter_mom_mailconsumer.sh"

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

test -x $DAEMON || exit 0
. /lib/lsb/init-functions

d_start () {
    log_daemon_msg "Starting system $DAEMON_NAME Daemon"
    start-stop-daemon --background --start --quiet --chuid $DAEMON_USER --exec $DAEMON -- $DAEMON_OPT
    log_end_msg $?
}

d_stop () {
    log_daemon_msg "Stopping system $DAEMON_NAME Daemon"
    start-stop-daemon --name $DAEMON_NAME --stop --retry 5 --quiet --name $DAEMON_NAME
    log_end_msg $?
}

case "$1" in
    start|stop)
        d_${1}
        ;;

    restart|reload|force-reload)
        d_stop
        d_start
        ;;

    force-stop)
        d_stop
        killall -q $DAEMON_NAME || true
        sleep 2
        killall -q -9 $DAEMON_NAME || true
        ;;

    status)
        status_of_proc "$DAEMON_NAME" "$DAEMON" "system-wide $DAEMON_NAME" && exit 0 || exit $?
        ;;
    *)
        echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
        exit 1
        ;;
esac
exit 0

The logs of encrypter_mom_mailconsumer.sh (at vm startup) - he should stop and wait at "Forward broker message to encrypter_mom_mailencrypter.sh" step

2015/03/13 15:15:25 [START] : Executing encrypter_mom_mailconsumer...
2015/03/13 15:15:25 [INFO] : Check script arguments and owner
2015/03/13 15:15:25 [INFO] : Forward broker message to encrypter_mom_mailencrypter.sh
2015/03/13 15:15:27 [INFO] : Status: 1
2015/03/13 15:15:27 [END] : Successfully executed

I do not understand the problem. Do you have some tips ?

If i miss some informations, please tell me !


Solution

  • I had to nohup my amqp-consume command,, and launch it as a background processus with &.

    And i just had to take care to the way i stopped my VMs, rabbitmq needs to clean its exchanges and queues.

    So:

    write_log INF "Forward broker message to encrypter_mom_mailencrypter.sh"
    nohup /usr/bin/amqp-consume \
        --server broker1.rmq:5672 \
        --vhost "/" \
        --username ub1v1 --password aze \
        --exchange amq.direct \
        --routing-key to_encrypt \
        --declare \
        --queue q_encrypt \
        /bin/bash /home/rabbit/encrypter_mom_mailencrypter.sh &
    

    did the job !

    :)