【问题标题】:script in crontab not being runcrontab 中的脚本未运行
【发布时间】:2014-06-25 04:02:23
【问题描述】:

我有一个问题,crontab 根本没有运行我的脚本。我已将脚本简化为一行,但它仍然无法运行:

$ cat /etc/crontab
# /etc/crontab: system-wide crontab                                             
# Unlike any other crontab you don't have to run the `crontab'                  
# command to install the new version when you edit this file                    
# and files in /etc/cron.d. These files also have username fields,              
# that none of the other crontabs do.                                           

SHELL=/bin/sh                                                                   
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin               

# m h dom mon dow user  command                                                 
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly             
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
*/1 * * * * root /usr/share/test/script.sh                         
# don't forget the newline at the end (https://askubuntu.com/a/23337/12057):     


$ cat /usr/share/test/script.sh
#!/bin/bash                                                                     
echo "got here" > /tmp/test.txt

$ ls -l /usr/share/test/script.sh
-rwxr-xr-x 1 root root 951 May  8 08:59 /usr/share/test/script.sh

$ uname -a
Linux mypcname 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 x86_64 GNU/Linux

$ ps aux | grep cron
root      1111  0.0  0.0  22222  3333 ?        Ss   08:27   0:00 /usr/sbin/cron
me        4444  0.0  0.0   5555   666 pts/0    S+   09:06   0:00 grep --color=auto cron

如您所见,crontab 应该每分钟运行一次script.sh 并写入文件/tmp/test.txt,但是这个文件永远不会出现。我一直在阅读these possible reasons for cron not running,但到目前为止,它们都不适用。我认为一双新的眼睛可能会散发出一些光芒。

【问题讨论】:

  • 系统日志告诉我们什么?通常 cron 作业执行会记录在那里

标签: cron debian


【解决方案1】:

/var/log/syslog给出了答案的线索:

May  8 08:50:01 mypcname /usr/sbin/cron[2222]: (*system*) WRONG FILE OWNER (/etc/crontab)
May  8 08:51:01 mypcname /usr/sbin/cron[2222]: (*system*) WRONG FILE OWNER (/etc/crontab)

$ ls -l /etc/crontab
lrwxrwxrwx 1 root root 24 Oct 12 2013 /etc/crontab -> /home/me/.crontab

$ ls -l ~/.crontab
-rw-r--r-- 1 root root 24 Oct 12 2013 /home/me/.crontab

我记得我在安装 debian 操作系统时这样做了,因为我的主目录中的所有内容都是从 subversion 存储库中检出的。我像这样解决了这个问题:

$ sudo cp ~/.crontab /etc/crontab

现在一切正常:)

【讨论】:

    【解决方案2】:

    每一分钟都是

    * * * * *

    不是

    */1 * * * *

    相关问题:Using crontab to execute script every minute and another every 24 hours

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 2019-11-24
      • 2020-11-13
      • 2020-02-02
      • 1970-01-01
      • 2012-05-23
      • 2015-10-05
      • 2017-12-16
      • 2020-07-10
      相关资源
      最近更新 更多