I am using Android Studio "Jsch" package to connect my Android Device to my Raspberry Pi via SSH. The Event "on press" sends a command,which executes a Python file on my Raspberry Pi. This File turns on the light for 2 minutes, via GPIO-Ports. Thereafter the light turns off. But if my device is disconnected from the Jsch Session the light doesn't turn off, because the Raspberry Pi terminates the Pyhton Scrpit. To execute The file with nohup or screen is not an option. I would like that the light immediately turns off, if the Connection is lost.

Thank You for Help!

Edit1: Thanks to Mark Smith. With The Signals it was possible to finally cleanup the GPIO ports without any problems.

  • I don't know anything about your connection but it seems that you have to monitor the connection itself (maybe with an independent script/program?) and when it goes down then switch off the light. – Ingo Mar 15 '20 at 12:20
  • You can run your Python script by systemd service* and add the state of SSH to your code. I mean, if someone connected by port number 22 to the raspberry pi, the light turns on, if not, turns off. You can also set which user or IP address connected to run that if statement. – M. Rostami Mar 16 '20 at 6:40
  • 1
    I don't have time to work up and test the code, but I think you should be able to register a signal handler in your python program to intercept SIGHUP, and switch the lights off in the handler before exiting. Googling SIGHUP and signal handlers might get you started. Good luck. – Mark Smith Mar 16 '20 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.