【Linux】Linux計劃任務 crontab中%處理 unexpected EOF

海星星hktk發表於2016-05-14
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章