The screen command in Unix-like systems allows you to run long tasks in the background without being tied to an active terminal session. This setup is particularly useful for long-running scripts or tasks on remote servers, where keeping an active connection isn’t always feasible. Here’s how to use it.
Replace session_name with a name for your session to make it easier to identify.
Run the long-running task as you normally would. For example:
Press , then to detach from the session and leave the task running in the background.
To see a list of all active screen sessions, use:
To reattach to a session, use (if you didn’t name your session, you can use the session ID listed from screen -ls):
To terminate a session, reattach to it and then exit the shell by typing exit or pressing .
You can start a command directly in a new screen session without manually entering it:
screen -S session_name -d -m your_command
If you need to scroll through the output, press then to enter scroll-back mode. Use arrow keys to navigate, and press q to exit scroll-back mode.
Press then to enter copy mode. Use the arrow keys to move the cursor to the beginning of the text you want to copy, press , move to the end of the text, and press again.