记录定时任务的一个错误:crontab 中使用"%"的问题

最近工作需要,需要定时执行命令文件,并且把执行的日志重定向输出到以日期命名的文件中,命令如下:

/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)

原文地址:https://www.cnblogs.com/kofxxf/p/8456405.html

时间: 2024-08-04 18:11:24

记录定时任务的一个错误:crontab 中使用"%"的问题的相关文章

Spring中的一个错误:使用Resources时报错(The annotation @Resources is disallowed for this location)

在学习Spring的过程中遇到一个错误:在使用注解@resources的时候提示:The annotation @Resources is disallowed for this location 后来来在学问Java网友的时候解决了. 原来的代码是这样的: 1 package com.show.biz; 2 3 import javax.annotation.Resources; 4 5 import com.show.biz.UserBiz; 6 import com.show.dao.Us

asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

开发web项目时需要安装IIS,在安装好IIS的Windows7本上发布asp.net网站时,web程序已经映射到了本地IIS上,但运行如下错误提示“处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”” 一,上述错误详情为 二.上述错误分析:  vs2010默认采用的是.NET 4.0框架,4.0框架是独立的CLR,和.NET 2.0的不同,如果想运行.NET 4.0框架的网站,需要用aspnet_r

RDP 协议组件 X.224 在协议流中发现一个错误并且中断了客户端连接

如果你的服务器有如下错误: “RDP 协议组件 X.224 在协议流中发现一个错误并且中断了客户端连接.” 可能的有2种: 1:你试试能否能继续远程登陆,有可能你的远程登陆组件出现问题. 2:有人攻击你,使用暴力破解的方法登陆你的系统,导致系统拒绝服务. 一句话注意防贼!! 解决方法: 设置组策略=====>管理摸板=====>wndows组件====>终端服务!! 新的补充(比较狠的解决方法,呵呵.): 事件类型: 错误 事件来源: TermDD 描述: RDP 的 "DAT

java中可以让实例改变了类Feild(java这么干就是一个错误,你看到了就当做是类在该修改或这个类Feild)

class Person { public String name;//定义实例Feild public static int eyeNum;//定义类Feild } public class PersonTest { public static void main(String[] args) { System.out.println("Person的eyeNum的类Feild值是:" + Person.eyeNum); } } 运行: [[email protected] java

oracle中记录被另一个用户锁住的原因与解决办法

oracle数据中删除数据时提示“记录被另一个用户锁住” 解决方法: 1.查看数据库锁,诊断锁的来源及类型: select object_id,session_id,locked_mode from v$locked_object; 或者用以下命令: select b.owner,b.object_name,l.session_id,l.locked_mode from v$locked_object l, dba_objects b where b.object_id=l.object_id

asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

开发web项目时需要安装IIS,在安装好IIS的Windows7本上发布asp.net网站时,web程序已经映射到了本地IIS上,但运行如下错误提示"处理程序"PageHandlerFactory-Integrated"在其模块列表中有一个错误模块"ManagedPipelineHandler"" 我要发布的的web项目开发工具及所用系统 ①开发工具:vs2010.数据库:sqlserver ②操作系统:windows7 ③IIS:IIS 7.5

C++primer原书中的一个错误(派生类using声明对基类权限的影响)

在C++primer 第4版的 15章 15.2.5中有下面这样一段提示: "注解:派生类可以恢复继承成员的访问级别,但不能使访问级别比基类中原来指定的更严格或者更宽松." 在vs2010中经过验证,这段话是错误的.具体见以下代码: //Base.h #pragma once #include <iostream> using namespace std; class Base { public: Base(void); ~Base(void); size_t size()

EF错误:无法加载关系xxx,因为类型xxx不可用。以下信息对于解决上一个错误可能很有用: 类型xxx中不存在必需属性xxx

工作中遇到下面错误: 指定的架构无效.错误: 无法加载关系“Centria.ABATracker.Domain.FK_ABC_TherapySession_SessionId”,因为类型“Centria.ABATracker.Domain.TherapySession”不可用. 以下信息对于解决上一个错误可能很有用: 类型“Centria.ABATracker.Domain.TherapySession”中不存在必需属性“SessionTargetTARecord”. 无法加载关系“Centri

Jenkins. 安装过程中出现一个错误: No such plugin: cloudbees-folder

安装过程中出现一个错误: No such plugin: cloudbees-folder 安装插件,有时候会报类似的错误:An error occurred during installation: No such plugin: cloudbees-folder 上面的错误显示是,安装插件cloudbees-folder失败,是因为下载的Jenkins.war里没有cloudbees-folder插件 需要在网上下载:http://ftp.icm.edu.pl/packages/jenkin