如何设置每小时的Cron作业来从`/ etc / cron.hourly`运行`grive`?

我将文件grive.sh添加到/etc/cron.daily但命令似乎没有运行。 我也将其复制到/etc/cron.hourly但我的Google Drive目录没有任何变化。 该文件只有三行:

 #! /bin/sh cd /media/james/Seagate Expansion Drive/GD grive 

权限是只读的,因此我将更新这些权限。

我使用了/etc/cron.hourly# chmod u+rwx grive.sh ,但是当我在Nautilus中检查文件的权限时,它们没有改变 – 我不知道为什么。

我可以从终端手动运行grive ,文件在本地和在线同步。

 root@james-Streacom:/etc/cron.hourly# grep CRON /var/log/syslog Feb 1 09:17:02 james-Streacom CRON[8696]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Feb 1 10:17:01 james-Streacom CRON[10958]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Feb 1 11:17:01 james-Streacom CRON[12897]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Feb 1 12:17:01 james-Streacom CRON[15307]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Feb 1 13:17:01 james-Streacom CRON[17043]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Feb 1 14:17:01 james-Streacom CRON[17354]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Feb 1 15:17:01 james-Streacom CRON[17705]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) root@james-Streacom:/etc/cron.hourly# cd / && run-parts --report /etc/cron.hourly root@james-Streacom:/# bash -c "cd / && run-parts --report /etc/cron.hourly" root@james-Streacom:/# 

请注意,这可能是一个副本。 当放入`/ etc / cron.hourly /`时,cron作业不起作用,但在`crontab -e`中定义时工作 ,我只是完成这些步骤。

我将echo test >/tmp/foobar.tmp添加到脚本的最后一行。

 root@james-Streacom:/etc/cron.hourly# grep 'cron\.hourly' /etc/crontab 17 * * * * root cd / && run-parts --report /etc/cron.hourly 

/temp/foobar.tmp不存在。

我试图实际运行# cd /media/james/Seagate Expansion Drive/GD ,并得到错误-su: cd: too many arguments 。 然后我将脚本中的cd行更改为cd /media/james/"Seagate Expansion Drive"/GD

$ echo test >/tmp/foobar.tmp确实在其中创建带有test的文件。 # echo test2 >/tmp/foobar.tmp用test2覆盖测试。

除了原始脚本中的未加引号的空格(您似乎已更正),问题可能在于脚本的命名。

根据man cronDEBIAN SPECIFIC部分:

  As described above, the files under these directories have to be pass some sanity checks including the following: be executable, be owned by root, not be writable by group or other and, if symlinks, point to files owned by root. Additionally, the file names must conform to the filename requirements of run-parts: they must be entirely made up of letters, digits and can only contain the special signs underscores ('_') and hyphens ('-'). Any file that does not conform to these requirements will not be executed by run-parts. For example, any file containing dots will be ignored. This is done to prevent cron from running any of the files that are left by the Debian package management system when handling files in /etc/cron.d/ as configuration files (ie files ending in .dpkg-dist, .dpkg-orig, and .dpkg-new). 

由于grive.sh的名称中有一个点,因此run-parts将忽略它。

我真的没有得到你的问题,虽然根据我的经验,当你将它们添加到脚本并放入cronjob时,某些命令无法正常工作。

我首先将它添加到/etc/cron.d/test新文件中,如下所示(我将每隔5分钟配置一次以获取一些日志用于测试目的):

 # mh dom mon dow user command */5 * * * * root [DIR_TO_SCRIPT]/grive.sh >> /var/log/grivesh.log 2>&1 

请参阅/var/log/grivesh.log stdout,一旦你进行了分析,就可以像这样每小时配置一次cronjob(例如每小时5分钟):

 # mh dom mon dow user command 5 * * * * root [DIR_TO_SCRIPT]/grive.sh >> /var/log/grivesh.log 2>&1 

/etc/cron.hourly/有必要吗? 你不喜欢在/etc/cron.d/或用户的crontab中使用它吗?

问候,

Interesting Posts