Running Multiple Commands in the Background on Linux

Executing multiple commands in the background is a powerful feature in Linux that allows users to run tasks simultaneously without blocking the terminal. This capability is essential when working with long-running processes, system administration tasks, or when you need to maintain productivity while commands execute.

Linux provides several methods to run commands in the background, primarily using the & operator and the nohup command. Understanding these tools and their proper usage is crucial for effective system management.

Running Commands in Background Using "&" Operator

The & operator is the simplest way to run commands in the background. When appended to a command, it immediately returns control to the terminal while the command continues executing.

Basic Usage

$ sleep 45 &
[1] 1234

This starts the sleep command in the background. The system returns a job number [1] and process ID 1234.

Managing Background Jobs

Use these commands to control background processes:

$ jobs                    # List all background jobs
[1]+  Running             sleep 60 &

$ fg %1                   # Bring job 1 to foreground
$ bg %1                   # Send job 1 to background
$ kill %1                 # Terminate job 1

Running Multiple Commands

$ command1 & command2 & command3 &
$ (command1; command2; command3) &    # Sequential execution in background
$ command1 & command2 && command3     # Mixed foreground/background

Running Commands in the Background Using "nohup"

The nohup command runs processes immune to hangup signals, allowing them to continue even after terminal closure or user logout. This is essential for long-running tasks on remote servers.

Basic nohup Usage

$ nohup sleep 60 &
nohup: ignoring input and appending output to 'nohup.out'
[1] 1235

By default, output is redirected to nohup.out in the current directory.

Custom Output Redirection

$ nohup ./long-script.sh > output.log 2>&1 &    # Redirect both stdout and stderr
$ nohup command > /dev/null 2>&1 &               # Discard all output
$ nohup command >> logfile.txt &                 # Append to existing log

Advanced Background Process Management

Process Control Commands

Command Description Example
jobs -l List jobs with process IDs jobs -l
ps aux Show all running processes ps aux | grep myprocess
disown Remove job from shell's job table disown %1
screen/tmux Terminal multiplexers for persistent sessions screen -S mysession

Practical Examples

# Run backup script in background
$ nohup ./backup.sh > backup.log 2>&1 &

# Multiple file downloads simultaneously
$ wget file1.zip & wget file2.zip & wget file3.zip &

# Long compilation with logging
$ nohup make -j4 > build.log 2>&1 &

# Database maintenance script
$ nohup ./db-maintenance.sh &
$ disown                    # Prevent termination on shell exit

Key Differences

Method Survives Terminal Close Output Handling Use Case
& operator No Displayed on terminal Interactive sessions, short tasks
nohup Yes Redirected to nohup.out Remote servers, long-running tasks
screen/tmux Yes Full session persistence Complex interactive workflows

Conclusion

Running commands in the background is essential for efficient Linux system management. The & operator provides quick background execution for interactive sessions, while nohup ensures process persistence across terminal sessions. Combined with job control commands, these tools enable effective multitasking and system administration workflows.

Updated on: 2026-03-17T09:01:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements