If you have a long-term task, it is not always advisable to wait for it to finish. I mean, why keep the terminal busy for a particular command? In Linux, you send a command or process in the background for the command to execute, but the terminal will be free to execute other commands.
Directly Start a Linux process in the background
If you know the Background Linux command or process will take a long time, it would be a better idea to start the command in the background.
To run in background Linux command, all you have to do is add a sign (&) the end of the command, like this:
Let’s a simple bash sleep command and send it to the background.
sleep 30 &
When the command ends in the background, you should see information about it in the terminal.
+ Done sleep 30
Send a running Linux process to the background.
If you have already run a program and then realized that you should have run it in the background, do not worry. You can also send a running process in the background.
What you should do here is to use Ctrl + Z to suspend the running process and then use ‘bg‘ (background abbreviation) to send the process in the background. The suspended process will now run in the background.
running_command ^z bg
Let’s take the same example as before.
sameer@t4tech:~$ sleep 30 ^Z + Stopped sleep 30 sameer@t4tech:~$ bg + sleep 30 &
See all processes running in the background.
Now that you know how to send the processes in the background, you may want to know what commands are running in the background.
For this purpose, you can enter this command in the terminal.
The jobs command will show you all jobs/processes/commands running in the background like this:
jobs  Running firefox & - Running gedit & + Stopped vim
Do you see numbers 1, 2, and 3, etc.? These are the job identifiers. You would also see the sign – and + in two of the commands. The + sign indicates the last job you executed or placed in the foreground. The sign – indicates the second last job you executed
Bring a process to the forefront in Linux.
Good! Then you learned to execute commands in the background on Linux. But what about bringing a process running in the background?
To send the command to the background, he used “bg”. To recover the background process, use the “fg” command.
Now, if you use fg, it will bring to the first process the last process in the background job queue. In our previous example, running “fg” will bring the Vim editor back to the terminal.
If you bring a certain process to the foreground, you must specify its id of work. The job ID is the number you see at the beginning of each line in the output of the “jobs” command.
“n” is the job displayed, as shown in the output of the command jobs.
Open the terminal application and show all processes running in the system, including those running in the background: you can enter this command in the terminal.
Killing a Process
To end the background process, use the kill command followed by the process ID:
This was a quick one but enough for you to learn a few things about running commands in the background in Linux. If you have any questions or suggestions, leave a comment below.