bash

Running Bash script in background

Running Bash script in background

Running Bash script in the background is useful for executing long-running tasks without keeping the terminal occupied. This guide will show you various methods to achieve this.

Basic Method

To run a script in the background, use the & operator at the end of the command:

./your-script.sh &

With Output Redirection

To prevent the script from outputting to the terminal, redirect its output to a file or to /dev/null:

./your-script.sh > output.log 2>&1 &

Using nohup

If you want the script to keep running even after you log out, use nohup:

nohup ./your-script.sh > output.log 2>&1 &

Checking Background Processes

To check the background processes, use the jobs command:

jobs

Using disown

If you want to disassociate the script from the terminal completely, use disown:

./your-script.sh &
disown

Using screen or tmux

For more advanced background running and session management, consider using screen or tmux.

With screen:

  1. Start a new screen session:
    screen
  2. Run your script:
    ./your-script.sh
  3. Detach from the screen session by pressing Ctrl-A followed by D.

Reattach to the session later with:

screen -r

With tmux:

  1. Start a new tmux session:
    tmux
  2. Run your script:
    ./your-script.sh
  3. Detach from the tmux session by pressing Ctrl-B followed by D.

Reattach to the session later with:

tmux attach
moin

Recent Posts

Bash Script Basics Tutorial: #1

Bash Script Basics Bash scripting is a powerful way to automate tasks in Linux. This…

1 year ago

Screen vs Tmux: Which One to Choose?

Screen vs Tmux Both screen and tmux are popular terminal multiplexers that allow you to…

1 year ago

How to Use nohup

How to Use nohup The nohup command allows you to run a script or command…

1 year ago

How do I make Git ignore file mode (chmod)?

How do I make Git ignore file mode (chmod)? Git is a powerful version control…

1 year ago

Setup Tailwind CSS with Angular 11

Install tailwind css with npm Run the following command to install tailwind css npm install…

3 years ago

How to setup laravel project using homestead

Step 1 : Create a new directory Homestead by using this command  cd ~/Homestead Step…

3 years ago

This website uses cookies.