代码之家  ›  专栏  ›  技术社区  ›  jerrygarciuh

仅当从cron运行命令时出现语法错误

  •  2
  • jerrygarciuh  · 技术社区  · 15 年前

    /usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`date +%Y%m%d`".sql"
    

    从命令行运行很好,但是当cron运行它时

    /bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
    /bin/sh: -c: line 1: syntax error: unexpected end of file
    

    命令也都在crontab的一行上,所以我被第0行和第1行的引用搞糊涂了。。。

    有人能告诉我我哪里做错了什么吗?

    3 回复  |  直到 13 年前
        1
  •  4
  •   Paul Rubel    15 年前

        2
  •  5
  •   Norman Gray    15 年前

    这显然是个愚蠢的问题,但是你的crontab中有匹配的反引号吗( crontab -l )?

    第一行,第零行不是指crontab中的行,只是指单行脚本中的“行”。

    更新时间:

    啊,我想我知道了。这来自crontab(5):

    Percent-signs (%) in the command, unless escaped with backslash (\),
    will be changed into newline characters, and all data after the
    first % will be sent to the command as standard input.
    

    所以转义百分比字符。我忘了克朗塔布的事。。。。

        3
  •  1
  •   Amardeep AC9MF    15 年前

    从cron执行的命令不能从您的登录shell访问环境变量,包括路径。因此,请尝试以下操作(将完全限定路径添加到日期):

    /usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`/usr/bin/date +%Y%m%d`".sql"
    

    date which date