• 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

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

  • 相关阅读:
    vscode 增加git-bash 去除问题中的信息
    在Visual Studio Code的侧栏中隐藏某些文件
    word中两端对齐之后文字之间空格很大
    Word中如何设置公式居中,但在同一行的公式序号靠右
    vscode隐藏不常用文件及文件夹
    Console.log输出数组,值并非期望中的值
    word 查找替换 ctrl + H
    excel + ArcGIS 坐标数据处理
    EPSON 墨仓式打印机 使用
    推动新阶段水利高质量发展
  • 原文地址:https://www.cnblogs.com/wangb2/p/12167671.html
Copyright © 2020-2023  润新知