【问题标题】:Shell command to remove files not working in crontab用于删除在 crontab 中不起作用的文件的 Shell 命令
【发布时间】:2018-05-15 07:10:46
【问题描述】:

我有一个使用crontab 定期删除旧文件的命令。

命令是:

0 * * * * sudo find /path/to/data/ -type f -mmin +600 -name '*.gz' -execdir rm -- '{}' +;

这有助于我从给定文件夹位置删除所有早于 600 分钟(10 小时)的 .gz 文件。它在命令提示符和 crontab 中完美运行。

现在我修改了上面的命令来删除另一组文件。但是它在命令提示符下有效,但在 crontab 中无效。

0 * * * * sudo find /path/to/data/ -type f -mmin +600 -name '*.gz%%' -execdir rm -- '{}' +;

我的文件扩展名为*.gz%%。文件名已附加%%,如果需要可以更改为其他字符。

该命令如果从命令提示符运行则运行良好,但保存在crontab 中时根本不会执行。

似乎 crontab 无法执行该命令。任何调试指针或命令中的问题是什么?

【问题讨论】:

    标签: bash shell ubuntu cron


    【解决方案1】:

    来自crontab(5) 手册页:

       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 a  "%"
       character, will be executed by /bin/sh or by the shell specified in the
       SHELL variable of the cronfile.  A "%" character in the command, unless
       escaped  with a backslash (\), will be changed into newline characters,
       and all data after the first % will be sent to the command as  standard
       input.
    

    【讨论】:

    • 看起来像这样。我不能马上接受答案。一旦允许我就会这样做。
    猜你喜欢
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 2016-03-16
    相关资源
    最近更新 更多