Using "screen" to run jobs in the background - Green-Biome-Institute/AWS GitHub Wiki

When you ssh to the instance you created, you will be doing your analyses in the command line. You might want to start a job and then turn off your computer and come back and check the status of your job later. However, if you start a job on the command line that you logged in in, exiting or turning your laptop off will also stop that job from running. This is also true if you for any reason lost your connection to AWS. In order to avoid this, we use "screen". This command when typed on your command line will direct you to another shell that is running in the background. And in that background screen/shell, you can start your job, exit ssh and come back again when you want to check your job. This will be very handy when you are running jobs that are taking hours to finish, like assemblies. In order to do this, you first need to ssh to your instance. From there type:

$ screen

Now you can start your job. When you see that it is running fine you can exit this background screen with "ctrl+a+d", which will bring you to your previous shell. You can then work on other things or exit your shell. When you ssh again type:

$ screen -r

This will take you back to your background shell and you can check the status of your run. Sometimes you might want to start several background screens and run several jobs simultaneously on one instance. You simply do this by exiting the current screen you are running to come back to your login shell, and typing "screen" again. When you would like to check your jobs, you can type "screen -r". This will bring you the "screens you are running, looks like this:

There are several suitable screens on:

29608.pts-0.ip-172-31-56-22 (07/21/21 19:03:28) (Detached)

29431.pts-0.ip-172-31-56-22 (07/21/21 18:17:37) (Detached)

and you can choose which one you would like to go to by:

$ screen -r 29431.pts-0.ip-172-31-56-22

In order to stop a screen session go to your login shell and type:

$ screen -X -S "sessionname" quit

session name in this case would be 29431.pts-0.ip-172-31-56-22, and the command would look like this:

$ screen -X -S 29431.pts-0.ip-172-31-56-22 quit