2

I have the following bash script:

#!/bin/bash
src="/home/user/testingscript"
for dir in $(ls "$src")
do
find ${src}/${dir}/ -type f -mtime +31 delete
done

It runs fine if i execute it from the terminal, but once i put it in the crontab like that:

* * * * * /bin/bash /home/user/script/mailboxclean.sh >> /home/user/mailboxcleanup.log 2>&1

or as:

* * * * * /home/user/script/mailboxclean.sh

it doesnt run.

I see no feedback in the syslog, apart that the script has been executed and in the mailboxcleanup.log where i am redirecting I see the followinh which doesnt really help:

/home/user/mailboxcleanup.log: 1: /home/user/mailboxcleanup.log: /bin/sh:: not found

Any ideas?

Thanks

4

1 回答 1

1

这取决于你把脚本放在哪里。

如果您希望它通过 /etc/crontab 运行:您需要在命令之前的第 6 个参数,指定用户!

 * * * * * theuser /home/user/script/mailboxclean.sh

如果您将它放在某人(root 或用户)的 crontab 中使用crontab -e

 * * * * * /home/user/script/mailboxclean.sh

(即,没有第 6 个参数,因为用户是已知的)

请注意,如果您希望通过将其放在 /etc/cron.*/ 下使其每天/每周/每月运行,则需要删除扩展名(即调用它mailboxclean),否则它将被忽略

最常见的差异原因是脚本在通过 cron 运行时缺少一些环境变量(或放置在 /etc/cron.* 目录下)...检查这一点(您可以echo "=== set: ============ " ; set ; echo "=== env: ============ " ; env在脚本的开头添加 :例如,在调试时。或者只是确保您正确设置了所需的一切


与问题无关,但您应该更改

for dir in $(ls "$src"); do find ${src}/${dir}/ -type f ....

for dir in "${src}"/*/ ; do find "${dir}" -type f ....

(请参阅http://mywiki.wooledge.org/BashPitfalls以获取有关此内容和许多其他内容的信息(当您学习 bash 脚本时,他们网站的其余部分也很棒))

于 2013-11-13T17:08:45.377 回答