【问题标题】:crontab not producing the required result [closed]crontab 没有产生所需的结果[关闭]
【发布时间】:2014-01-28 18:34:12
【问题描述】:

]# crontab -l

#ROOT CRONTAB test

#22 15 * * * root my_date=`date +\%Y-\%m-\%0e_\%H:\%M:\%S`; /usr/bin/pg_dumpall -U postgres > /home/apache/tactic_backup/postgresDb_${my_date}
53 13 * * * root /home/apache/tactic_custom_tools/backup.sh

]# 更多 /home/apache/tactic_custom_tools/backup.sh

#!/bin/bash                                                               
/usr/bin/pg_dumpall -U postgres > /home/apache/tactic_backup/postgresDb/`date +\%Y-\%m-\%0e_\%H:\%M:\%S

它不会在 /home/apache/tactic_backup/postgresDb/ 下创建任何东西

但是当我以 root 身份运行脚本或命令时,它可以正常工作并执行所需的操作。但是通过 crontab 它不会 上面的 crontab 是针对 root 的。即使在日志中它也显示它运行(或如此)

Jan 28 13:40:01 bjweb01 crond[13935]: (root) RELOAD (/var/spool/cron/root)
Jan 28 13:40:01 bjweb01 CROND[34555]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan 28 13:40:01 bjweb01 CROND[34556]: (root) CMD (root /home/apache/tactic_custom_tools/backup.sh)
Jan 28 13:46:47 bjweb01 crontab[34743]: (root) LIST (root)
Jan 28 13:49:53 bjweb01 crontab[34814]: (root) BEGIN EDIT (root)
Jan 28 13:50:01 bjweb01 CROND[34818]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jan 28 13:52:11 bjweb01 crontab[34814]: (root) REPLACE (root)
Jan 28 13:52:11 bjweb01 crontab[34814]: (root) END EDIT (root)
Jan 28 13:53:01 bjweb01 crond[13935]: (root) RELOAD (/var/spool/cron/root)
Jan 28 13:53:01 bjweb01 CROND[34887]: (root) CMD (root /home/apache/tactic_custom_tools/backup.sh)

知道我错过了什么吗?

【问题讨论】:

  • pg_dumpall 是否可能需要 postgres 参数提供完整路径?
  • pg_dumpall: 无法连接到数据库“template1”:致命:角色“root”不存在你得到这个
  • 当您尝试使用完整路径或尝试使用/usr/bin/pg_dumpall -U postgres 时,您会得到这个?我会尝试在脚本中添加touch /tmp/testfile 以查看脚本是否正在执行。这样您就可以准确地看到问题出在哪里。
  • 它就像我的脚本,它的 /usr/bin/pg_dumpall -U postgres > /home/apache/tactic_backup/postgresDb/`date +\%Y-\%m-\%0e_\% H:\%M:\%S 脚本在以 root 身份在 shell 上手动运行时工作并创建文件,但不是通过 crontab
  • 奇怪。您是否尝试过提供二进制文件的路径来执行脚本? * * * * * /bin/bash /path/to/script.sh

标签: linux centos crontab


【解决方案1】:

我的文档指出,在时间限制之后提及用户(在这种情况下为 root)仅在存储在 /etc/crontab 的系统 crontab 中有效。

您正在向我们展示用户的 crontabs(显示为 crontab -l),它们在这方面有所不同;即使用户是root用户,也不能在那里给出用户。

【讨论】:

  • 所以我应该将它添加到 /etc/crontab 或 crontab -u root -e ?我希望这只是备份。我认为 root crontab 更安全。以及实际调用 /etc/crontab,哪个用户?
  • 我不会更改系统 crontab,不,除非您认为您的更改是操作系统的一部分(听起来不像,不)。因此,只需为用户 root 创建一个 crontab(我认为您已经拥有),并在时间规范之后忽略用户的那些错误提及。
  • 谢谢你,成功了。从 crontab 中删除“根”
猜你喜欢
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 2019-06-03
  • 1970-01-01
  • 2018-11-30
相关资源
最近更新 更多