问题原文
原帖链接 · BenMorel · CC BY-SA 4.0
I have a script that installs and sets up traffic server:
yum install -y trafficserver
systemctl start trafficserver
traffic_line -s proxy.config.url_remap.remap_required -v 0
traffic_line -s proxy.config.reverse_proxy.enabled -v 0
The problem is, traffic_line fails with:
[connect] ERROR (main_socket_fd 3): No such file or directory error: could not connect to management port, make sure traffic_manager is running
This is because systemctl start returns immediately, without waiting for traffic server to be actually started.
Is there a way to tell systemctl start to only return once the service is started?
If this is not possible, is there a command that I can run after systemctl start to actually wait for the service to be started?
已采纳解答
原帖链接 · Lucas Werkmeister · CC BY-SA 4.0
This is because
systemctl startreturns immediately, without waiting for traffic server to be actually started.Is there a way to tell
systemctl startto only return once the service is started?
systemctl start does wait for the service to be ready (except if invoked with --no-block), the service just needs to indicate that properly (i. e., not use Type=simple). If the service doesn’t tell systemd when it’s ready, no variation of systemctl is-active, systemctl show, etc. will help you.
The most elegant solution, as mentioned in the comments, would be a socket unit. systemd starts the socket, traffic_line connects to it, systemd starts the service, and traffic_line blocks until the service starts to accept connections on the file descriptor it inherited from systemd.
Alternatively, you can use either Type=forking (the service forks, and the main PID exits once the forked service is ready) or Type=notify (the service calls sd_notify(0, "READY=1") once it’s ready).
Unfortunately, all of these solutions require some support from trafficserver – use systemd’s socket instead of allocating its own, fork and wait appropriately in the main process, or call sd_notify. systemd can’t magically guess when the server is ready if the server doesn’t cooperate :)
After looking at trafficserver’s source code a bit, it looks like it might actually support Type=forking – the server is spawned by a dedicated traffic_cop command, which seems to wait until the server is up and perform some basic testing (at least the code looks like it). So if you change the service type, it might just work:
# /etc/systemd/system/trafficserver.service.d/type-forking.conf
[Service]
Type=forking
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
