I have a python script I want to keep running for most of a day. I have tried the following;

-- Screen. I SSH to the Raspberry Pi,and they type "screen bash", I then start my script and then "control-shift-a b" to detach from screen.

At this point I can logout of SSH, log back in, do a screen -r and reconnect to my "screen session" and the script is still runnning; perfect!

But, I leave my Mac for a while, and the corporate-mandated sleep kicks in, when I return, and reconnect to the raspberry-pi over SSH, a "screen -list" no longer shows any detached session to reconnect to.

I think the raspberry pi must be cleaning up after some period of time.

-- I also tried preventing SSH timeouts on the Pi, with "ServerAliveInterval 120" but again, when the Mac goes to sleep, the SSH session still gets killed, and the detached screen is no longer on the Pi when I reconnect.

Can anyone help me figure out how to keep my script running on the Pi even after my SSH session is timed out/gone?

Thanks, Bruce

EDIT:: Thanks for the responses, I have dabbled with nohup and I think tmux is an alternative to screen, but I don't believe either of these work for my situation.

I think the root problem is that anything I do to keep the script or execution envoronment running is "cleaned up" by some Pi process when my SSH session times out.

Is there anything I can do to keep screen, or tmux or nohup running when my SSH session times out?

  • Not sure what you really want to do but have a look at nohup. – joan Mar 18 '18 at 14:27
  • "when the Mac goes to sleep, the SSH session still gets killed" = you could change the settings on your Mac support.apple.com/en-us/HT202824 (it is not the RPi killing the ssh connection, it is your Mac) – Fabian Mar 18 '18 at 15:33
  • If your processes are killed even when you use tmux and screen, it sounds like you need to figure out how the "corporate-mandated sleep" works first. – jDo Mar 18 '18 at 16:10
  • 1
    the pi wont clean up anything running under screen - regardless if your ssh session terminates within screen or with screen detached - also control-shift-a b" to detach from screen you mean control-a d don't you? – Jaromanda X Mar 19 '18 at 0:38
  • 2
    I can't see how, after you've logged out of the pi, the "corporate mandated sleep" on the Mac could effect the raspberry pi at all - I think you've missed something important in your description of the problem – Jaromanda X Mar 19 '18 at 0:41

I would recommend tmux.

It's a console environment you can attach to once you're SSH-ed into a machine. You can come and go and your output is saved and scripts in terminal are still active in the background. It supports various features like split-windows, colored text, etc. You can go in and change the default config to allow clicking between windows, scrolling and many other features.

Edit: Your client computer should have no impact on the Pi itself once you've closed the SSH session. Ultimately we would need more information to troubleshoot but below are some causes off the top of my head:
- Pi is rebooting, killing all running tasks.
- Your script is crashing. Try adding logging and error handling.
- You have an infinite for loop. If you have a for loop in your script, you will eventually run into a max number of steps is exceeded. Common mistake. Again, fixed by adding error handling.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.