您好,登錄后才能下訂單哦!
需求:
1、復制 /data/web/s1 并把s1改成對應的開服的區服
2、將s1目錄里面的a.py文件里面的s1字符 全部替換成對應的開服的區服id,并給a.py寫權限
3、將對應sn目錄里面的insert.log do.txt 清空
4、把定時任務中的/5 * python /data/web/s1/a.py >> /data/web/s1/insert.log 復制成對應 的區服 并且追加寫入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'
#獲取開服數字并拷貝一個新目錄出來
def copy_newservice():
global open_num
all_service = set()
alredy_open_service = set()
if not os.path.exists(openservice_num):
print('%s開服數文件不存在,請檢查' % 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腳本區服名字三處
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)
#創建定時任務
def crontab():
if not os.path.exists(crontab_file):
print('%s文件不存在,請檢查' % 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()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。