[英]python - how to schedule a function to run at last day of each month?
[英]python - how to run a function on last day of every month?
我有一個python函數,我想在每個月的最后一天運行該函數。
我怎樣才能做到這一點?
這要求使用cron作業或類似的調度應用程序。 http://en.wikipedia.org/wiki/Cron
#!/bin/bash
TODAY=`/bin/date +%d`
TOMORROW=`/bin/date +%d -d "1 day"`
# See if tomorrow's day is less than today's
if [ $TOMORROW -lt $TODAY ]; then
exit 0
fi
exit 1
現在我們可以將此行添加到您的crontab中(可以通過crontab -e
訪問
12 0 * * * last-day-of-month.sh && /usr/bin/python /location/of/my/python/script
如果使用的是Windows,則可以將命令行工具schtasks與LASTDAY參數一起使用,以將任務設置為在每月的最后一天執行。 更多信息在這里 。
如果您想要python解決方案,則可以研究celery 。
沒有“ Python功能”可在特定時間運行代碼。 做到這一點的唯一方法是一直運行 ,這是否與時間的函數。 它看起來像這樣:
import calendar
import datetime
import time
def called_last_day():
print("Hark! It's the last day of the month!")
def running_all_the_time():
while True:
today = datetime.date.today()
if today.day == calendar.month_range(today.year, today.month):
called_last_day()
time.sleep(3600) # Wait for an hour
這樣就可以做到,但是有點傻。 請改用cron和schtask。 真。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.