Manage Long-Running Tasks with Screen on a Remote Linux Server

When managing remote Linux servers, you might need to run scripts that take hours, even after you've disconnected, for example a system backup or scan. This is where the screen utility shines, allowing processes to continue running independently of your SSH session. Below is a brief step-by-step guide on using screen to manage long-running tasks efficiently.

Getting Started with screen

Step 1: Connect to the Remote Server

Use SSH to log into your server:

ssh user@remote-server

Step 2: Start a Screen Session

Initiate a new screen session and give it a name for easy identification later:

screen -S my_backup_session

Step 3: Execute Your Script

Run your backup or long-term script within the screen session:

./backup_script.sh

Step 4: Detach the Screen Session

Once your script is running, detach the session without interrupting the process:

Press Ctrl + a, then d.

This keystroke leaves the session intact, running in the background.

Step 5: Disconnect from the Server

You can now safely log out:

exit

Step 6: Reattach to Check Progress

When you wish to check on the script:

  1. SSH back into the server:

bash ssh user@remote-server

  1. List active screen sessions:

bash screen -ls

  1. Reattach to your specific session:

bash screen -r my_backup_session

Step 7: End the Session

When your task completes, exit the screen session by closing the shell:

Press Ctrl + d.

This will terminate the session once your script has concluded.

Conclusion

Utilizing screen not only secures your long-running scripts from unexpected SSH disconnections but also offers the flexibility to monitor progress and manage multiple tasks simultaneously. It's an essential tool for any Linux server administrator seeking reliability and efficiency.


This post was written by Ramiro Gómez (@yaph) and published on . Subscribe to the Geeksta RSS feed to be informed about new posts.

Tags: guide linux sysadmin

Disclosure: External links on this website may contain affiliate IDs, which means that I earn a commission if you make a purchase using these links. This allows me to offer hopefully valuable content for free while keeping this website sustainable. For more information, please see the disclosure section on the about page.


Share post: Facebook LinkedIn Reddit Twitter

Merchandise