use*_*555 32 bash cron meteor docker meteor-up
我有一个非常简单的命令,它作为一个命令或bash脚本独立工作,但当我把它放在crontab中时
40 05 * * * bash /root/scripts/direct.sh >> /root/cron.log
Run Code Online (Sandbox Code Playgroud)
有以下行
PATH=$PATH:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/root/
# Mongo Backup
docker exec -it mongodb mongodump -d meteor -o /dump/
Run Code Online (Sandbox Code Playgroud)
我试图改变脚本的网址,/usr/bin/scirpts/没有运气
我甚至试图直接在cron中运行脚本
26 08 * * * docker exec -it mongodb mongodump -d meteor -o /dump/ >> /root/cron.log
Run Code Online (Sandbox Code Playgroud)
没有运气,任何帮助表示赞赏.
编辑
我也没有在/root/cron.log文件中看到任何错误
VDR*_*VDR 85
您的docker exec命令说它需要"伪终端并以交互模式运行"(-it flags),而cron不会附加到任何TTY.
尝试将docker exec命令改为此命令,看看是否有效?
docker exec mongodb mongodump -d meteor -o /dump/
Run Code Online (Sandbox Code Playgroud)
/var/log or sendmailAs crond work as a daemon, without ability of failing, execution is more important than logging. Then by default, if something goes wrong, cron will send a mail to $USER@localhost reporting script output and errors.
Have a look at /var/mail or /var/spool/mail for some mails, maybe
and at /etc/aliases to see where root's mail are sents.
$PATHWhen you run a command by cron, have care that $PATH is user's default path and not root default path (ie no */sbin and other reserved path to super user tools).
For this, the simplier way is to print your default path in the environment where everything run fine:
echo $PATH
Run Code Online (Sandbox Code Playgroud)
or patch your script from command line:
sed -e "2aPATH='$PATH'" -i /root/scripts/direct.sh
Run Code Online (Sandbox Code Playgroud)
This will add current $PATH initializer at line 2 in your script.
Or this, will whipe from your script all other PATH=:
sed -e "s/PATH=[^ ]*\( \|$\)/\1/;2aPATH='$PATH'" -i /root/scripts/direct.sh
Run Code Online (Sandbox Code Playgroud)
Add at top of your script:
exec 1>/tmp/cronlog-$$.log
exec 2>/tmp/cronlog-$$.err
Run Code Online (Sandbox Code Playgroud)
Try this:
sed -e '1a\\nexec 1>/tmp/cronlog-$$.log\nexec 2>/tmp/cronlog-$$.err' -i ~/scripts/direct.sh
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
# uncomment two following lines to force log to /tmp
# exec 1>/tmp/cronlog-$$.log
# exec 2>/tmp/cronlog-$$.err
PATH='....' # copied from terminal console!
docker exec -it mongodb mongodump -d meteor -o /dump/
Run Code Online (Sandbox Code Playgroud)
If you run your script by
40 05 * * * bash /root/scripts/direct.sh
Run Code Online (Sandbox Code Playgroud)
no executable flag are required, but you must add them:
chmod +x ~/scripts/direct.sh
Run Code Online (Sandbox Code Playgroud)
if you want to run:
40 05 * * * /root/scripts/direct.sh
Run Code Online (Sandbox Code Playgroud)
小智 7
为了它的价值我有同样的问题.修复你的PATH,更改权限,并确保你作为适当的docker用户运行都是好事,但这还不够.它会继续失败,因为你正在使用"docker exec -it",它告诉docker使用交互式shell.将其更改为"docker exec -t",它将正常工作.但是,在任何地方都没有记录输出.请享用!
1) 确保此任务在root用户的 crontab 中 - 可能是这种情况,但您没有明确写入
2)cron可能无法找到bash。我会删除它并在使其可执行后直接调用您的脚本:
chmod 755 /root/scripts/direct.sh
Run Code Online (Sandbox Code Playgroud)
然后将您的 crontab 条目设置为 40 05 * * * /root/scripts/direct.sh 2>&1 >> /root/cron.log
如果它仍然不起作用,那么你应该有一些有用的输出 /root/cron.log
| 归档时间: |
|
| 查看次数: |
11952 次 |
| 最近记录: |