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
。。。。。。
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