如何设置每小时的Cron作业

分享于 

5分钟阅读

ubuntu

  繁體

问题

我将grive.sh添加到/etc/cron.daily,但是命令没有运行,我还将它复制到/etc/cron.hourly,但还是没有运行。。文件只有三行:


#! /bin/sh


cd /media/james/Seagate Expansion Drive/GD


grive



我用过 /etc/cron.hourly# chmod u+rwx grive.sh

我可以从终端手动运行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:/#



我将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 /media/james/"Seagate Expansion Drive"/GD

$ echo test >/tmp/foobar.tmp 确实创建了带有测试的文件。 # echo test2 >/tmp/foobar.tmp


答案1

根据DEBIAN SPECIFIC部分的man cron


 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 (i.e.


 files ending in .dpkg-dist, .dpkg-orig, and .dpkg-new).



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


答案2

将它添加到/etc/cron.d/test,如下所示(每隔5分钟执行,以便获得测试日志):


# m h dom mon dow user command


*/5 * * * * root [DIR_TO_SCRIPT]/grive.sh >> /var/log/grivesh.log 2>&1



查看/var/log/grivesh.log输出,一旦有输出,就可以每小时配置一次cronjob,比如,每小时5分钟)


# m h dom mon dow user command


5 * * * * root [DIR_TO_SCRIPT]/grive.sh >> /var/log/grivesh.log 2>&1




SET  job  CRON  HOUR  
相关文章