加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 02:58:32 所属栏目:Linux 来源:网络整理
导读:我写了这个小的 Python脚本,每天备份一个包含一些文件的目录(备份应在一周后轮换).就是这个: $cat /etc/cron.daily/file-store-backup.py#!/usr/bin/python3import datetimeimport calendarimport subprocessimport os.pathdef main(): origin = '/var/file
我写了这个小的 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!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读