【问题标题】:Cronjob Not Running via Crontab -eCronjob 未通过 Crontab -e 运行
【发布时间】:2021-03-14 15:10:24
【问题描述】:

我尝试每天运行一个脚本来连接到我的 ESXi 主机,删除我所有虚拟机的所有快照,然后每天创建新快照。我试图通过在运行 Ubuntu 的 docker VM 上使用 VMWare PowerCLI docker 映像 (https://hub.docker.com/r/vmware/powerclicore) 在 docker 容器中运行脚本来做到这一点。

我可以通过在终端中运行以下命令来成功运行此脚本:

/usr/bin/docker run --rm -it --name=powerclicore --entrypoint="/usr/bin/pwsh" -v /home/<redacted>/config/powercli:/scripts vmware/powerclicore /scripts/VMSnapshot.ps1

但是,在通过crontab -e 将上述命令添加到我的 cronfile 后,我的作业没有运行。

  GNU nano 4.8                                                                                             /tmp/crontab.EZkliu/crontab                                                                                                       
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

0 0 * * * /usr/bin/docker run --rm -it --name=powerclicore --entrypoint="/usr/bin/pwsh" -v /home/<redacted>/config/powercli:/scripts vmware/powerclicore /scripts/VMSnapshot.ps1

我做错了吗?第二双眼睛将不胜感激!

【问题讨论】:

  • 我认为在 /usr/local/bin (或您系统的其他适当位置)中创建一个脚本文件并从 cron 调用该脚本会更好(并且更易于维护和更新)。每次更新脚本时都不需要更改 cron。
  • @KazikM 谢谢。我会试试这个。快速的问题:知道如何让这项工作比今晚等待 12 点更快运行吗?
  • 将此部分 0 0 * * * 更改为您想要的其他值。您可以检查各种组合here at Crontab Guru。只需用其他适当的值替换星星。它甚至可以每天运行多次,甚至每分钟运行一次。
  • 嗨@KazikM。感谢您的答复。所以,我其实走了一条不同的路线,确定命令可以简化为/usr/bin/docker powerclicore-vmsnapshot start。我确认当我以当前用户身份运行此命令时,它会成功执行。但是,当我像这样通过crontab -e 将它添加到我的 crontab 时:0 0 * * * /usr/bin/docker powerclicore-vmsnapshot start,它似乎没有运行,因为我的虚拟机没有备份。您是否知道为什么当我手动运行命令而不是将其添加到我的 crontab 时该命令有效?
  • 看起来可能是 docker 特有的问题,但不幸的是我对 docker 不太熟悉。对不起。

标签: docker powershell cron powercli


【解决方案1】:

@KazikM。经过一些故障排除后,我终于能够使用您的建议使其正常工作。我创建了一个 Bash 脚本,该脚本调用了我试图放入 int crontab 的 Docker 命令,然后使用 crontab 调用了 Bash 脚本:0 0 * * * /bin/bash /home/&lt;redacted&gt;/config/powerclicore/VMSnapshot-bash.sh。再次感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2015-04-25
    • 2019-11-24
    • 1970-01-01
    • 2014-04-07
    • 2017-07-17
    • 1970-01-01
    • 2010-12-08
    • 2014-01-02
    相关资源
    最近更新 更多