-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32ae400
commit 5e6fcd1
Showing
1 changed file
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# Understanding the `screen` Command in Linux | ||
|
||
The `screen` command in Linux is a powerful utility that allows users to create and manage multiple terminal sessions from a single window. It’s particularly useful for managing long-running processes, remote work, or simply organizing different tasks without cluttering your workspace. In this blog post, we’ll explore the basics of the `screen` command, how to use it, and provide some practical examples. | ||
|
||
## What is `screen`? | ||
|
||
`screen` is a terminal multiplexer that enables you to start a session and then disconnect from it, leaving the processes running. You can later reattach to the session, allowing for continuous work without worrying about interruptions or losing progress. | ||
|
||
## Installing `screen` | ||
|
||
Most Linux distributions come with `screen` pre-installed. If it's not already on your system, you can install it using your package manager. Here’s how to do it on different distributions: | ||
|
||
- **Debian/Ubuntu:** | ||
```bash | ||
sudo apt update | ||
sudo apt install screen | ||
``` | ||
|
||
- **CentOS/RHEL:** | ||
```bash | ||
sudo yum install screen | ||
``` | ||
|
||
- **Fedora:** | ||
```bash | ||
sudo dnf install screen | ||
``` | ||
|
||
## Basic Usage of `screen` | ||
|
||
### Starting a New Screen Session | ||
|
||
To start a new screen session, simply type: | ||
```bash | ||
screen | ||
``` | ||
|
||
This command creates a new session and presents you with a new terminal prompt. | ||
|
||
### Detaching from a Session | ||
|
||
To detach from the current session and return to your normal terminal, press: | ||
``` | ||
Ctrl + A, then D | ||
``` | ||
This will leave the session running in the background. | ||
|
||
### Listing Screen Sessions | ||
|
||
To view all active screen sessions, use: | ||
```bash | ||
screen -ls | ||
``` | ||
|
||
This will display a list of sessions with their respective IDs. | ||
|
||
### Reattaching to a Session | ||
|
||
To reattach to a detached session, you can use: | ||
```bash | ||
screen -r <session_id> | ||
``` | ||
If you have only one session, you can simply use: | ||
```bash | ||
screen -r | ||
``` | ||
|
||
### Exiting a Screen Session | ||
|
||
To exit a screen session completely, type `exit` in the terminal or press: | ||
``` | ||
Ctrl + D | ||
``` | ||
This will terminate the session, and any processes running in it will stop. | ||
|
||
## Practical Examples | ||
|
||
### Example 1: Running a Long-Running Command | ||
|
||
Suppose you want to run a backup script that takes several hours to complete. Instead of keeping your terminal open, you can use `screen`: | ||
|
||
1. Start a new screen session: | ||
```bash | ||
screen -S backup_session | ||
``` | ||
|
||
2. Run your backup command: | ||
```bash | ||
./backup.sh | ||
``` | ||
|
||
3. Detach from the session: | ||
``` | ||
Ctrl + A, then D | ||
``` | ||
|
||
You can log out of your system, and your backup process will continue to run. Later, you can reattach to the session using: | ||
```bash | ||
screen -r backup_session | ||
``` | ||
|
||
### Example 2: Multiple Sessions | ||
|
||
You can run multiple tasks simultaneously. For instance, if you're monitoring server logs and running a database update: | ||
|
||
1. Start the first session for monitoring: | ||
```bash | ||
screen -S log_monitor | ||
tail -f /var/log/syslog | ||
``` | ||
|
||
2. Detach from that session: | ||
``` | ||
Ctrl + A, then D | ||
``` | ||
|
||
3. Start another session for the database update: | ||
```bash | ||
screen -S db_update | ||
./update_db.sh | ||
``` | ||
|
||
Sure! The `screen -x` command allows multiple users to attach to the same screen session simultaneously. This can be particularly useful for collaborative work or monitoring processes together. Here’s how to use it with examples: | ||
|
||
## Example 3: Using `screen -x` | ||
|
||
### Allowing Other Users to Attach | ||
|
||
To allow another user to attach to your screen session, you may need to set the appropriate permissions. You can do this by changing the terminal permissions or by using a shared user account. | ||
|
||
### Example of Using `screen -x` | ||
|
||
1. **User A starts the session:** | ||
User A runs: | ||
```bash | ||
screen -S shared_session | ||
``` | ||
|
||
2. **User A runs a command:** | ||
User A executes a command like `top`. | ||
|
||
3. **User A detaches the session: (OPTIONAL) ** | ||
User A detaches with: | ||
``` | ||
Ctrl + A, then D | ||
``` | ||
|
||
4. **User B attaches to the same session:** | ||
User B can now attach to the same session using: | ||
```bash | ||
screen -x shared_session | ||
``` | ||
|
||
Both User A and User B will now see the same terminal output and can interact with the session together. This is helpfull when multple person over ssh debugging or running something so both can be on same page. | ||
|
||
### Important Notes | ||
|
||
- **Multiple Attachments:** If a user is already attached to a session, using `screen -x` will allow another user to view the same session, but they will not be able to run commands in it if the session is already active for someone else. | ||
|
||
- **Collaboration:** This setup is great for pair programming or collaborative troubleshooting, as both users can see the same outputs in real-time. | ||
|
||
- **Security:** Be cautious with permissions when allowing multiple users to attach to a session, as this can expose sensitive information. | ||
|
||
## Conclusion | ||
|
||
The `screen` command is an invaluable tool for anyone working in a Linux environment. It allows for effective session management, making it easier to handle long-running processes or multiple tasks simultaneously. With just a few simple commands, you can significantly enhance your productivity and workflow. | ||
|
||
Whether you're a system administrator, developer, or just a Linux enthusiast, mastering `screen` can greatly improve your command-line experience. Happy screening! |