I need to handle the event when the shutdown process is started(for example with long press the robot's chest button or when the battery is critically low). The problem is that I didn't find a way to handle the shutdown/poweroff event. Do you have any idea how this can be done in some convenient way?
Asked
Active
Viewed 165 times
1 Answers
2
Unfortunately this won't be possible as when you trigger a shutdown naoqi will exit as well and destroy your service.
If you are coding in c++ you could use a destructor, but there is no proper equivalent for python...
An alternative would be to execute some code when your script exits whatever the reason. For this you can start your script as a service and wait for "the end" using qiApplication.run(). This method will simply block until naoqi asks your service to exit.
Note: in case of shutdown, all services are being killed, so you cannot run any command from the robot API (as they are probably not available anymore!)

JLS
- 968
- 5
- 12
-
1"execute some code when your script exits whatever the reason" is a good habits. Another way would be to catch the low battery event and also the press button. and/or to install a shutdown daemon in the linux system. eg as explained there; https://opensource.com/life/16/11/running-commands-shutdown-linux – Alexandre Mazel Jul 30 '18 at 08:38
-
@AlexandreMazel, this article is very useful but unfortunately the file system is read-only for `/usr/lib/systemd/system-shutdown` and I have not root permissions to run `chmod`. – dim Aug 01 '18 at 11:35
-
1@JLS, It is actually possible to handle "shutdown" or exit from the program with Python `signal`. I found an example [here](https://stackoverflow.com/a/31464349/2595579). It works - I already tested it. It turned out that Python is a much more powerful language than I thought. – dim Aug 17 '18 at 19:47
-
Nice! Then I guess this would work as well :) thanks @dim the script will be stopped by the os anyway so it will probably send a signal – JLS Aug 20 '18 at 10:01