bash 脚本从 shell 运行,但不是从 cron 作业运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/157342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:29:48  来源:igfitidea点击:

bash script runs from shell but not from cron job

linuxbashcron

提问by Tanj

Cron installation is vixie-cron

cron安装是vixie-cron

/etc/cron.daily/rmspam.cron

/etc/cron.daily/rmspam.cron

#!/bin/bash
/usr/bin/rm /home/user/Maildir/.SPAM/cur/*;

I Have this simple bash script that I want to add to a cron job (also includes spam learning commands before) but this part always fails with "File or directory not found" From what I figure is the metachar isn't being interperted correctly when run as a cron job. If I execute the script from the commandline it works fine.

我有这个简单的 bash 脚本,我想将其添加到 cron 作业中(之前还包括垃圾邮件学习命令),但这部分总是以“找不到文件或目录”而失败。据我所知,元字符在以下情况下没有被正确解释作为 cron 作业运行。如果我从命令行执行脚本,它工作正常。

I'd like a why for this not working and of course a working solution :)

我想知道为什么这不起作用,当然还有一个有效的解决方案:)

Thanks

谢谢

edit #1 came back to this question when I got popular question badge for it. I first did this,

当我获得流行问题徽章时,编辑 #1 又回到了这个问题。我第一次这样做,

#!/bin/bash
find  /home/user/Maildir/.SPAM/cur/ -t file | xargs rm

and just recently was reading through the xargs man page and changed it to this

就在最近正在阅读 xargs 手册页并将其更改为此

#!/bin/bash
find  /home/user/Maildir/.SPAM/cur/ -t file | xargs --no-run-if-empty rm

short xargs option is -r

短 xargs 选项是 -r

回答by janm

If there are no files in the directory, then the wildcard will not be expanded and will be passed to the command directly. There is no file called "*", and then the command fails with "File or directory not found." Try this instead:

如果目录中没有文件,那么通配符不会被扩展,直接传递给命令。没有名为“*”的文件,然后命令失败并显示“找不到文件或目录”。试试这个:

if [ -f /home/user/Maildir/.SPAM/cur/* ]; then
    rm /home/user/Maildir/.SPAM/cur/*
fi

Or just use the "-f" flag to rm. The other problem with this command is what happens when there is too much spam for the maximum length of the command line. Something like this is probably better overall:

或者只是使用“-f”标志来 rm。这个命令的另一个问题是当命令行的最大长度有太多垃圾邮件时会发生什么。像这样的事情总体上可能更好:

find /home/user/Maildir/.SPAM/cur -type f -exec rm '{}' +

If you have an old find that only execs rm one file at a time:

如果你有一个旧的发现,一次只执行 rm 一个文件:

find /home/user/Maildir/.SPAM/cur -type f | xargs rm

That handles too many files as well as no files. Thanks to Charles Duffy for pointing out the + option to -exec in find.

这处理太多文件以及没有文件。感谢 Charles Duffy 在 find 中指出 -exec 的 + 选项。

回答by Mark Biek

Are you specifying the full path to the script in the cronjob?

您是否在 cronjob 中指定了脚本的完整路径?

00 3 * * * /home/me/myscript.sh

rather than

而不是

00 3 * * * myscript.sh


On another note, it's /bin/rmon all of the linux boxes I have access to. Have you double-checked that it really is /usr/bin/rmon your machine?

另一方面,它在我可以访问的所有 linux 机器上都是/bin/rm。您是否仔细检查过它确实是您机器上的/usr/bin/rm

回答by Ken

try adding

尝试添加

[email protected]

to the top of your cron file and you should get any input/errors mailed to you.

到您的 cron 文件的顶部,您应该收到邮寄给您的任何输入/错误。

Also consider adding the command as a cronjob

还可以考虑将命令添加为 cronjob

0 30 * * * /usr/bin/rm /home/user/Maildir/.SPAM/cur/*

回答by bcelary

Try using a force option and forget about adding a path to rm command. I think it should not be needed...

尝试使用强制选项并忘记向 rm 命令添加路径。我觉得应该不需要...

rm -f

This will ensure that even if there are no files in the directory, rm command will not fail. If this is a part of a shell script, the * should work. It looks to me that you might have an empty dir...

这将确保即使目录中没有文件,rm 命令也不会失败。如果这是 shell 脚本的一部分,则 * 应该可以工作。在我看来,您可能有一个空目录...

I understand that the rest of the script is being executed, right?

我知道脚本的其余部分正在执行,对吗?

回答by ulidtko

Is rmreally located in /usr/bin/on your system? I have always thought that rmshould reside in /bin/.

难道rm真的坐落在/usr/bin/您的系统上?我一直认为rm应该驻留在/bin/.