import sys, os, time import logging import psutil pid_file = 'test.PID' block_file = 'test.BLOCKED' def createPidFile(filename): mypid = os.getpid() print("Current process ID: {0}".format(mypid)) fp = open(pid_file, 'w') fp.write(str(mypid)) fp.close() def checkFileExist(filename): if os.path.isfile(filename): print("File [{0}] is exist." .format(filename)) return True print("File [{0}] does not exist." .format(filename)) return False def readPidFile(filename): fp = open(filename, 'r') data = fp.readline() print('PID: {0}' .format(int(data))) return int(data) def checkRunning(): if checkFileExist(pid_file): pid = readPidFile(pid_file) if checkPidExist(pid): print("Process is already running...") sys.exit(0) else: createPidFile(pid_file) print("PID file created successfully...") else: print("Process is not running.") def checkPidExist(pid): if psutil.pid_exists(pid): print("PID [{0}] is running." .format(pid)) return True print("PID [{0}]does not exist." .format(pid)) checkRunning() skip_day = '17,18,20' run_week = 'Mon,Tue,Wed,Thu,Fri' start_time = '17:30' end_time = '19:20' def checkRunTime(): (week_day, day, hour, min) = time.strftime('%a:%d:%H:%M', time.localtime()).split(':') (hour, min) = (int(hour), int(min)) print("Current Day and Time: {0} {1} - {2}:{3}" .format(day, week_day, hour, min)) if skip_day: skip_days = skip_day.split(',') print('Skip Days: ',skip_days) if day in skip_days: return False run_weeks = run_week.split(',') (start_hr, start_min) = [int(x) for x in start_time.split(':')] (end_hr, end_min) = [int(x) for x in end_time.split(':')] print('Run Days : ',run_weeks) print('Run Time : {0}:{1} - {2}:{3}' .format(start_hr, start_min, end_hr, end_min)) #print(day, run_weeks, hour, start_hr, end_hr, min, start_min, end_min) if (week_day in run_weeks) and (hour >= start_hr and hour <= end_hr): if ((hour == start_hr and min < start_min) or (hour == end_hr and min > end_min)): return False return True return False if not checkRunTime(): print("Runtime does not match for current run...") sys.exit(0) print("Runtime matched for current run...") while True: if checkFileExist(block_file): print('BLOCK file exist.') time.sleep(2) continue print("Inside Loop [{0}]" .format(os.getpid())) time.sleep(5)