一、脚本代码
将文件名作为限定条件,使用datetime模块,获取七天前的年月日,并用split函数将其以‘-’为关键字进行拆分;使用os.system模块,删除指定日期日志。
#!/usr/bin/python # -*- coding: utf-8 -*- import datetime import os d = str((datetime.datetime.now() - datetime.timedelta(days=7)).date()).split("-") str1 = d[0]+d[1]+d[2] #delete yyyymmdd format os.system('rm /home/workspace/log/*%s*' % (str1))
二、crontab添加
1.日常使用
crontab -e 添加crotab定时任务
crontab -l 列出所有的定时任务
格式:
# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
特殊字符:
- * 取值范围内的所有数字
- / 每过多少个数字
- - 从X到Z
- ,散列数字
2.常用参数
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond status //查看状态
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置
3.添加开机自启动
在/etc/rc.d/rc.local这个脚本的末尾加上:
/sbin/service crond start