I have a python application which creates JSON files. I need to execute an application three times per day at midnight, noon and evening.
import json
def add():
a = 10
b = 20
c = a+b
f = open("add.json","w")
f.write(str(c))
def subtract():
a = 10
b = 20
c = b-a
f = open("subtract.json","w")
f.write(str(c))
add()
subtract()
I need to run the application at a specified time automatically
You have 2 possibilities :
1- Let your code run with some timer !
For windows user:
2- use the AT command to create 3 scheduled tasks!
For Linux user:
2- use the Cron command to create 3 scheduled tasks !
which seems to be the best solution in your case!
Related
i have using django project with celery + rabbitmq , some of my task takes like 6 hour or more ,even stack , so i want to re-run the same task if its takes more than 6 hour how to do that ,im new with celery ?
You could try;
from celery.exceptions import SoftTimeLimitExceeded
#celery.task(soft_time_limit=60*60*6) # <--- Set time limit to 6 hours
def mytask():
try:
return do_work()
except SoftTimeLimitExceeded:
mytask.retry() # <--- Retry task after limit of 6 hours exceeded
I wanted to execute a program every once hour. I can use time.sleep function to delay the interval but, if the program executed let's say at 16:30 then the function will only execute at 17:30. But I need to execute the function at 17:00, 18:00 and so on.
import datetime
def onehour(value):
print "%d Hours" % value
now = datetime.datetime.now()
oncehour(now.hour)
You need to sleep first, if current time is 12:23, then you need to sleep 60-23 = 37 minutes and then run your code every hour.
Here you can write like this:
import time
import datetime
time.sleep(60 * (60 - datetime.datetime.now().minute))
while True:
do_smth()
sleep(60 * 60)
What about Cron jobs ? You can write your code inside a Python file then call it from command line inside your crontab:
0 * * * * python script.py
I want to make this script to run automatically once or twice a day at a specified time, what would be the best way to approach this.
def get_data():
"""Reads the currency rates from cinkciarz.pl and prints out, stores the pln/usd
rate in a variable myRate"""
sock = urllib.urlopen("https://cinkciarz.pl/kantor/kursy-walut-cinkciarz-pl/usd")
htmlSource = sock.read()
sock.close()
currancyRate = re.findall(r'<td class="cur_down">(.*?)</td>',str(htmlSource))
for eachTd in currancyRate:
print(eachTd)
print currancyRate[0]
myRate = currancyRate[0]
print myRate
return myRate
You can use crontab to run any script at regular intervals. See https://stackoverflow.com/a/8727991/1517864
To run a script once a day (at 12:00) you will need an entry like this in your crontab
0 12 * * * python /path/to/script.py
You can add a bash function.
while true; do <your_command>; sleep <interval_in_seconds>; done
My ini code for the config is as:
[Config BR54MBPS1MS]
description = "at 54MBPS with SI 1ms for 1250 Bytes with all time interval"
repeat = 2
sim-time-limit = 1 min
**.scalar-recording = true
**.vector-recording = false
**.host1.udpApp[0].messageLength = 1250B
**.wlan*.bitrate = 54Mbps
**.host1.udpApp[*].sendInterval = ${interval = 100..1200 step 100} us
**.vector-recording = false
output-scalar-file = 54Mbps/${configname}54Mbps${interval}us.sca
and I want to run it for all given intervals from 100 us to 1200 us with a gap of 100 us (at 100, 200, 300 ... us) in omnet tkenv or gui. The only option I read for it is by run it through run configuration as:
The problem is that, it runs only for 100us successfully, generates the output sca file and terminates the process. I am not able to figure out the reason for not running the for the next send interval.
In order to run all combinations of sendInterval values you should write * (asterisk) in Run number field and select Command line interface. Multiple runs are not possible when Tcl/Tk user interface is selected.
I am trying to implement cron job in flask. Code structure is like
this:
code:
#sched.cron_schedule(second='*/30')
def some_decorated_task():
time_now = datetime.datetime.now()
print time_now
print "##########0######"
output:
2015-03-01 23:53:30.001843
##########0######
2015-03-01 23:53:30.002615
##########0######
Why it printing two time instead of one ?