间歇性 crontab 问题

间歇性 crontab 问题

我的 crontab 条目偶尔无法在用户主目录中找到脚本。crontab 条目完全按照指定的时间表运行,有时脚本也能正确运行,但大多数情况下它只会产生如下输出:

/bin/sh: 1: /home/<user>/script/check_cgi.pl: not found

其 crontab 条目为:

5,35 * * * *    /home/<user>/scripts/check_cgi.pl >>/tmp/check_cgi.pl.out 2>&1

我已仔细检查该脚本是否存在,并且 root 是否可以看到文件系统,以及如果我使用 sudo 切换到 root,命令是否可以正确运行。

有人能解释一下为什么 cron 经常找不到文件以及如何解决它吗?

编辑

这是 root 的 crontab(由 sudo crontab -e 编辑),因此检查 root 是否可以看到该脚本是相关的。

脚本的权限为:

-rwxrwxr-x 1 <user> <user>  700 Apr 20 09:27 check_cgi.pl

并且目录权限也是 775 或 755,因此 root 拥有执行脚本的权限没有问题。

在我添加了一个新的 cron 作业来运行“find /”之后,cron 似乎开始“查找”该脚本;就好像强制 cron 遍历整个目录树允许它再次“查找”该脚本一样。

有 4 个脚本,它们都包含在同一目录中。它们在不同的时间运行(每 15 分钟、每 30 分钟、每天一次),并且它们都同时出现此问题。当问题消失时,它们都正常工作。

编辑2

我找到了问题所在……通过每小时运行一次 cron 作业“ll -lR /home”,我发现大约在 15.00 时,它开始错误地报告 /home/ 目录的内容。它显示的不是正确的目录集合(包括所需的脚本目录),而是完全不同的内容:

lrwxrwxrwx 1 <user> <user> 56 Jan 18 20:53 Access-Your-Private-Data.desktop -> /usr/share/ecryptfs-utils/ecryptfs-mount-private.desktop
lrwxrwxrwx 1 <user> <user> 52 Jan 18 20:53 README.txt -> /usr/share/ecryptfs-utils/ecryptfs-mount-private.txt

当我查看 /usr/share/ecryptfs-utils/ecryptfs-mount-private.txt 文件时,它告诉我:

THIS DIRECTORY HAS BEEN UNMOUNTED TO PROTECT YOUR DATA.
From the graphical desktop, click on:
 "Access Your Private Data"
or
From the command line, run:
 ecryptfs-mount-private

所以这似乎就是答案。主目录设置为加密文件系统,操作系统在使用率低时会卸载它“以保护您的数据”。

我只需要弄清楚如何永久禁用此行为或在 cron 作业运行时禁用。我猜答案就在 ecryptfs-mount-private 中...

答案1

ubuntu帮助描述了这个问题https://help.ubuntu.com/community/EncryptedHome

Caveats
There are a few minor caveats that one must keep in mind about these encrypted configurations.
When you are not logged into your system, data in your home directory is not accessible in plain text. This, of course, is by design. This is what keeps an attacker from gaining access to your files. However, this means that:
1.    Your cronjobs may not have access to your Home Directory 

我认为,解决方案是不要加密主目录,或者将文件移动到 root 可以看到的地方(例如 root 的主目录)。

答案2

脚本偶尔运行良好,这很奇怪。

但是,根据我的经验,大多数与 crontab 相关的问题都与权限有关。

crontab 以其自己的权限运行脚本,而不是 root 权限。

因此,对您的脚本和用户文件夹执行“chmod 707”或“777”怎么样?

相关内容