Stop executing the python code using "Python Shell" Node

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..?

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

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

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.

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

1 Like

Thanks, Sir, I will check and inform you.

My python code uses:

    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 :slight_smile:

1 Like

So is your problem solved now?

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