linux计划任务格式转换,【Linux】Linux计划任务 crontab中%处理 unexpected EOF

"在Linux的crontab计划任务中,使用`date+%Y%m%d-%H%M%S`来生成时间戳文件名时遇到错误,由于%在crontab中有特殊含义,需要进行转义才能正确执行。通过在每个%前添加反斜杠``,如`date+%Y%m%d-%H%M%S`,解决了crontab命令执行失败的问题,成功实现了每分钟自动生成带有时间戳的文件。"
摘要由CSDN通过智能技术生成

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: Crontouch /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

。。。。。。

ea830e7c64f211dd7df9d32c4c64cf41.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值