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

查看:120
本文介绍了对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


$ b b

man 5 crontab


字段(行的其余部分)指定到
的命令是
运行。该行的整个命令部分,直到
换行符或%
字符,将由/ bin / sh或由
中指定的shell执行
SHELL变量crontab文件。
命令中的百分号(%),
除非用反斜杠(\)转义,将被更改为换行符
个字符,第一个%之后的所有数据将被发送到命令
as
标准输入。没有办法将单个命令行
拆分到
多行,就像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天全站免登陆