Working in the terminal is very common if you use Linux as your main system. Although you don’t have to do it, you can always optimize certain tasks with it. But occasionally, you need to run background process.
Why run commands in the background?
Usually, when working in the terminal, there are certain processes or commands that should be left running without taking up the terminal. An example of this is Firefox. Imagine that you are going to run it from the terminal for whatever reason, if you do it normally, you will not be able to use that terminal window unless you close the program.
The above also applies a bit to configuration scripts, where some of these commands are not required to be in the foreground. So, the usefulness of this post is 100% up to you and your needs.
How To Run Background process on Linux
There are several ways to do this, but it will always depend on your circumstances and the way you work.
Using the Ampersand Operator
This method is simple to apply, since all you have to do is add a &
to the end of the command. This will assign you a PID, with which you can then terminate or manage the process.
For example:
xed &
Sample output:
[1] 8372
Note: xed is the Linux Mint text editor. It can be another command or application.
This way the application will be displayed, but the terminal will be free because it is running in the background.
The bg command
The next method is with the bg
command on Linux. Although it is not as direct as the previous method, it is quite useful in certain occasions.
If you run any command such as xed
, you can use
xed
You will see that the application associated with the command is executed, but if you press the CTRL + Z
keys to regain control of the terminal, it is best to send the application to the background with the bg
command.
Then, run the command
xed
Press CTRL + Z
and when you regain control of the terminal execute
bg
Sample output:
$ xed
^Z
[1]+ Stopped xed
$ bg
[1]+ xed &
Using the nohup command
The nohup command allows you to quickly and easily send processes to the background. More technically, nohup runs a command immune to hangups, with output to a non-tty.
It comes installed by default on Linux, so you don’t have to do anything to get it.
If you want to send the xed
command to the background, then just run
nohup xed
You will get an output screen like this:
nohup: ignoring input and appending output to 'nohup.out'.
And the desired application will be executed. If you press CTRL + Z
it will end the execution.
How To View the Processes Running in the Background
An important part of this post is to know which processes are running in the background. These processes can become zombie processes that slow down the system. So, it is necessary to know what they are and manage them.
To achieve this, there is the jobs command that quickly shows you these processes
jobs
Sample Output
[1]+ Done xed
Conclusion
Background processes are significant for a system and for the user who runs them. That is why it is significant to know how to manage them and run them as such. Today you learned two simple methods to do it.
I hope you liked the post and help us to share it.