python API - detect shutdown
- djdelorie
- Away
- Senior Member
-
Less
More
- Posts: 69
- Thank you received: 10
04 Jul 2026 16:45 #347521
by djdelorie
python API - detect shutdown was created by djdelorie
I've seen a couple of "solutions" to this, but my question here is: what's the canonical way to detect that linuxcnc is shutting down and/or exiting, or isn't running, using the python linuxcnc.stat.poll() api ? I can start my python script in hal using loadusr, but there's no unloadusr, and I can't just run it at computer boot because linuxcnc.stat() just fails if linuxcnc isn't running yet. I tried using the parent PID but that isn't reliable (and prevents you from manually running the script in the background)
I've attached my script. I'd like to cleanly detect that linuxcnc has exited or the computer is shutting down, and send a final MQTT message to home assistant to update the status to "off".
I've attached my script. I'd like to cleanly detect that linuxcnc has exited or the computer is shutting down, and send a final MQTT message to home assistant to update the status to "off".
Please Log in or Create an account to join the conversation.
- rodw
-
- Offline
- Platinum Member
-
Less
More
- Posts: 12020
- Thank you received: 4098
05 Jul 2026 05:13 #347526
by rodw
Replied by rodw on topic python API - detect shutdown
I trying to hook up with a guy in an unfriendly timezone (for an Aussie) who has been working on a implementation of the MTconnect protocol for Linuxcnc that does exactly what you want. The cool thing is that it supports MQTT and there are already integrations to Home Assistant if you have a Fanuc machine. Right now, I don't know any more detail but keep an eye on my @MrRodW YouTube channel as I will cover it there. Once we get going, I'm sure another like minded helper won't go astray.
Please Log in or Create an account to join the conversation.
- djdelorie
- Away
- Senior Member
-
Less
More
- Posts: 69
- Thank you received: 10
05 Jul 2026 22:34 #347544
by djdelorie
Replied by djdelorie on topic python API - detect shutdown
I look forward to testing that, but my tiny script does what I need right now - except exit. I had hoped to find something in the docs about detecting shutdown, but did not. It seems a strange oversight, unless you assume linuxcnc never exits until the machine shuts off...
Please Log in or Create an account to join the conversation.
- rodw
-
- Offline
- Platinum Member
-
Less
More
- Posts: 12020
- Thank you received: 4098
05 Jul 2026 23:31 #347545
by rodw
Replied by rodw on topic python API - detect shutdown
your script might be able to look for linuxcnc's rtapi thread (ie the servo thread) and exit if its not found.
AI seems to think it's possible.
AI seems to think it's possible.
import threading
def get_running_thread_by_name(thread_name):
# threading.enumerate() returns all active/running Thread objects
for thread in threading.enumerate():
if thread.name == thread_name:
return thread # Returns the specific running Thread object
return None
# Example usage:
target_thread = get_running_thread_by_name("MyWorkerThread")
if target_thread:
print(f"Thread '{target_thread.name}' is active.")
else:
print("Thread is not running.")
Please Log in or Create an account to join the conversation.
- MaHa
- Offline
- Platinum Member
-
Less
More
- Posts: 532
- Thank you received: 235
05 Jul 2026 23:59 #347549
by MaHa
Replied by MaHa on topic python API - detect shutdown
I have a component that uses atexit. On my system this works fine to do something when i close linuxcnc. All other attemps failed.
import atexit
def exit():
some code...
atexit.register(exit)
import atexit
def exit():
some code...
atexit.register(exit)
The following user(s) said Thank You: meister, Aciera
Please Log in or Create an account to join the conversation.
Time to create page: 0.154 seconds