linux - cron 执行的命令是否有特殊限制?

标签 linux unix cron

<分区>

我有一个 crontab 看起来像

0 0 * * * pg_dump DB_NAME > /path/to/dumps/`date +%Y%m%d`.dmp

当我手动运行它时它工作正常,但当 cron 运行它时就不行了。挖掘日志后,我看到了

Dec 12 00:00:01 localhost crond[17638]: (postgres) CMD (pg_dump DB_NAME > /path/to/dumps/`date +)

看起来像是百分号的问题,但是 man 页面甚至根本不包含百分号,所以我认为它们没问题。

最佳答案

你必须用反斜杠转义百分号:

0 0 * * * pg_dump DB_NAME > /path/to/dumps/`date +\%Y\%m\%d`.dmp

来自 man 5 crontab:

The ‘‘sixth’’ field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file. 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. There is no way to split a single command line onto multiple lines, like the shell’s trailing "\".

关于linux - cron 执行的命令是否有特殊限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/363049/

相关文章:

c - 如何在不使用 POSIX 库 <pthread.h> 的情况下在 C 中创建线程

php - Ubuntu for php 的 Cron 作业

c++ - glog 库(谷歌日志)在堆栈跟踪中打印出 `(unknown)`?

linux - IDA 在屏幕内不起作用

unix - 相当于内存检查的时间

php - 调用另一个 PHP 脚本并在另一个脚本完成之前将控制权返回给用户

python - App Engine cron 作业可以使用 https (SSL) 吗?

c - mkdir() sys 中的访问模式调用 linux c

linux - 如何在 Code::Blocks 中定义声纳

regex - 查找命令与 'regex' 匹配不工作