1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import datetime
import subprocess
begin = datetime.datetime(2022, 2, 21, 00, 00, 00) end = datetime.datetime(2022, 3, 7, 00, 00, 00)
d = begin delta = datetime.timedelta(seconds=600) time_list = [] while d <= end: interval_time = d.strftime("%Y-%m-%d %H:%M:%S") time_list.append(interval_time) d += delta
time_list2 = time_list.copy() time_list3 = [] for i in range(len(time_list2)): if i < len(time_list2) - 1: time_list3.append((time_list2[i], time_list2[i + 1]))
base_cmd = ''' influx -username 'admin' -database location -password 'admin' -execute \ "SELECT * INTO llocation FROM location_bak \ where time>='%s' \ AND time<='%s' \ GROUP BY * TZ('Asia/Shanghai') && docker restart influxdb && sleep 60" '''
for j in time_list3: command = base_cmd % (j[0], j[1])
res = subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
print('stdout', res.stdout.read().decode('utf8')) print('stderr', res.stderr.read().decode('utf8')) with open(r'command.sh', 'a', encoding='utf8') as f: f.write(command)
|