In Linux, long-running shell scripts or commands can get interrupted if you close the terminal or lose SSH access. To prevent this, the screen
command allows you to run shell scripts in the background in a detachable terminal session.
Unlike nohup
or &
, screen
lets you:
- Reattach to the process later
- View logs and output in real-time
- Run multiple background sessions cleanly
This guide teaches you how to:
- Install and use
screen
- Create, detach, and reattach sessions
- Run scripts persistently even after logout
🧰 Step 1: Install screen
(if not already installed)
sudo apt update
sudo apt install screen
This command ensures that screen
is installed on Debian/Ubuntu systems. For RedHat/CentOS, use yum install screen
.
▶️ Step 2: Create a New Screen Session
screen -S myscript
This starts a new screen session named myscript
. You’ll enter a terminal window inside this session. You can now run your shell script here safely.
🧪 Step 3: Run Your Shell Script
./backup.sh
Assuming backup.sh
is your shell script, this command runs it inside the screen session. You can monitor the output live, just like a normal terminal.
If your script isn’t executable, make it so:
chmod +x backup.sh
This ensures the script can be executed directly using ./backup.sh
.
🔙 Step 4: Detach from the Screen Session
Ctrl + A, then press D
This key combination detaches you from the screen session while the script continues to run in the background.
You’ll return to your normal terminal with a message like:
[detached from 12345.myscript]
🔄 Step 5: Reattach to Your Screen Session Anytime
screen -r myscript
This reattaches to the previously detached session. You can now see the output or status of your running script as if you never left.
🔍 List All Screen Sessions
screen -ls
This shows a list of all currently running screen sessions, including their IDs and names. Useful when managing multiple background tasks.
There are screens on:
12345.myscript (Detached)
❌ Close or Kill a Screen Session
Once your script completes or you want to end the session:
- Reattach using
screen -r myscript
- Press
Ctrl + D
or typeexit
To kill without attaching:
screen -S myscript -X quit
🧠 Use Case Example: Running a Python Script in Background
Let’s say you want to keep a Python data collector running.
screen -S datacollector
python3 collect_data.py
After running, press Ctrl+A
then D
to detach. You can reattach later or check output logs if your script supports it.
📦 Bonus: Log Output to File for Later Viewing
Inside your script or session:
./process.sh > output.log 2>&1
This redirects both standard output and error to output.log
, so you don’t miss anything even if not attached.
⚠️ Common Pitfalls
Issue | Cause | Fix |
---|---|---|
screen: command not found | Screen is not installed | Use sudo apt install screen |
Cannot reattach session | Wrong session name | Use screen -ls to confirm name |
Script doesn’t run | Not executable | Use chmod +x script.sh |
The screen
command is a powerful tool for Linux users who want to run and monitor shell scripts in the background safely—even over SSH or in unstable networks. It’s especially useful for:
- System administrators
- DevOps engineers
- Remote workers on VPS or cloud instances
With this tool, you can always pick up where you left off—no process interruptions, no data loss.
Have you tried using
screen
in your workflows?
Or do you usetmux
ornohup
instead?
Let us know your experience and tips in the comments below! 👇