How to Run Commands Simultaneously in Linux

How to Run Commands Simultaneously in Linux

Let’s say you’re editing a configuration file in the Linux “vi” editor, and suddenly need to look up some data in another file? On a regular GUI system, this wouldn’t be a problem. You just open the second file, check when you need, and then switch back to the first program. On a command line, it isn’t that simple. Executing a process via the CLI blocks further input on any other program. Take this sleep command, for example,:

Mutliple Commands Simultaneously

While it’s running for 30 minutes, we can’t do anything else. However, we can still manually manage processes and run command simultaneously in Linux using the “bg” and “fg” commands. Here’s how it works.

Ctrl+c and Ctrl+z

We all know that if a process is misbehaving, ctrl+c just shuts it down. Either a program is running for too long, or it’s leaving a huge output that’s scrolling by faster than you can see it! Pressing Ctrl+c aborts the process immediately with something called a SIGINT signal. This tells the process to wrap up what it’s doing and abort immediately.

In most cases, this works, but keep in mind that it’s up to each process to decide how to handle it. Some processes like vi ask for confirmation before exiting – usually with good reason since aborting without warning can lead to data loss or corruption.

However, instead of killing a process with Ctrl+c, you can suspend it instead. And this is where Ctrl+z is useful. Pressing Ctrl+z temporarily pauses a program and sends it to the background. For example:

Simultaneous Commands in Linux

In the image above, we started to edit the file “test” with the vi text editor. Then we pressed Ctrl+z. This “stops” the process or suspends it. These are called “jobs”, and each job has a number. This number is shown whenever we suspend a process in square brackets. For example, the job number of the suspended vi application is 1.

Now that the process is suspended, we’re back to the command prompt, where we’re free to execute other commands!

Resuming a Suspended Job

Keep in mind that a suspended job like the vi editor above isn’t running anymore. It’s in a temporary freeze waiting to be revived. We can revive it in two ways:

  1. Revive the job and bring it to the foreground (and hog the command line)
  2. Revive the job and send it to the background (and we can continue doing other stuff)

To reactivate the suspended job and bring it to the foreground, we use the command:

fg %n

Where “n” represents the job number. So if you’ve finished looking up your data and want to get back to editing the file in vi, we type:

fg %1

If we want the process to resume operations and remain in he background, we type:

bg %n

Where “n” is again the job number. So if you have a process that needs to work continuously (like say installing Apache), but still need to do other stuff on the command line, you can resume its operations using the “bg” command.

If you forget the job number for your processes, don’t worry. Just use this command:

jobs -l

This will show a list of existing jobs with their status and job number like this:

Running Commands Simultaneously in Linux

So there’s never any danger of losing track of too many jobs.

Running Commands Immediately in the Background

The above scenario shows you how to suspend a process with Ctrl+z, and then resume its operations in the background with “bg %n”. However, we can combine these two commands by simply typing an ampersand symbol (&) after a command. This will immediately send it into the background. For example:
sleep 30m &
Executes the sleep command for 30 minutes, and the process will continue to run for 30 minutes in the background like this:

How do You Run Commands Simultaneously in Linux

Very convenient! As before, you can use the “fg” command to bring the process to the foreground again and then kill it or suspend it as you wish.

Warning: When sending a process to the background like this, make sure that it doesn’t generate any output to the command line or stdout! Make sure you redirect the output to a file or something. Otherwise, it will continue to run in the background and fill the command line with its output and you won’t even be able to kill it with Ctrl+c because it’s not running in the foreground.

So with that caveat, you now know how to run commands simultaneously in Linux and can multitask yours to your heart’s content.


If you are one of our fully-managed customers, you can always ask one of our system administrators via live chat or ticket, to help with  any aspect regarding managing your Linux server, they are available 24/7h.



2 thoughts on “How to Run Commands Simultaneously in Linux”

  1. Besides using ampersands, Ctrl-Z and other job-control commands, you can also just forget about all these and use tmux and/or GNU screen.

  2. “Otherwise, it will continue to run in the background and fill the command line with its output and you won’t even be able to kill it with Ctrl+c because it’s not running in the foreground.”

    Just type fg, followed by ctrl-c to bring the process to the foreground and kill it.


Leave a Comment

To prove you are human please solve the following *