【问题标题】:Script does not run under cron but runs manually again脚本在 cron 下不运行但再次手动运行
【发布时间】:2014-01-31 15:59:27
【问题描述】:

很抱歉再次问这个问题,但我已经尝试了所有建议。我在/var/TPbackup_script/ 中有 2 个脚本。这是第一个:

mysqldump -u root -pPASSWORD teampass > /var/TPbackups/TPbackup_$(date +"%Y-%m-%d").sql

/etc/crontab中对应的cronjob

20 9    * * *   root    sudo sh /var/TPbackup_script/TPbackup_script

此脚本在 crontab 中工作。一切都很好。第二个脚本没有运行:

s3cmd sync /var/TPbackups s3://PwdMgmt

/etc/crontab中对应的cronjob:

25 9   * * *   root    sudo sh /var/TPbackup_script/TPsyncS3_script

这个失败了。如果我在终端中手动运行它:

sudo sh /var/TPbackup_script/TPsyncS3_script

然后它完美地工作。我尝试了什么:

1) 尝试将 shebang #!/bin/sh 添加到脚本的开头
2) 将脚本重命名为 TPsyncS3_script.sh
3) 我已将脚本添加到 cron.daily 中,它位于每日 cron 任务列表中(我通过命令 run-parts --test /etc/cron.daily 看到它)
没有成功。
这是我的 /etc/crontab 文件:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
16 9    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
20 9    * * *   root    sudo sh /var/TPbackup_script/TPbackup_script
25 9    * * *   root    sudo sh /var/TPbackup_script/TPsyncS3_script.sh > /var/TPbackup_script/sync_log.txt
#

脚本的所有权限均使用sudo chmod 777 设置。
顺便说一下。 sync_log.txt 是在 cronjob 之后创建的,但它是空的。
任何帮助表示赞赏

【问题讨论】:

  • 您检查过您的日志文件吗? fgrep cron /var/log | fgrep TPsyncS3 说什么?此外,您实际上并不需要 sudo 部分,因为您已经将 cronjobs 设置为以 root 运行。
  • “s3cmd”在哪里?它在 /bin 还是 /usr/bin 中? Cron 的 PATH 非常受限。它是否依赖于您环境中的变量?
  • 格伦,s3cmd 在 /usr/bin 中。但我不明白为什么它不起作用,因为我的 /etc/crontab 文件中有 /usr/bin 路径(您可以在主消息中看到 PATH)。
  • 还有 lanzz,我没有任何与 s3cmd 或 /var/log 中的 TPsyncS3 连接的日志。当我从你的答案中输入命令时,什么也没发生。
  • 更新:我在脚本中添加了 PATH 参数以及我使用 whereis s3cmd 找到的所有 s3cmd 位置。现在我的脚本看起来像这样:#!/bin/sh PATH=/usr/bin/s3cmd:/usr/bin/X11/s3cmd:/usr/share/s3cmd:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin s3cmd sync /var/TPbackups s3://PwdMgmt 但如果手动运行它仍然可以工作,但在 cron 中不起作用。

标签: linux shell cron


【解决方案1】:

遇到了同样的问题。通过添加选项来指定 s3cfg 的位置解决了这个问题:

--config /root/.s3cfg

例如:

s3cmd sync --config /root/.s3cfg /var/TPbackups s3://PwdMgmt

【讨论】:

    【解决方案2】:

    我遇到了类似的问题。尝试使用 root 的 crontab 运行您的脚本。 做: sudo crontab -e

    添加您的脚本并重试。它对我有用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-27
      • 2014-09-06
      • 1970-01-01
      • 2011-09-15
      • 2019-04-24
      • 2022-09-30
      • 1970-01-01
      • 2010-12-23
      相关资源
      最近更新 更多