# How to stop and restart a gateway event script with code

**URL:** <https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529>\
**Category:** Ignition\
**Tags:** ignition79, ignition80\
**Created:** [September 9, 2020, 4:27pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529 "2020-09-09T16:27:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [September 9, 2020, 4:27pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529/1 "2020-09-09T16:27:31Z")

</div>

i have a gateway script that stops in a middle probably due to a jdbc connection failure on a database. There is no log associated with this error and the database is valid. So the only thing is to restart the script which I can do manually, but I would like to do this automatically.

Thanks,

---

<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:** [September 9, 2020, 4:41pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529/2 "2020-09-09T16:41:46Z")

</div>

I don’t believe you can.

Consider sprinkling the use of _[system.util.getLogger()](https://docs.inductiveautomation.com/display/DOC80/system.util.getLogger)_ within your script to help debug what is going on.

---

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [September 9, 2020, 5:08pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529/3 "2020-09-09T17:08:49Z")

</div>

I have set the Scripting.ScriptManager to TRACE.  
Restarted the gateway, and I already know that the jdbc driver will fail due to a bad internet connectivity and this will not help at all. I will still need to restart the gateway script manually, what about automatically?

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [September 9, 2020, 5:22pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529/4 "2020-09-09T17:22:49Z")

</div>

There might be simpler ways, but I’ve handled vaguely similar issues by having the script use memory tags to record it’s current state and/or a timestamp of when it passed certain checkpoints. You can then have another tag change or timed gateway event which looks at those tags and decides if things went wrong and how to respond.

---

<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:** [September 9, 2020, 5:33pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529/5 "2020-09-09T17:33:39Z")

</div>

> [@patrick.garceau](#):
>
> I will still need to restart the gateway script manually

You cannot. You should write your code as a state machine and call it regularly (timer event) to act (or not) upon current conditions. You almost certainly need to catch your own exceptions--the ScriptManager's logging points won't help you debug _your_ code.

---

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [September 9, 2020, 7:38pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529/6 "2020-09-09T19:38:46Z")

</div>

Not sure I understand this.  
What is a state machine?  
If the script manager logging is not the help I need, what is the log I need to find out why the Gateway event script is stopped? I know that it is stopped because the jdbc cannot reach the database because of the internet connection that is not reliable. But it still would be nice to catch the log information.

Thanks,

---

<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:** [September 9, 2020, 8:08pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529/7 "2020-09-09T20:08:57Z")

</div>

> [@patrick.garceau](#):
>
> What is a state machine?

Full generic description here:

> **[Finite-state machine](https://en.wikipedia.org/wiki/Finite-state_machine)**
>
> A finite-state machine (FSM) or finite-state automaton (FSA, plural: automata), finite automaton, or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number of states at any given time. The FSM can change from one state to another in response to some inputs; the change from one state to another is called a transition. An FSM is defined by a list of its states, its initial state, and the inputs that trigger each tra

For our purposes, it is a piece of code that follows a series of steps (states), where the actions it takes and conditions it checks depend on the step (state) it is in. Sequential Function Charts are a popular formal way to do state machines but they can be done in any language. The point is that you write code that looks at a persistent variable (memory tag, perhaps) that says "I'm in step 5" and the code does the next action for step 5, then decides if it achieved step 6, or stays in 5, or goes back to 4. Or whatever.

The code checks its state every time it runs (and it is made to run regularly) and acts accordingly. In your case, you have at least the following steps:

1. Start. Haven't done anything yet. Probably should try to connect.
2. Connecting in progress. Waiting for connection or timeout or error.
3. Connected. Can do operations with connection.
4. Timed out or errored. Log it, wait a bit perhaps, then back to step 1.

Anyways, your code must capture the errors and update the state appropriately. There isn't any logger that will tell you why your code died.

---

<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:** [June 17, 2024, 12:44am UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529/8 "2024-06-17T00:44:35Z")

</div>

> [@pturmel](#):
>
> 1. Connecting in progress. Waiting for connection or timeout or error.

Hi Phil, regarding this step here for example, say you want a timeout/watchdog of 30s to wait for the connection to establish before erroring, how would you do this in a state machine?

The timer script would continuously run, check the state it's in (2), see that the connection is still waiting, **check how long it's been running** , exit if less than timeout, error if \> timeout. But how would you check the time it's been waiting already?  
I can think of some probably silly ways (or maybe not - e.g. storing and using the start system time against current time), but I'd like to know how it _should_ be done

---

<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:** [June 17, 2024, 11:46am UTC](https://forum.inductiveautomation.com/t/how-to-stop-and-restart-a-gateway-event-script-with-code/38529/9 "2024-06-17T11:46:06Z")

</div>

Timestamps. I would usually store the value that represents the **end** of the time period, to minimize the math at each check.
