cron 执行的命令有特殊限制吗? [英] Is there a special restriction on commands executed by cron?

查看:20
本文介绍了cron 执行的命令有特殊限制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 crontab 看起来像

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

当我手动运行它时效果很好,但当 cron 运行它时就不行了.翻阅日志后,我看到

which works fine when I run it manually, but not when cron runs it. After digging through the logs, I see

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

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

It looks like a problem with percent signs, but the man page doesn't even contain the percent character at all, so I thought they were alright.

推荐答案

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

You have to escape percent signs with a backslash:

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

来自man 5 crontab:

''sixth'' 字段(该行的其余部分)指定要执行的命令是跑.该行的整个命令部分,最多一个换行符或%字符,将由/bin/sh 或由指定的 shell 执行这crontab 文件的 SHELL 变量.百分比符号 (%)命令,除非用反斜杠 () 转义,否则将更改为换行符字符,第一个 % 之后的所有数据都将发送到命令作为标准输入.无法拆分单个命令行上多行,就像 shell 的尾随"一样.

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 "".

这篇关于cron 执行的命令有特殊限制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆