【Linux】Linux计划任务 crontab中%处理 unexpected EOF

Linux计划任务 crontab中%处理
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'


使用date +%Y%m%d 形式,方便根据时间生成相应文件名称
但在crontab中使用时,会出现错误,需要对%进行转义

[oracle@rhel64 ~]$ date;date +%Y%m%d-%H%M%S

Sat May 14 19:51:35 CST 2016
20160514-195135
[oracle@rhel64 ~]$
[oracle@rhel64 ~]$ touch `date +%Y%m%d-%H%M%S`.log
[oracle@rhel64 ~]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 May 14 19:51 20160514-195150.log
[oracle@rhel64 ~]$
[oracle@rhel64 ~]$
[oracle@rhel64 ~]$

编辑计划任务,每分钟生成一个以时间命名的文件

[oracle@rhel64 ~]$ crontab -e
[oracle@rhel64 ~]$ crontab -l
* * * * * touch /home/oracle/`date +%Y%m%d-%H%M%S`.log

稍后查看对应目录,未成功生成相应文件
[oracle@rhel64 ~]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 May 14 19:51 20160514-195150.log


[oracle@rhel64 ~]$
You have new mail in /var/spool/mail/oracle
[oracle@rhel64 ~]$

[oracle@rhel64 ~]$ cat /var/spool/mail/oracle
From root@rhel64.oracle.com  Sat May 14 19:57:01 2016
Return-Path:
X-Original-To: oracle
Delivered-To: oracle@rhel64.oracle.com
Received: by rhel64.oracle.com (Postfix, from userid 501)
        id 53F2E4625C; Sat, 14 May 2016 19:57:01 +0800 (CST)
From: root@rhel64.oracle.com (Cron Daemon)
To: oracle@rhel64.oracle.com
Subject: Cron touch /home/oracle/`date +
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
X-Cron-Env:
Message-Id: <20160514115701.53F2E4625C@rhel64.oracle.com>
Date: Sat, 14 May 2016 19:57:01 +0800 (CST)

/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file


[oracle@rhel64 ~]$ 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 cronfile.  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.
。。。。。。


修改crontab   %前加\进行转义
[oracle@rhel64 ~]$ crontab -e
[oracle@rhel64 ~]$ crontab -l
* * * * * touch /home/oracle/`date +\%Y\%m\%d-\%H\%M\%S`.log
[oracle@rhel64 ~]$


修改之后 crontab中命令执行成功
[oracle@rhel64 ~]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 May 14 19:51 20160514-195150.log
-rw-r--r-- 1 oracle oinstall 0 May 14 20:07 20160514-200701.log
-rw-r--r-- 1 oracle oinstall 0 May 14 20:08 20160514-200801.log
-rw-r--r-- 1 oracle oinstall 0 May 14 20:09 20160514-200901.log
-rw-r--r-- 1 oracle oinstall 0 May 14 20:10 20160514-201001.log
-rw-r--r-- 1 oracle oinstall 0 May 14 20:11 20160514-201101.log



吕星昊
2016.5.14

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29475508/viewspace-2099930/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29475508/viewspace-2099930/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值