记录定时任务的一个错误:crontab 中使用"%"的问题
2018-02-21 11:37
337 查看
最近工作需要,需要定时执行命令文件,并且把执行的日志重定向输出到以日期命名的文件中,命令如下:
/bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +"%F"`.log 2>&1
单独执行这条命令执行正常
然后把命令添加到Linux的定时任务,每天凌晨02:30执行一次定时任务:crontab -l
30 2 * * * /bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +"%F"`.log 2>&1
第二天检查发现定时任务执行失败,查看日志 /var/log/cron:
Feb 21 02:30:01 iZbp1cs0fu03n6k79ztuipZ CROND[27824]: (root) CMD (/bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +")
从执行日志发现cmd 从中间截断了,命令没有正常执行;
结合输出日志发现错误日志显示了上面的脚本在 `date +" 之后就被截断了,出现了语法错误。
查看manpage,发现这么一句话:
The "sixth" field (the rest of the line) specifies the command to be run.
The entire command portion of the line, up to a new‐line or a "%" character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.
A "%" character in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after thefirst % will be sent to the command as standard input.
之后确定原来是 % 这个符号在crontab里面被翻译成了命令结束,所以当crontab读到%就吧后面的内容截取掉了,导致脚本执行失败。 在%前面加上转移符号"\"即可,如下
30 2 * * * /bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +"\%F"`.log 2>&1
检查日志看到定时任务执行成功:
Feb 22 02:30:01 iZbp1cs0fu03n6k79ztuipZ CROND[31285]: (root) CMD (/bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +"%F"`.log 2>&1)
/bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +"%F"`.log 2>&1
单独执行这条命令执行正常
然后把命令添加到Linux的定时任务,每天凌晨02:30执行一次定时任务:crontab -l
30 2 * * * /bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +"%F"`.log 2>&1
第二天检查发现定时任务执行失败,查看日志 /var/log/cron:
Feb 21 02:30:01 iZbp1cs0fu03n6k79ztuipZ CROND[27824]: (root) CMD (/bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +")
从执行日志发现cmd 从中间截断了,命令没有正常执行;
结合输出日志发现错误日志显示了上面的脚本在 `date +" 之后就被截断了,出现了语法错误。
查看manpage,发现这么一句话:
The "sixth" field (the rest of the line) specifies the command to be run.
The entire command portion of the line, up to a new‐line or a "%" character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile.
A "%" character in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after thefirst % will be sent to the command as standard input.
之后确定原来是 % 这个符号在crontab里面被翻译成了命令结束,所以当crontab读到%就吧后面的内容截取掉了,导致脚本执行失败。 在%前面加上转移符号"\"即可,如下
30 2 * * * /bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +"\%F"`.log 2>&1
检查日志看到定时任务执行成功:
Feb 22 02:30:01 iZbp1cs0fu03n6k79ztuipZ CROND[31285]: (root) CMD (/bin/bash /data/shell/merge.sh &>> /data/shell/merge-`date +"%F"`.log 2>&1)
相关文章推荐
- 今天使用VS2015遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的"
- 今天使用VS2012遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的"
- 查看crontab的日志记录定位定时任务问题
- 今天使用VS2012遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的"
- 今天使用VS2012遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的"
- 使用VS2012遇到一个问题:"链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的"
- [错误记录]关于指针传递获得数据使用错误问题....
- "BOF或EOF中有一个是真,或者当前的记录已被删除,所需的操作要求一个当前的记录"问题解决
- 使用CsV格式的CDR记录碰到的一个怪问题的解决
- 未知错误 ID:-2147467259 "操作必须使用一个可更新的查询"
- 连接Access数据库出现"操作必须使用一个可更新的查询"错误
- Microsoft JET Database Engine 错误 '80004005' 操作必须使用一个可更新的查询。问题解决办法
- 安装使用MySQL时一个典型错误的解决、临时记录下
- Ubuntu 使用crontab定时任务
- UserControl 的一个值得注意的问题 [属性" * "的代码生成失败.错误是:"程序集"*.Version=1.0.0.0,Culture=neutral,..........无标记为序列化"
- 如何解决Mysql "发生系统错误2,找不到指定的文件" 的问题(第一次安装使用)
- "执行SQL语句时出现问题操作必须使用一个可更新的查询"错误的解决方法
- 执行SQL语句时出现问题操作必须使用一个可更新的查询错误的解决方法
- "Microsoft OLE DB Provider for ODBC Drivers (0x80004005) 操作必须使用一个可更新的查询”错误的解决办法
- access 报 "操作必须使用一个可更新的查询" 错误