How to execute commands at the same time in Linux

We once wrote how to run several commands at the same time in Linux here, now we will learn how to execute them at the same time. Let’s say you are editing a configuration file in the Linux “vi” editor, and suddenly you need to find some data in another file? In a normal GUI system, this is not a problem. You just open the second file, check when you need to, and then switch to the first program. It’s not that easy on the command line. Executing a process through the CLI blocks further entry of any other program. Take the sleep command for example:

While it works for 30 minutes, we cannot do anything. However, we can still manually manage processes and run a command at the same time in Linux using the “bg” and “fg” commands. This is how it works.

Ctrl + c and Ctrl + z

We all know that if a process is wrong, ctrl + c will just shut it down. Either if the program runs for too long, or it comes out of a huge output that scrolls faster than you can see it! Pressing Ctrl + C interrupts the process immediately with a SIGINT signal. This indicates that the process completes what it is doing and is immediately terminated.

This works most of the time, but keep in mind that each process must decide how to handle it. Some processes, such as vi, require confirmation before exiting – usually for no good reason, as interrupting without warning can result in data loss or corruption.

However, instead of killing the process with Ctrl + c, you can suspend it. And here it is useful to use Ctrl + z. Pressing Ctrl + z temporarily suspends the program and send it to the back. For instance:

How to execute commands at the same time in Linux

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

Now that the process is paused, we are back at the command line, where we can execute other commands!

Resuming suspended work

Be aware that a suspended job like the vi editor above is no longer running. She is in a temporary frozen state awaiting revival. We can revive it in two ways:

  1. Rebuild the work and bring it to the front (and run the command line)
  2. Restore the work and send it to the background (and we can keep doing other things)

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

fg% n

Where “n” is the job number. So if you’re done looking for your data and want to go back to editing the file in vi, we enter:

fg% 1

If we want the process to resume operations and stay in it, we enter:

bg% n

Where “n” is the job number again. Therefore, if you have a process that needs to run continuously (for example, install Apache), but still need to do other work on the command line, you can resume it with the “bg” command.

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

jobs -l

It will show a list of existing jobs with their status and job number as follows:

How to execute commands at the same time in Linux

Therefore, there is never any danger of losing too many jobs.

Running commands immediately in the background

The above script shows 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 (&) after the command. Examples of ampersand commands can be found here. This will send it to the background immediately. For instance:

sleep 30m &

Runs the sleep command for 30 minutes and the process will continue to run for 30 minutes in the background like this:

How to execute commands at the same time in Linux

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

Warning When submitting a process in the background like this, make sure it doesn’t generate any output on the command line or on standard output! Make sure you redirect the output to a file or something. Otherwise, it will keep running in the background and filling the command line with its output, and you won’t even be able to kill it with Ctrl + c because it doesn’t work in the foreground.

So with this warning, you now know how to run commands in Linux at the same time and can multitask your content.

Please disable your ad blocker or whitelist this site!

Related Posts