Keeping a terminal session running with the screen command

While there are other applications, the most common problem this solves for me is when I log into a workstation remotely to start a jupyter notebook, and want to leave it running (to finish a set of runs, or just to leave it up so I can pick up where I left off when I have to work on things throughout the week). The problem is that the process running our jupyter notebook will terminate as soon as our ssh session ends. We can prevent that by using the screen command. Once you've ssh'ed into the workstation you want to use (i.e., we want to run the jupyter notebook on the workstation, not on our own laptop!), the trick is to start a new screen session before launching jupyter notebook:

                    
                        screen -S danjupyter
                        jupyter notebook
                    
                

The first command starts a new screen session, and names that screen session 'danjupyter' (you may need to choose another name if someone has already used that). The second starts a jupyter notebook as usual. Once that is running, you hold Ctrl + a, and while holding those keys, press d. This detaches your screen session, and you get back your original ssh session with your screen session now running in the background. Now you can close your terminal window (which will end your ssh session), or even shut down your laptop, and the screen session will continue running on the workstation in the background. You can google screen commands, but useful ones include

                    
                        screen -r danjupyter
                    
                

to reconnect to your running screen session called 'danjupyter' (can replace with whatever you chose to call it). This is useful to close jupyter notebook after you're all done (Ctrl + c). If you're done with the screen session, you can end it by typing

                    
                        exit
                    
                

This will take you back to wherever you were / whatever machine you were on when you started the screen session. To check the names of all screen sessions currently running in the background you use

                    
                        screen -ls
                    
                

If you want to kill a screen session that's running, you need to know the name of that session, or use the above command to get the number that's in front (e.g., 20351 in 20351.jupyter). Then

                    
                        screen -X -S danjupyter kill
                    
                

where you would replace 'danjupyter' with the name (or number) of the screen session you want to kill. This can be useful in cases where you get mixed up with the commands above, and it won't let you reconnect to a screen session because it says it's already attached (and you want to start over, killing anything that's running in it).