简体   繁体   English

运行 docker-compose 的 Linux At 命令

[英]Linux At Command running docker-compose

Goal: I am trying to schedule a 'docker-compose up' at a scheduled time目标:我正在尝试在预定时间安排“docker-compose up”

Operating System: Linux Ubuntu 18.04操作系统:Linux Ubuntu 18.04

I have tried using the 'at' command in linux but I am trying to set variables and couldn't get it to quite work.我曾尝试在 linux 中使用“at”命令,但我正在尝试设置变量并且无法让它正常工作。

Attempts below:下面的尝试:

sudo echo "KEY='VALUE' DIRECTION='D' sh run.sh" | at now + 2 minute
# Above does not run the docker-compose

sudo echo "KEY='VALUE' DIRECTION='D' sh run.sh" | at now + 2 minute
# Above runs docker-compose command immediately but not in 2 minutes
# run.sh

# SET LOGFILE LOCATION
LOG_FILE="/var/log/at/test.log"

FLIGHT_FILE="flight.yaml" docker-compose up --abort-on-container-exit >> "${LOG_FILE}"

If there is a better way than using the 'at' command I am also open to that.如果有比使用“at”命令更好的方法,我也愿意接受。

Thank you in advance先感谢您

in your example (both attempts are the same) only the echo is executed by sudo , at is executed as your user.在您的示例中(两次尝试都相同)只有echosudo执行, at以您的用户身份执行。

options:选项:

run the script on the root's at queue:在根的at队列上运行脚本:

sudo sh -c "echo \"KEY='VALUE' DIRECTION='D' sh /home/user/run.sh \" | at now + 1 min"

make at run sudo - works only if you have sudo setup not to ask a password: make at run sudo - 仅当您设置了sudo不询问密码时才有效:

echo "sudo sh -c \"KEY='VALUE' DIRECTION='D' /home/user/run.sh\"" | at now + 2 min

configure docker so you don't need sudo配置 docker,这样你就不需要sudo

There are 2 possible approaches -有两种可能的方法 -

Single file-单个文件-

Create a file /etc/systemd/system/dc-test.service创建文件/etc/systemd/system/dc-test.service

[Unit]
Description=My Docker Compose runner

[Service]
User=%I
Restart=always
RestartSec=600s
ExecStart=/bin/bash /home/user/run.sh

Above will run the script ever 600 seconds.以上将每 600 秒运行一次脚本。

Multi File (with service timers)多文件(带服务定时器)

You need to create two files, one for service, other for timer with same name.您需要创建两个文件,一个用于服务,另一个用于具有相同名称的计时器。

example:例子:

/etc/systemd/system/dc-test.service /etc/systemd/system/dc-test.service

[Unit]
Description=My Docker Compose runner

[Service]
Type=oneshot
ExecStart=/bin/bash /home/user/run.sh

/etc/systemd/system/dc-test.timer /etc/systemd/system/dc-test.timer

[Unit]
Description=Docker Compose Timer

[Timer]
OnUnitActiveSec=600s
OnBootSec=600s

[Install]
WantedBy=timers.target

In both the approaches you will need to run systemctl daemon-reload after you are done creating files.在这两种方法中,您都需要在完成创建文件后运行systemctl daemon-reload

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

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