zoukankan      html  css  js  c++  java
  • crontab里的特殊符号%导致命令不能执行

    有群里的小伙伴说crontab里的任务不执行,具体是这样的

    */1 * * * * /bin/date "+%Y-%m-%d %H:%M:%S" >>/data/tmp/test.log

    我试了下在命令行可以执行,定时任务确实不执行,于是去翻了下日志:

    #tail -f /var/spool/mail/root
    X-Cron-Env: <HOME=/root>
    X-Cron-Env: <PATH=/usr/bin:/bin>
    X-Cron-Env: <LOGNAME=root>
    X-Cron-Env: <USER=root>
    Message-Id: <20200108081701.8FD502100481@D3L40.localdomain>
    Date: Wed,  8 Jan 2020 16:17: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
    
    From root@D3L40.localdomain  Wed Jan  8 16:18:01 2020

    说是语法错误,结尾不对,明明是两个双引号啊,这样就有可能是里面有特殊字符导致命令意外中断了,于是查了下万能的百度发现:

    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 crontab file. 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. There is no way to split a single command line onto multiple lines, like the shell’s trailing “”.

    大概是在crontab里%当换行符用了,想要正常使用就得加反引号,\%这样来用。

    于是把命令改成了:

    */1 * * * * /bin/date "+%Y-%m-%d %H:%M:%S" >>/data/tmp/test.log

    最后:

    # cat test.log
    2020-01-08 16:38:01
    2020-01-08 16:39:01

    可以了,于是想着记一下,好记性不如烂笔头嘛。

  • 相关阅读:
    生成更大的陆地 Making A Large Island
    HDU 3342 Legal or Not【拓扑排序】
    POJ 2367 Genealogical tree【拓扑排序】
    CodeForces Round #290 Div.2
    HDU 1010 Tempter of the Bone【DFS】
    HDU 1312 Red and Black【DFS】
    POJ 1664 放苹果【DFS】
    HDU 1587 Flowers【贪心】
    Codeforces Round #289 Div 2
    HDU 1241 Oil Deposits【DFS】
  • 原文地址:https://www.cnblogs.com/wangb2/p/12167671.html
Copyright © 2011-2022 走看看