您好,登錄后才能下訂單哦!
需求:
1、復(fù)制 /data/web/s1 并把s1改成對(duì)應(yīng)的開服的區(qū)服
2、將s1目錄里面的a.py文件里面的s1字符 全部替換成對(duì)應(yīng)的開服的區(qū)服id,并給a.py寫權(quán)限
3、將對(duì)應(yīng)sn目錄里面的insert.log do.txt 清空
4、把定時(shí)任務(wù)中的/5 * python /data/web/s1/a.py >> /data/web/s1/insert.log 復(fù)制成對(duì)應(yīng) 的區(qū)服 并且追加寫入crond
#!/usr/bin/env python
#coding:utf-8
import os
import re
import shutil as copy
copy_web_service = '/data/web/s1'
dest_web_service = '/data/web/s'
all_web_service = '/data/web/'
crontab_file = '/data/update/crontab.txt'
openservice_num = '/data/update/open_num.txt'
#獲取開服數(shù)字并拷貝一個(gè)新目錄出來
def copy_newservice():
global open_num
all_service = set()
alredy_open_service = set()
if not os.path.exists(openservice_num):
print('%s開服數(shù)文件不存在,請(qǐng)檢查' % openservice_num)
exit(1)
with open('/data/update/open_num.txt','r') as f:
for file in f.readlines():
num = file.strip()
for nums in range(1,int(num)+1):
all_service.add(nums)
for service in os.listdir(all_web_service):
if re.match('s\d+',service):
alredy_open_service.add(int(service[1:]))
cha_num = alredy_open_service^all_service
open_num = list(cha_num)
if not os.path.exists(copy_web_service):
print('%s目錄不存在,腳本退出' % copy_web_service)
exit(1)
else:
for nums in open_num:
dest_dir = dest_web_service + str(nums)
if not os.path.exists(dest_dir):
copy.copytree(copy_web_service,dest_dir)
#修改a.py腳本區(qū)服名字三處
def modify_script():
script = ''
for nums in open_num:
new = 's' + str(nums) + '/'
dest_dir = dest_web_service + str(nums)
if not os.path.exists(dest_dir+'/a.py'):
print('%s腳本不存在,腳本退出' % (dest_dir+'/a.py'))
exit(1)
with open(dest_dir+'/a.py') as f:
for line in f.readlines():
if line.find(str('s1/')):
line = re.sub('s1/',new,line)
script += line
else:
script += line
with open(dest_dir+'/a.py','w') as f:
f.write(script)
script = ''
#清空do.txt和insert.log文件
def clear_file():
kong = ''
file_list = ['/insert.log','/do.txt']
for nums in open_num:
for files in file_list:
dest_dir = dest_web_service + str(nums)
if not os.path.exists(dest_dir+files):
print('%s文件不存在,腳本退出' % (dest_dir+files))
else:
with open(dest_dir+files,'w') as f:
f.write(kong)
#創(chuàng)建定時(shí)任務(wù)
def crontab():
if not os.path.exists(crontab_file):
print('%s文件不存在,請(qǐng)檢查' % crontab_file)
for nums in open_num:
task = '*/5 * * * * python /data/web/s' + str(nums) + '/a.py >> /data/web/s' + str(nums) + '/insert.log\n'
with open(crontab_file,'a') as f:
f.write(task)
print('s\033[31m%s\033[0m服已完成操作...' % nums)
os.system('crontab %s' % crontab_file)
if __name__ == '__main__':
copy_newservice()
modify_script()
clear_file()
crontab()
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。