Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux Run Commands In The Background (& And Ctrl+Z)

Running commands in the background allows you to continue using your terminal while the command executes. This can be useful when running long processes or managing multiple tasks simultaneously. In this tutorial, we'll cover various methods to run commands in the background on Linux systems.

  • Append & to a command:

To run a command in the background, append an ampersand & to the end of the command. For example, to run the sleep command in the background:

sleep 60 &

When you run a command with &, the shell will return a job ID and a process ID for the background process:

[1] 12345
  • 1 is the job ID.
  • 12345 is the process ID.
  • Use Ctrl+Z and bg:

If you've already started a command and want to move it to the background, you can use the Ctrl+Z key combination followed by the bg command.

Press Ctrl+Z to pause the running command:

^Z
[1]+  Stopped                 sleep 60

Then, use the bg command to resume the paused command in the background:

bg
  • List background jobs:

To list all background jobs in the current shell session, use the jobs command:

jobs

The jobs command will display a list of background jobs with their job IDs, statuses (Running or Stopped), and command names:

[1]+  Running                 sleep 60 &
  • Bring a background job to the foreground:

To bring a background job back to the foreground, use the fg command followed by the job ID:

fg %1

This command will bring the job with the ID 1 to the foreground, resuming its execution in the current terminal.

  • Terminate a background job:

To terminate a background job, use the kill command followed by the job's process ID:

kill 12345

Alternatively, you can use the kill command with the % symbol followed by the job ID:

kill %1

In this tutorial, we've covered how to run commands in the background on Linux systems, including starting, listing, foregrounding, and terminating background jobs. Running commands in the background is a powerful way to manage long-running processes and multitask efficiently in the Linux terminal.

  1. Running commands in the background with & in Unix-like systems: To run a command in the background, append & at the end. Example:

    command &
    
  2. How to resume suspended processes in Linux: To resume a suspended process, use the fg command followed by the job number or %. Example:

    fg %1
    
  3. Running multiple commands concurrently in the background: Execute multiple commands concurrently by separating them with &. Example:

    command1 & command2 &