微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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!

原文地址:https://www.jb51.cc/linux/397182.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐