我在我的raspberry Pi上安装了一个RabbitMQ服务器,我想在同一个设备上运行一个消费者来处理我的一个队列的消息。我首先尝试从crontab执行它,但后来意识到作为systemd服务运行它可能是一个更好的主意。这是我第一次设置systemd服务,所以我可能会监督一些琐碎的事情。
我的queue_listener. service文件如下所示。
[Unit]
Description= queue_listener
Wants=rabbitmq-server.target
After=rabbitmq-server.target
[Service]
Type=simple
Restart=always
WorkingDirectory=<path>
User=<user>
ExecStart=python3 <path>/queue_listener.py
[Install]
WantedBy=multi-user.target
创建服务文件后,我通过运行以下命令启动了服务。
sudo systemctl daemon-reload
sudo systemctl enable queue_listener.service
sudo systemctl start queue_listener.service
服务启动,我可以在RabbitMQ管理界面中看到使用者。
但是当我重新启动我的设备时,服务没有启动。下面是重新启动后的syslog内容。
.
.
.
13:51:15 pi-server systemd[1]: Started queue_listener.
13:51:16 pi-server python3[1035]: Traceback (most recent call last):
13:51:16 pi-server python3[1035]: File "/home/pi/Projects/Pi-Project/Pi-Server/queue_listener.py", line 45, in <module>
13:51:16 pi-server python3[1035]: mq.consume("measurements",write_measurement_to_db)
13:51:16 pi-server python3[1035]: File "/home/pi/Projects/Pi-Project/Pi-Server/MQ_handler.py", line 25, in consume
13:51:16 pi-server python3[1035]: connection = pika.BlockingConnection(parameters)
13:51:16 pi-server python3[1035]: File "/home/pi/.local/lib/python3.7/site-packages/pika/adapters/blocking_connection.py", line 360, in __init__
13:51:16 pi-server python3[1035]: self._impl = self._create_connection(parameters, _impl_class)
13:51:16 pi-server python3[1035]: File "/home/pi/.local/lib/python3.7/site-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
13:51:16 pi-server python3[1035]: raise self._reap_last_connection_workflow_error(error)
13:51:16 pi-server python3[1035]: pika.exceptions.AMQPConnectionError
13:51:16 pi-server systemd[1]: queue_listener.service: Main process exited, code=exited, status=1/FAILURE
13:51:16 pi-server systemd[1]: queue_listener.service: Failed with result 'exit-code'.
13:51:16 pi-server systemd[1]: queue_listener.service: Service RestartSec=100ms expired, scheduling restart.
13:51:16 pi-server systemd[1]: queue_listener.service: Scheduled restart job, restart counter is at 10.
13:51:16 pi-server systemd[1]: Stopped queue_listener.
13:51:16 pi-server systemd[1]: queue_listener.service: Start request repeated too quickly.
13:51:16 pi-server systemd[1]: queue_listener.service: Failed with result 'exit-code'.
13:51:16 pi-server systemd[1]: Failed to start queue_listener.
.
.
.
13:51:37 pi-server rabbitmq-server[478]: ## ## RabbitMQ 3.8.7
13:51:37 pi-server rabbitmq-server[478]: ## ##
13:51:37 pi-server rabbitmq-server[478]: ########## Copyright (c) 2007-2020 VMware, Inc. or its affiliates.
13:51:37 pi-server rabbitmq-server[478]: ###### ##
13:51:37 pi-server rabbitmq-server[478]: ########## Licensed under the MPL 2.0. Website: https://rabbitmq.com
13:51:37 pi-server rabbitmq-server[478]: Doc guides: https://rabbitmq.com/documentation.html
13:51:37 pi-server rabbitmq-server[478]: Support: https://rabbitmq.com/contact.html
13:51:37 pi-server rabbitmq-server[478]: Tutorials: https://rabbitmq.com/getstarted.html
13:51:37 pi-server rabbitmq-server[478]: Monitoring: https://rabbitmq.com/monitoring.html
13:51:37 pi-server rabbitmq-server[478]: Logs: /var/log/rabbitmq/rabbit@pi-server.log
13:51:37 pi-server rabbitmq-server[478]: /var/log/rabbitmq/rabbit@pi-server_upgrade.log
13:51:37 pi-server rabbitmq-server[478]: Config file(s): (none)
13:51:41 pi-server rabbitmq-server[478]: Starting broker... completed with 3 plugins.
13:51:41 pi-server systemd[1]: Started RabbitMQ broker.
我的消费者服务似乎在RabbitMQ代理启动之前就开始了。我做错了什么?我应该用其他方法来处理这个问题吗?
1条答案
按热度按时间mspsb9vt1#
将
Wants
替换为Requires
,Wants
表示"好",而Requires
表示"必须"。Also, make sure the rabbitmq-server.service is in rabbitmq-server.target. If not, replace rabbitmq-server.target with rabbitmq-server.service
参考:https://www.freedesktop.org/software/systemd/man/systemd.unit.html