Long Running Scripts Over SSH with Kinsta

I recently discovered a hidden gem with Kinsta’s SSH environment. They have a built in option for performing a background process using the screen command. Huge thanks for this tip goes to Jeff, one of Kinsta’s support engineers 🙏.

Two types of SSH disconnects with Kinsta

The most common type of disconnect happens due to inactivity. This is easily solved by modifying your local ~/.ssh/config file. With the following your SSH client will talk to the remote server every 4 minutes ensuring your always active.  

Host *
ServerAliveInterval 240

The second type of disconnect with Kinsta happens due to their infrastructure. By default Kinsta’s SSH access something like this ssh sitename@xxx.xxx.xxx.xxx -p xxxxx. The IP address is the same used for DNS purposes which happens to be their load balancer IP not the direct server IP. Switching this out for the IP listed under Kinsta’s “Current External IP Address” will reduce the SSH disconnects with long running scripts. The external IP isn’t guaranteed to always be the same. If you have any trouble connecting make sure your using the current external IP.

Running scripts in the background with screen

Even with the above recommendations, I still recommend putting long running scripts into the background. This means once the script is started you can safely close out of terminal without interrupting the script. Later in the day you can reconnect to SSH and attach to the background session. 

The first thing to do after connecting to Kinsta via SSH is type screen. Now start your big script and that it. You can close the terminal. Later when you reconnect you can type screen -ls to list all background sessions. To attach the background session type screen -r <session-id>. That’s it. Couldn’t be easier to use. 🎉