ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 1.1 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://vipinyadav.com/blog/scripts-run-in-background-using-screen-detached-mode |
| Last Crawled | 2026-03-07 22:11:45 (1 month ago) |
| First Indexed | 2025-07-01 13:16:53 (9 months ago) |
| HTTP Status Code | 200 |
| Meta Title | Running scripts in background with screen command on Ubuntu/Linux | Vipin Yadav |
| Meta Description | How to run long running tasks in background without any interruption. |
| Meta Canonical | null |
| Boilerpipe Text | SSHing and executing long-running scripts but only to see it interrupted due to a network reset?
Imagine you have a script that takes several hours to complete. You want to run it in the background.
Well, we all have had that moment where we wanted to execute a long-running script/job but only to find out that it exited due to network disconnection.
I had one similar business requirement where I was running a script that takes more than 24 hours to complete. After multiple tries, I couldn't find a way to complete the job successfully.
Until I found this in-built
screen
command which helps run a script in the background. If you don't have the screen installed you can run
sudo apt install screen
to install it.
The screen command works by opening a new virtual terminal session. Run the
screen
command in the terminal and press
enter
and you will see a new terminal session with a new screen session. Whatever you do in this session will not be affected by the main terminal session.
Screen command
You can use the following screen commands to interact with the screen session:
Create a new screen session:
screen -S <session-name>
. -S is short for session the name.
To return to the main terminal session press
Ctrl+A
and then
D
.
To return to the virtual screen session use command
screen -r
.
To list the screen sessions run command
screen -list
.
To choose a different screen session you can use pid or session name. Run
screen -r <pid-or-session-name>
.
Screen List
You can also run the command directly in the background.
screen
-d
-m
<
script
comman
d>
After running multiple sessions if you want close a few or all of the sessions you can use the following command:
To delete all the sessions you can use
screen -wipe
or
pkill screen
.
To only delete the current session use
screen -X quit
or just
exit
command.
To kill a specific session from the main terminal use
screen -X -S <pid-or-session-name> kill
.
Screen kill session
Tip: If you just want to run a command in the background without creating a new screen session you can send the current running process in the background by simply pressing
Ctrl+Z
.
If you want to bring the process back to the foreground you can use
fg
command.
To list all the background jobs you can use
jobs
command.
To bring a specific job to the foreground you can use
fg <job-number>
.
Jobs bg command
That's all about running scripts in the background. You can find more information about this in the following links:
http://www.gnu.org/software/screen/manual/screen.html
http://aperiodic.net/screen/quick_reference
You can also tweet me at
@vip_iny
if you have any questions or suggestions. |
| Markdown | [Vipin Yadav](https://vipinyadav.com/)
- [Blog](https://vipinyadav.com/blog)
[GitHub](https://github.com/vipiny35)
Toggle menu
# Running scripts in background with screen command on Ubuntu/Linux
July 4, 2022
•4 min read
*SSHing and executing long-running scripts but only to see it interrupted due to a network reset?*
Imagine you have a script that takes several hours to complete. You want to run it in the background.
Well, we all have had that moment where we wanted to execute a long-running script/job but only to find out that it exited due to network disconnection.
I had one similar business requirement where I was running a script that takes more than 24 hours to complete. After multiple tries, I couldn't find a way to complete the job successfully.
Until I found this in-built `screen` command which helps run a script in the background. If you don't have the screen installed you can run `sudo apt install screen` to install it.
The screen command works by opening a new virtual terminal session. Run the `screen` command in the terminal and press `enter` and you will see a new terminal session with a new screen session. Whatever you do in this session will not be affected by the main terminal session.

Screen command
You can use the following screen commands to interact with the screen session:
- Create a new screen session: `screen -S <session-name>`. -S is short for session the name.
- To return to the main terminal session press `Ctrl+A` and then `D`.
- To return to the virtual screen session use command `screen -r`.
- To list the screen sessions run command `screen -list`.
- To choose a different screen session you can use pid or session name. Run `screen -r <pid-or-session-name>`.

Screen List
You can also run the command directly in the background.
```
screen -d -m <script command>
```
After running multiple sessions if you want close a few or all of the sessions you can use the following command:
- To delete all the sessions you can use `screen -wipe` or `pkill screen`.
- To only delete the current session use `screen -X quit` or just `exit` command.
- To kill a specific session from the main terminal use `screen -X -S <pid-or-session-name> kill`.

Screen kill session
> Tip: If you just want to run a command in the background without creating a new screen session you can send the current running process in the background by simply pressing `Ctrl+Z`.
>
> - If you want to bring the process back to the foreground you can use `fg` command.
> - To list all the background jobs you can use `jobs` command.
> - To bring a specific job to the foreground you can use `fg <job-number>`.

Jobs bg command
That's all about running scripts in the background. You can find more information about this in the following links:
- <http://www.gnu.org/software/screen/manual/screen.html>
- <http://aperiodic.net/screen/quick_reference>
You can also tweet me at [@vip\_iny](https://x.com/vip_iny) if you have any questions or suggestions.
Vipin Yadav © 2017 - 2025
[Privacy Policy](https://vipinyadav.com/privacy-policy)
[GitHub](https://github.com/vipiny35)
[X](https://x.com/vip_iny)
[LinkedIn](https://linkedin.com/in/vipiny35) |
| Readable Markdown | null |
| Shard | 12 (laksa) |
| Root Hash | 12441548201413849012 |
| Unparsed URL | com,vipinyadav!/blog/scripts-run-in-background-using-screen-detached-mode s443 |