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

前端之家收集整理的这篇文章主要介绍了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配置未被篡改.

为什么脚本没有运行?

@H_404_14@

解决方法

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

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

If neither the –lsbsysinit option nor the –regex option is given then@H_404_20@ the names must consist entirely of ASCII upper- and lower-case letters,@H_404_20@ 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!

@H_404_14@ @H_404_14@

猜你在找的Linux相关文章