# Running script weekly at specific time

**URL:** <https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222>\
**Category:** Ignition\
**Created:** [July 15, 2015, 10:36am UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222 "2015-07-15T10:36:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![MGambrell](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@MGambrell](https://forum.inductiveautomation.com/u/MGambrell)\
**Post date:** [July 15, 2015, 10:36am UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/1 "2015-07-15T10:36:02Z")

</div>

I need to run a script at at 2:30 every Friday if certain tags are active, im finding it difficult to work out how to specify the day of the week i want this to run as well as the time, i know that i should run this off a timer script, and i have the contents of the script all worked out its just the date/time im having issues getting over i know it would look something like this,

[code]get current time/date  
if current date = friday  
&&  
time = 14:30:

~~Script~~  
[/code]

I feel like this should be really easy but i cant get my head around it, any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [July 15, 2015, 2:05pm UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/2 "2015-07-15T14:05:08Z")

</div>

[code]from java.util import Calendar

currentTime = Calendar.getInstance()

print currentTime.get(Calendar.DAY\_OF\_WEEK) # 1=Sunday to 7=Saturday  
print currentTime.get(Calendar.HOUR)  
print currentTime.get(Calendar.MINUTE)[/code]

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 15, 2015, 2:06pm UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/3 "2015-07-15T14:06:19Z")

</div>

The script would look something like:`from java.util import Calendar, Date
cal = Calendar.getInstance()
cal.time = Date()
if cal.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY and cal.get(Calendar.HOUR)==14:
  if cal.get(Calendar.MINUTE)==30:
    # your code here`This assumes you configured the timer script to execute every 60000 milliseconds, using a fixed rate (not fixed delay).  
This solution has a slight chance of either double executing or missing an event due to latency between triggering an event and actually starting execution. To address that, create a memory tag of type Date in which you write the last actual execution timestamp and check it like this:`from java.util import Calendar, Date
cal = Calendar.getInstance()
cal.time = Date()
if cal.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY and cal.get(Calendar.HOUR)==14:
  if cal.get(Calendar.MINUTE)>=30:
    previous = system.tag.read("MyWeeklyEventTimestamp").value
    if cal.time.time - previous.time > 86400000:
      system.tag.write("MyWeeklyEventTimestamp", cal.time)
      # your code here`With the latter code, you can play with the execution rate to improve precision or reduce CPU load.  
If you need both high precision and low CPU load, you could use a gateway startup script with java.util.Timer or java.util.concurrent.ScheduledThreadPoolExecutor to precisely schedule a first event and the execution pace. If you do this, make sure you choose daemon mode for the timer’s thread.

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [July 15, 2015, 2:17pm UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/4 "2015-07-15T14:17:44Z")

</div>

PTurmel - Calendar.getInstance() returns the current date and time, so isn’t cal.time = Date() redundant?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [July 15, 2015, 2:27pm UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/5 "2015-07-15T14:27:17Z")

</div>

It’s not documented to do that, so Oracle could change that and claim compatibility. Just looked… it is specified for GregorianCalendar(), so that could be used instead of the generic getInstance().

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [July 15, 2015, 2:37pm UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/6 "2015-07-15T14:37:26Z")

</div>

I see that method summary doesn’t say that it returns the current date/time. Under the class information it says - “Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar’s getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time:” That’s why I was scratching my head 🙂

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 15, 2015, 7:07pm UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/7 "2015-07-15T19:07:59Z")

</div>

Option B: Use an expression tag.

Expression:

```auto
dateFormat(now(),"E H:mm")="Fri 14:30"
```

Then put the script in the tag’s Value Changed area:  
 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/e/e5711b37d245fc0f505a834bdc4038a19af0dda1.png)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [February 7, 2018, 12:47am UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/8 "2018-02-07T00:47:39Z")

</div>

A little late to the party, but this seems like a bit of a hack imo. Literally every other SCADA package I’ve used offers the ability to schedule an event to occur at xx:xx PM on Day(s).

Surely there’s something available within Ignition to do this? The crontab format used by reports and the gateway backups schedule would be perfect.

---

<div class="post-metadata">

**Author:** ![mmckinnon](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@mmckinnon](https://forum.inductiveautomation.com/u/mmckinnon)\
**Post date:** [February 4, 2019, 10:39pm UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/9 "2019-02-04T22:39:10Z")

</div>

Agreed! I hope the come up with a scheduled gateway event script.

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [February 5, 2019, 12:36am UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/10 "2019-02-05T00:36:14Z")

</div>

Well there is a **_completely unsupported_** way if you have the reporting module.  
Create a blank report, schedule it, and set it to run a script.  
Put whatever you want in the script. 🙂

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 5, 2019, 1:44am UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/11 "2019-02-05T01:44:35Z")

</div>

> [@MMaynard](#):
>
> Create a blank report, schedule it, and set it to run a script.

Clever. You win this topic, supported or no. (-:

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [February 5, 2019, 2:39am UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/12 "2019-02-05T02:39:24Z")

</div>

It is full CRON scheduling too.  
Works awesome, we use this to generate PDF reports and send them all in one email instead of separate emails. We generate other reports to a folder in the gateway, then use system send email to send them all together. Works very well.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [February 5, 2019, 9:29pm UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/13 "2019-02-05T21:29:15Z")

</div>

Interesting! This is definitely something to be clearly highlighted in any technical design manual for the system, else no-one will ever find this! But a great work around, cheers 🙂

---

<div class="post-metadata">

**Author:** ![Steve\_Laubach](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steve_laubach/32/60900_2.png) [@Steve\_Laubach](https://forum.inductiveautomation.com/u/Steve_Laubach)\
**Post date:** [January 27, 2021, 5:16pm UTC](https://forum.inductiveautomation.com/t/running-script-weekly-at-specific-time/10222/14 "2021-01-27T17:16:27Z")

</div>

I don’t know why I didn’t think of that. Thanks for the tip.
