简体   繁体   中英

Python script to send mail at specific time and day every month

I want to send mail every 2nd Friday in month at 4:00 pm How we will do in python. I know following logic is not efficient, is there other way to do this in python ?

// Pseudocode for check 2nd Friday and time 4:00 pm

function(Day='Fri',Time='4:00 p.m.')
while(1){
    String current_date=new date();// This will in following formate.
   // Fri Aug 21 2015 16:00:00 GMT+0530 (IST). 
   // Here we can see that it is combination of Date, Day and time.
   //Now this is the current_date is string so search the sub string 'Fri'
  if(current_date.substring('Fri') && current_date.substring('16:00:00')){
      // Now search for this date is 2nd Friday or not,
      int day=current_date.getDay();
      if(day>7 && day<=13)
         start_script to send mail
  }
}

You can use the python-crontab module.

https://pypi.python.org/pypi/python-crontab

Here's how you can use it:

from crontab import CronTab
#Initialize Cron
cron   = CronTab()

#Add your jobs
job  = cron.new(command='/usr/bin/echo')

#Set period
job.hour.every(4)

Alternatively, you can use crontab -e and follow this link: http://adminschoice.com/crontab-quick-reference

You can use Celery periodic tasks to automate this. Look at here http://celery.readthedocs.org/en/latest/userguide/periodic-tasks.html

Celery will handle cron in your behalf and also provide you with a number of additional tools such as login and interfaces that should make your script scalable (to more users, tasks, etc.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM