linux下crontab中%的使用

crontab中下面的腳本不能shell

0 1 * * *   (cd /u01/prod; rsync -avz app 192.168.0.192:/u01/prod/) &>/home/applprod/backuplog/rsync_`date +%Y%m%d_%H%M%S`.log

執行的時候,/var/log/messages日誌中顯示:app

Sep 22 22:50:01 ebsapp CROND[13389]: (applprod) CMD ((cd /u01/prod; rsync -avz app 192.168.0.192:/u01/prod/) &>/home/applprod/backuplog/rsync_`date +)

好像是命令被截斷的樣子。
因而我把要執行的命令都封裝到一個腳本中,放到crontab中執行。less

可是稍後查看crontab的manpage時發現,%在crontab中有特殊含義:rest

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 % 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命令被截斷,而是被理解成另外的含義了,解決的方法也有了,就是將%進行轉義,修改以後的腳本變成了:日誌

03 23 * * * (cd /u01/prod; rsync -avz app 192.168.0.192:/u01/prod/) &>/home/applprod/backuplog/rsync_`date +\%Y\%m\%d_\%H\%M\%S`.log
相關文章
相關標籤/搜索