# Stop executing the python code using "Python Shell" Node

**URL:** <https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194>\
**Category:** General\
**Created:** [2 November 2020 09:12 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194 "2020-11-02T09:12:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandy](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Sandy](https://discourse.nodered.org/u/Sandy)\
**Post date:** [2 November 2020 09:12 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194/1 "2020-11-02T09:12:50Z")

</div>

Hi All,  
I executed a python script(taken locally) using a python shell node in node-red. But As I am running Infinite Loop code I was unable to stop the execution.  
Please Help me out..?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 November 2020 10:15 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194/2 "2020-11-02T10:15:01Z")

</div>

If you use an exec node instead then you can send it a kill command.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [2 November 2020 13:16 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194/3 "2020-11-02T13:16:29Z")

</div>

I think maybe a better method instead of killing a process is a graceful shutdown. It might be that you have some data you would like to save and eventual other connections you want to terminate before closing down the script. In all my python scripts I have support for MQTT and when I want to shutdown a script, I send a shutdown command via MQTT

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 November 2020 21:30 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194/4 "2020-11-02T21:30:03Z")

</div>

When you send kill to the exec node you can specify the signal you want to use. I can never remember which is which, but I think the default in the exec node is the gracefull shutdown not the sledgehammer.

---

<div class="post-metadata">

**Author:** ![Christophe](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Christophe](https://discourse.nodered.org/u/Christophe)\
**Post date:** [2 November 2020 21:42 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194/5 "2020-11-02T21:42:58Z")

</div>

Hello,  
Kill -2 is equivalent to ctrl-C.  
The sledgehammer is kill -9

---

<div class="post-metadata">

**Author:** ![Sandy](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Sandy](https://discourse.nodered.org/u/Sandy)\
**Post date:** [3 November 2020 05:58 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194/6 "2020-11-03T05:58:58Z")

</div>

Thanks, Sir, I will check and inform you.

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [3 November 2020 17:51 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194/7 "2020-11-03T17:51:49Z")

</div>

My python code uses:

```auto
    def sigint_handler(signal, frame):
        global QUIT
        currentDT = datetime.datetime.now()
        #print('caught SIGINT, normal exit. -- ' + currentDT.strftime("%Y-%m-%d %H:%M:%S"))
        QUIT=True

    def sighup_handler(signal, frame):
        global QUIT
        currentDT = datetime.datetime.now()
        print('caught SIGHUP! **' + currentDT.strftime("%Y-%m-%d %H:%M:%S"))
        QUIT=True

    def sigquit_handler(signal, frame):
        global QUIT
        currentDT = datetime.datetime.now()
        print('caught SIGQUIT! ***' + currentDT.strftime("%Y-%m-%d %H:%M:%S"))
        QUIT=True

    def sigterm_handler(signal, frame):
        global QUIT
        currentDT = datetime.datetime.now()
        print('caught SIGTERM! ****' + currentDT.strftime("%Y-%m-%d %H:%M:%S"))
        QUIT=True

    signal.signal(signal.SIGINT, sigint_handler)
    signal.signal(signal.SIGHUP, sighup_handler)
    signal.signal(signal.SIGQUIT, sigquit_handler)
    signal.signal(signal.SIGTERM, sigterm_handler)

```

My main (infinite) loop is:  
while not QUIT:

I run in an exec node, but only need the "sledgehammer" when something goes wrong that is not inside a try block. As I discover them, new try blocks are added to my code 🙂

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 November 2020 21:30 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194/8 "2020-11-03T21:30:02Z")

</div>

So is your problem solved now?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 January 2021 21:30 UTC](https://discourse.nodered.org/t/stop-executing-the-python-code-using-python-shell-node/35194/9 "2021-01-02T21:30:05Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
