Django Celery. How do I run task at exact time? - python

How can I write a code that will run a task for example "everyday (or every 24 hours)at 3:20 a.m."?
The main problem is "3:20" part, how do I make cron task at this exact time?

You simply specify the minute and hour that you want it to occur on, and use * for the other values. For example: 20 3 * * * will run at 3:20 AM every day forever.
You can experiment with cron-schedules using this website to get a better understanding for how the syntax works: https://crontab.guru/#20_3_*_*_*

Related

Python scheduled task to run every 14 days

I am currently working on a program that needs to run every 14 days. I have looked into Schedule which works fine, but I have a few doubts about how to go about this.
I will create a service which will handle the execution of the python program itself on a CentOS 7 system.
The issue here is that every 14 days I will run a function that generates a lot of email addresses and send them to a support entity. I am afraid that if something unintended happens, and the program restart - the support entity will get spammed with emails outside the time frame in which they should receive emails.
As far as I can tell, Schedule does not have any way of determining if the program has restarted, and therefore a reboot of either the system or the service will cause this behaviour.
Would it be a correct solution to write a date to a text file after each completed function run, and then check that text file once a day to determine whether the function should run or not? This method would survive a service and/or system reboot, but is it a "correct" way of doing it?
****UPDATE**** Having the cronjob run on specific days of the month (for example 1st and 15th.) is not sufficient. This could cause gaps in the data which the program processes. The script makes a call which pulls data from 14 days back, and this is the maximum number of days supported by the script (licensing and stuff, can't be changed so not that important except that it is a limitation). So it need to run on lets say odd or even week numbers (to get 14 days).
Any ideas on how to accomplish this given this new information?.
You should look into the use of cron (or google it yourself if you dont like the link).
I suggest creating a simple Python script that is called by cron every 14 days. The crontab entry could look like the following:
# this will run at 00:01 on the 15th and 30th of every month
1 0 */15 * * /path/to/python/script.py
# this will run at 00:01 on the 1st and 15th of every month
1 0 1,15 * * /path/to/python/script.py
You still could make your script write some sort of result (with maybe a timestamp) to a file, so that you could easily check that file to see if it ran correctly (or log some error info).
# this will run at 00:01 on the 1st and 15th of every month
1 0 1,15 * * /path/to/python/script.py >> /path/to/logfile.log 2>&1
EDIT
You can also configure cron to run every Monday (or another day) if the 1st and 15th of every month are not sufficient. And the script could check a log file to see if it was run the previous Monday to assure it only executes your business logic every 2 weeks.
# this will run at 00:01 once a week on Mondays
1 0 * * 1 /path/to/python/script.py >> /path/to/logfile.log 2>&1

Python script schedule where to add

I have written a script to read and write in excel.
Now looking to run this script in every 15 minutes periodically.
Is there any one who can modify the code with time scheduler.
Here is the script:
http://prntscr.com/eow5cy
Thanks, Dharmendra
If you're on a *nix based operating system you can use cronjob. On windows you'd want to use a scheduled task.
To setup the cronjob it would be something like:
crontab -e
Edit this file to include the call to your script:
*/15 * * python /home/useraccount/dev/yourpythonscript.py
This will make run every 15 minutes, every hour, every day, every month.
You can use the module time module.
Use function time.sleep (<seconds>)
Check this out.
https://stackoverflow.com/a/510351/1150428

Is it possible to have more precision in cron?

I'm receiving some data through a socket in Python2.7 and treating it as I see fit. This data consists mainly of dates and timestamps, that should be then added to crontab as jobs using python-crontab.
Problem: crontab only goes as precise as minutes. I need at least seconds.
I found this frequent-cron that seems to do the trick, but since it's "under the MIT license" it's of no use to me. Is there another way to have seconds precision?
Just found out that if I add "14 15 * * * sleep 10s; python myScript.py" in crontab -e it will execute at 15:14:10. Crontab schedules the task to 15:14:00, but I delay the execution by 10 seconds, simulating the second precision I wanted. This might just do the trick, though I wish there was a more elegant way.

how to execute a program for every ten mins for the entire day in Python

I am working on web crawler app to download the stock price every ten minutes. I am able to extract the quote but I am not sure how to schedule it to run every ten minutes for the entire day.
Please suggest me either time loop kind of a thing or the solution for the web crawler app itself.
I need a solution which works on Windows.
Ok, so you have a python function f() which you want to execute every ten mins for a day. You can do this:
import time
while True: #Infinite loop
f() #Execute the function
time.sleep(600) #Wait 600s (10 min) before re-entering the cycle
And you keep the script running for as long as you need (a day, a week, whatever, it will not close by itself)
you can do try this
import time
and but this code before dawnload method
time.sleep(10*60) #this will stop the program for 10 minutes
Use crontab. Open the crontab file via terminal:
$ crontab -e
At the end of the file you can set the python script and the frequency of running it. For example to run a script every 10 mins:
*/10 * * * * /path/to/script
Windows has a Task Scheduler built-in. You can use that to run your script:
http://blogs.esri.com/esri/arcgis/2013/07/30/scheduling-a-scrip/
Schedule Python Script - Windows 7
Otherwise, you can write an infinite loop that will check the stock prices every ten minutes using something like time.sleep(600). Note that you can add your program to run at startup fairly easily:
https://mail.python.org/pipermail/tutor/2013-April/094756.html
how to start a python file while window starts?

How to know when cron task was run last time?

I have the following task:
- description: Task is run every 2 hours
url: /task
schedule: every 2 hours synchronized
Is there any way to know in my python code when it was run last time (date and time)?
The idea I have now is to store that in memcache when it is finished to run. But is there any other better way?
PS. Another problem I have is with the case when memcache is empty. How can I calculate when last run was?
You can't, not without storing the information of the last run somewhere. Cron, by itself, does not track that for you.
Why not add an extra bit of code to the file that is ran that creates a log, like a text file?

Categories

Resources