我正在使用python schedule库替代我的cron作业。我想一天安排多次工作,从特定时间开始。目前,我可以通过实现以下代码来实现这一目标
import os
import schedule
@with_logging
def run_shell_script(command, extra_params=None):
# dir_path = os.path.dirname(sys.argv[0])
os.system("sh %s" % command)
def run_threaded(command):
job_thread = threading.Thread(target=run_shell_script, args=(command, ))
job_thread.start()
def schedule_jobs():
# Clenup InActive jobs from the system
schedule.every().day.at("03:00").do(run_threaded, script_paths.get('cleanup'))
# Map JobTitles to Burning Glass std. titles
schedule.every().day.at("04:00").do(run_threaded, script_paths.get('map_std_titles'))
schedule.every().day.at("12:00").do(run_threaded, script_paths.get('map_std_titles'))
schedule.every().day.at("20:00").do(run_threaded, script_paths.get('map_std_titles'))
# Copy job documents to JobsModel Collection
schedule.every().day.at("05:30").do(run_threaded, script_paths.get('copy_documents'))
schedule.every().day.at("11:30").do(run_threaded, script_paths.get('copy_documents'))
schedule.every().day.at("17:30").do(run_threaded, script_paths.get('copy_documents'))
schedule.every().day.at("23:30").do(run_threaded, script_paths.get('copy_documents'))
if __name__ == '__main__':
logger = get_basic_logger()
schedule_jobs()
while True:
schedule.run_pending()
time.sleep(1)
我想在5:30、11:30、17:30和23:20的特定时间调用我的copy_documents脚本(在随后的运行中间隔6个小时)。等同的cron命令是
30 23,5,11,17 * * * bash /home/data/my-codebase/copy_document.sh
我想知道是否有更好的方法可以做到这一点。