linux – 为什么cron.daily中的这个脚本不会运行?

我写了这个小的 Python脚本,每天备份一个包含一些文件的目录(备份应在一周后轮换).就是这个:
$cat /etc/cron.daily/file-store-backup.py
#!/usr/bin/python3

import datetime
import calendar
import subprocess
import os.path

def main():
    origin = '/var/file-store'
    today_name = calendar.day_name[datetime.date.today().weekday()]
    dest = '/var/file-store-backup/' + today_name

    if os.path.exists(dest):
        subprocess.call(['rm','-rf',dest])

    subprocess.call(['cp','--reflink=always','-a',origin,dest])
    subprocess.call(['touch',dest])

    last = open('/var/file-store-backup/LAST','w')
    print(today_name,file=last)

if __name__ == "__main__":
    main()

当我手动运行它时,它按预期工作,创建一个以当前一周命名的备份目录,但它没有每天运行:我将它留在/etc/cron.daily中3天,之后没有创建备份目录,服务器一直都在.

权限是对的:

$ls -l /etc/cron.daily/file-store-backup.py 
-rwxr-xr-x 1 root root 553 Abr 11 17:19 /etc/cron.daily/file-store-backup.py

系统是Ubuntu Server 12.04.2 LTS,并且自安装以来cron配置未被篡改.

为什么脚本没有运行?

解决方法

发生这种情况是因为您的脚本具有.py扩展名. /etc/cron.daily中的文件由 run-parts(8)命令运行,默认情况下是忽略与各种规则不匹配的程序.您应该能够删除.py扩展名.

run-parts runs all the executable files named within constraints
described below,found in directory directory. Other files and
directories are silently ignored.

If neither the –lsbsysinit option nor the –regex option is given then
the names must consist entirely of ASCII upper- and lower-case letters,
ASCII digits,ASCII underscores,and ASCII minus-hyphens.

例如

touch /etc/cron.daily/test.py
chmod +x /etc/cron.daily/test.py
run-parts --test /etc/cron.daily
/etc/cron.daily/apache2
...

没有test.py的迹象

mv /etc/cron.daily/test.py /etc/cron.daily/test
run-parts --test /etc/cron.daily 
/etc/cron.daily/apache2
...
/etc/cron.daily/test

ta da!

相关文章

文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限...
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Wi...
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Lin...
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,...
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 Open...