运行“docker run"来自 crontab [英] run "docker run" from crontab

查看:34
本文介绍了运行“docker run"来自 crontab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从在 docker 容器内运行的 postgresql 数据库进行自动(每晚 4 点)备份.

I try to make automated (every night at 4) backups from a postgresql database running inside a docker container.

#!/bin/sh

CONTAINER=`docker ps|grep name_of_container|awk '{print $1}'`
USER='postgre_user'
PASSWORD='changed'
BUDIR='/some/path/backup/'

docker run -it --link $CONTAINER:db -v $BUDIR:/backup/ -e "PGPASSWORD=$PASSWORD" pg_dump -h db -U $USER -Fc -f /backup/$(date +%Y-%m-%d-%H-%M-%S).dump

我的 crontab 看起来像这样:

My crontab looks like this:

0 4 * * * /path/to/script.sh

脚本在我手动执行时运行良好,并且它也从 cron 执行(我试过 * * * * * 进行调试).

The script works fine when I execute it manually and it also get executed from cron (I tried * * * * * for debugging).

我不知道如何让 cron 和脚本一起工作.到目前为止,我尝试过:

I can't figure out how to make cron and the script work together. So far I tried:

  • 将变量写入日志文件
  • 检查 crontab 的输出 (* * * * * [...] &>cron.log)
  • 检查 docker exec [...] > 的输出output.log 在脚本中
  • write variables to log file
  • check output from crontab (* * * * * [...] &>cron.log)
  • check output from docker exec [...] > output.log in script

$CONTAINER 在从 cron 运行时包含正确的 docker id,cron.log 和 output.log 已创建但为空.

$CONTAINER contains the correct docker id when run from cron, cron.log and output.log are created but empty.

有什么想法吗?

推荐答案

从 cron 运行时无法使用 docker run -it --link [...] - 我使用 docker run --link [...] 现在.

Can't use docker run -it --link [...] when running from cron - I use docker run --link [...] now.

这篇关于运行“docker run"来自 crontab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆