简体   繁体   English

群 | 启动服务只执行一次然后被删除

[英]Swarm | Start a service to execute just one time then be deleted

I have a Swarm cluster and I have a service that will just create a container, execute one script and then delete the container.我有一个 Swarm 集群,我有一个服务,它只会创建一个容器,执行一个脚本,然后删除容器。 And currently it works fine, the container execute the script then delete him-self after the execution.目前它工作正常,容器执行脚本,然后在执行后删除他自己。 But after a few seconds, the service restart the container and execute again the script.但几秒钟后,服务重新启动容器并再次执行脚本。 But I would like to delete the service after the first execution, like that the service don't do the job several times.但是我想在第一次执行后删除该服务,就像该服务多次不做这项工作一样。

My only idea is to in my first script that launch the service, I put a timer, like 4 seconds and after that i just delete the service in the script like that:我唯一的想法是在我的第一个启动服务的脚本中,我放了一个计时器,比如 4 秒,然后我就像这样删除脚本中的服务:

#!/bin/bash

docker stack deploy -c docker-compose.yaml database-preapre
sleep 4
docker service rm database-prepare

But if one day, the service make more time to execute his script and I cut the service when he is running.但是如果有一天,服务腾出更多时间来执行他的脚本,我在他运行时切断了服务。 Or if the service execute itself one time real fast and then after 3 seconds restart it-self for the second execution.或者,如果服务自己执行一次非常快,然后在 3 秒后重新启动它自己进行第二次执行。 I don't want to cut him...我不想砍他...

To resolve this issue, I just adjust some things in my script.为了解决这个问题,我只是在我的脚本中调整了一些东西。 First, in the script that create the temporary container, I just sleep 10 seconds and I destroy the container.首先,在创建临时容器的脚本中,我只睡了 10 秒,然后销毁了容器。 (10 seconds is very large but i want to be sure the container have the time to do the job) (10 秒非常大,但我想确保容器有时间完成这项工作)

Secondly, when the container finished executing the entrypoint script he will just deleted himself and restart (cause swarm restart it), but I don't want that my script go Up, down and Up and be cut during the second execution and destroy the job.其次,当容器完成执行入口点脚本时,他只会删除自己并重新启动(导致群重新启动它),但我不希望我的脚本 go 向上、向下和向上并在第二次执行期间被剪切并破坏作业. So I add a sleep timer to the script that my temporary container run, so he will don't go down and up and down and up... He executes his job and then wait.所以我在我的临时容器运行的脚本中添加了一个睡眠计时器,所以他不会 go 上下上下......他执行他的工作然后等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM