Signup/Sign In

Run multiple commands in Background in Linux

In this tutorial, we will discuss about the bg command (part of Linux/Unix shell job control), which comes with the bash package, is used to send a specific process to the background, so that it can’t access the keyboard inputs. Although it writes to standard output, it is ignored by the shell.

bg is a basic Linux command line utility for controlling jobs on Unix-like operating systems. For a shell to adhere to the POSIX standard, bg must be present. After running the bg command, the user is brought back to the shell prompt while the job is running as it resumes suspended jobs (CTRL-Z) in the background.

Using the keyboard shortcut CTRL-Z, you can pause an ongoing command. You return to the shell terminal as soon as the command suspends. The command can be restarted in the background so that it continues to run without interfering with other tasks you are performing in the terminal.

The bg command is used to set each JOB SPEC's associated jobs in the background, as if they had been started with a "&" The shell's concept of the current job is used if JOB SPEC is absent. Exit status of bg command: It returns success unless an error happens or job control is not enabled.

Syntax of bg command Linux

bg [%job_spec …]

Linux terminal bg command

Options in bg command Linux

--help display the usage & help message and exit.
JOB SPEC Specify the job to be moved to the background.

Examples:

1. Move a currently suspended job to the background.

When the bg command is executed without any other arguments. It simply checks for a currently suspended job (or the most recently suspended job) and continues that job in the background.

For example, Let’s run the ping command on a for an alive host with no ICMP count limit. So, it will run forever until we pause or stop it (or if the host is unreachable).

$ ping google.com
PING google.com (74.125.130.102) 56(84) bytes of data.
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=1 ttl=114 time=1.41 ms
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=2 ttl=114 time=0.338 ms
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=3 ttl=114 time=0.384 ms
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=4 ttl=114 time=0.383 ms


Now, use the keyboard key CTRL-Z to suspend the ping command. We can use the ps command to check the currently running process by current use.

$ ping google.com
PING google.com (74.125.130.102) 56(84) bytes of data.
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=1 ttl=114 time=1.41 ms
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=2 ttl=114 time=0.338 ms
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=9 ttl=114 time=0.333 ms
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=10 ttl=114 time=0.387 ms
^Z
[1]+  Stopped                 ping google.com
$ ps
    PID TTY          TIME CMD
    354 pts/2    00:00:00 bash
    530 pts/2    00:00:00 ping
    533 pts/2    00:00:00 ps

Now, If we use the bg command. The currently suspended process or the ping command will resume in the background.

$ bg
[1]+ ping google.com &
$ 64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=11 ttl=114 time=1.17 ms
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=21 ttl=114 time=0.303 ms
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=15 ttl=114 time=0.414 ms
^C
$ 64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=16 ttl=114 time=0.446 ms
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=20 ttl=114 time=0.334 ms
f64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=22 ttl=114 time=0.379 ms
g
ping google.com
64 bytes from sb-in-f102.1e100.net (74.125.130.102): icmp_seq=23 ttl=114 time=0.327 ms
^C

We can see that the process is running in the background as it again starts printing on the terminal.

2. Move a specific suspended job to the background.

If there are more than one suspended jobs at a time. We can specify its process id as an argument to the bg command.

For example: Let’s execute a ping and a vim command and then suspend both of them. (You can verify by using the ps command.)

$ ping google.com
PING google.com (74.125.24.138) 56(84) bytes of data.
64 bytes from sf-in-f138.1e100.net (74.125.24.138): icmp_seq=1 ttl=114 time=1.84 ms
64 bytes from sf-in-f138.1e100.net (74.125.24.138): icmp_seq=2 ttl=114 time=0.414 ms
^Z
[1]+  Stopped                 ping google.com
$ ps
    PID TTY          TIME CMD
    354 pts/2    00:00:00 bash
    576 pts/2    00:00:00 ping
    579 pts/2    00:00:00 ps
$ vim

[2]+  Stopped                 vim
$ ps
    PID TTY          TIME CMD
    354 pts/2    00:00:00 bash
    576 pts/2    00:00:00 ping
    584 pts/2    00:00:00 vim
    587 pts/2    00:00:00 ps

Linux terminal using bg, fg and ps command to manage background tasks

Name the task you want to do in the background. The terms %1, %2, %3, and so on refer to the first job, the second job, the third job, respectively. %, %+, or %% refers to the current job; %- or - refers to the previous job.

Here we can specify the ping command with %1 to resume it in the background.

$ bg "%1"
[1]- ping google.com &
$ 64 bytes from sf-in-f138.1e100.net (74.125.24.138): icmp_seq=3 ttl=114 time=1.63 ms
64 bytes from sf-in-f138.1e100.net (74.125.24.138): icmp_seq=4 ttl=114 time=0.338 ms
64 bytes from sf-in-f138.1e100.net (74.125.24.138): icmp_seq=5 ttl=114 time=0.353 ms


Conclusion

In this article, we learnt that it is possible to restart/resume a process with the help of a basic Linux command line tool bg which is a part of the bash package. Both an internal and an external command may be available for the command. It restores a suspended process's execution as if it had been begun with ‘&’.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.