python脚本执行shell,通过crontab执行python脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
import os import time import datetime import subprocess
today =datetime.date.today() deltadays = datetime.timedelta(days=1) yesterday = today - deltadays
month = yesterday.strftime('%b') date = yesterday.strftime('%d')
command1 = "ls -hl /log1 | grep '%s %s' | awk '{print i$9}' i='/log1/' | xargs rm " % (month, date) command11 = "ls -hl /log1 | grep '%s %s' | awk '{print i$9}' i='/log1/' | xargs rm " % (month, date) command2 = "ls -hl /log2 | grep '%s %s' | awk '{print i$9}' i='/log2/' | xargs rm " % (month, date) command22 = "ls -hl /log2 | grep '%s %s' | awk '{print i$9}' i='/log2/' | xargs rm " % (month, date)
os.system(command1) os.system(command11) os.system(command2) os.system(command22)
|
注意
1
| ls -hl /log2 | grep '%s %s' | awk '{print i$9}' i='/log2/' | xargs rm
|
这段shell命令最好是通过python执行shell答应看下具体的文件列出来的格式,防止无效
1
| print os.system("ls -hl /log2")
|
这样运行后就能得出结果。然后根据具体情况修改就好了。