python如何每天在指定時(shí)間段運(yùn)行程序及關(guān)閉程序
python每天在指定時(shí)間段運(yùn)行程序及關(guān)閉程序
場(chǎng)景
程序需要在每天某一時(shí)間段內(nèi)運(yùn)行,然后在某一時(shí)間段內(nèi)停止該程序。
程序:
from datetime import datetime, time import multiprocessing from time import sleep # 程序運(yùn)行時(shí)間在白天8:30 到 15:30 晚上20:30 到 凌晨 2:30 DAY_START = time(8, 30) DAY_END = time(15, 30) NIGHT_START = time(20, 30) NIGHT_END = time(2, 30) def run_child(): while 1: print("正在運(yùn)行子進(jìn)程") def run_parent(): print("啟動(dòng)父進(jìn)程") child_process = None # 是否存在子進(jìn)程 while True: current_time = datetime.now().time() running = False # 子進(jìn)程是否可運(yùn)行 if DAY_START <= current_time <= DAY_END or (current_time >= NIGHT_START) or (current_time <= NIGHT_END): # 判斷時(shí)候在可運(yùn)行時(shí)間內(nèi) running = True # 在時(shí)間段內(nèi)則開啟子進(jìn)程 if running and child_process is None: print("啟動(dòng)子進(jìn)程") child_process = multiprocessing.Process(target=run_child) child_process.start() print("子進(jìn)程啟動(dòng)成功") # 非記錄時(shí)間則退出子進(jìn)程 if not running and child_process is not None: print("關(guān)閉子進(jìn)程") child_process.terminate() child_process.join() child_process = None print("子進(jìn)程關(guān)閉成功") sleep(5) if __name__ == '__main__': run_parent()
python定時(shí)程序(每隔一段時(shí)間執(zhí)行指定函數(shù))
import os import time def print_ts(message): print "[%s] %s"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message) def run(interval, command): print_ts("-"*100) print_ts("Command %s"%command) print_ts("Starting every %s seconds."%interval) print_ts("-"*100) while True: try: # sleep for the remaining seconds of interval time_remaining = interval-time.time()%interval print_ts("Sleeping until %s (%s seconds)..."%((time.ctime(time.time()+time_remaining)), time_remaining)) time.sleep(time_remaining) print_ts("Starting command.") # execute the command status = os.system(command) print_ts("-"*100) print_ts("Command status = %s."%status) except Exception, e: print e if __name__=="__main__": interval = 5 command = r"ls" run(interval, command)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python爬蟲中urllib3與urllib的區(qū)別是什么
Urllib3是一個(gè)功能強(qiáng)大,條理清晰,用于HTTP客戶端的Python庫(kù)。那么Python爬蟲中urllib3與urllib的區(qū)別是什么,本文就詳細(xì)的來介紹一下2021-07-07Python 開發(fā)工具通過 agent 代理使用的方法
這篇文章主要介紹了Python 開發(fā)工具通過 agent 代理使用的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09一篇文章弄懂Python中所有數(shù)組數(shù)據(jù)類型
這篇文章主要給大家介紹了關(guān)于Python中所有數(shù)組數(shù)據(jù)類型的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Anaconda下配置python+opencv+contribx的實(shí)例講解
今天小編就為大家分享一篇Anaconda下配置python+opencv+contribx的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08python實(shí)現(xiàn)冒泡排序算法的兩種方法
本篇文章主要介紹了python實(shí)現(xiàn)冒泡排序的兩種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03PyTorch的自適應(yīng)池化Adaptive Pooling實(shí)例
今天小編就為大家分享一篇PyTorch的自適應(yīng)池化Adaptive Pooling實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01python動(dòng)態(tài)網(wǎng)頁批量爬取
這篇文章主要介紹了python動(dòng)態(tài)網(wǎng)頁批量爬取的方法,主要針對(duì)四六級(jí)成績(jī)批量爬取,感興趣的小伙伴們可以參考一下2016-02-02python去除列表中的空值元素實(shí)戰(zhàn)技巧
這篇文章主要介紹了python實(shí)戰(zhàn)技巧之去除列表中的空值元素,搜集針對(duì)python高效處理數(shù)據(jù)的核心代碼,今天是實(shí)現(xiàn)去除列表中的空值元素,需要的朋友可以參考下2023-02-02