Let's continue our discussion on processes! There are basically two types of processes: foreground 1
and background processes! Foreground processes are started by the user and not by the system services 2
and while they are running, the user cannot start another process from the same terminal. By default 3
every process that you start runs in the foreground; it gets its input from the keyboard and sends its 4
output to the screen, to the terminal. 5
To simulate an expensive task, I'll run the sleep command, which poses or delays the execution for 6
a given amount of seconds. 7
sleep 15. 8
A foreground process was started and I have to wait until it terminates, I cannot start another process 9
from the same terminal. 10
So I'm waiting for 15 seconds. 11
And it's done; now I can start another process by running another command. 12
Let's move to the background processes! They are non-interactive and are started or executed by system 13
services or by the users. While they are running 14
you can start other processes within the same terminal. To start a background process 15
you add an ampersand at the end of the command. 16
I'll execute the sleep again, this time in the background. 17
While the process is running, I don't have to wait until it completes to start another one. 18
The process is still running because it takes 20 seconds and while it's running, I can run other cammands 19
in the same terminal. 20
The sleep process is finally done. 21
Note that even though you start the process in the background, it still redirects its output and errors 22
to the terminal where it was started from. 23
I'm executing the ls command in the background. 24
The process was running in the background, but the output was printed to the same terminal. Because 25
these background processes are non-interactive and do not require human intervention it’s common to 26
redirect both the output and the errors to a file like this. 27
ifconfig 28
I'm redirecting the output to a file output.txt 29
and the errors ,if there are any. 30
cat output.txt and cat errors.txt the errors file is empty. 31
If you want to run a command in the background and you are not interested in either its output or errors, 32
you can retract both the output and the errors to /dev/null slash. Let's see an example! 33
I'm pinging Google.com and I'm running the command in the background -c1 34
I'm sending just one packet. 35
google.com We will see that the output is printed in the terminal. 36
Now I’ll redirect both the output and the errors to /dev/null so nothing will be printed out. 37
> /dev/null 38
the output and the errors 39
I'm redirecting the errors to the same file, so the same as the output and I'm running it in the background. 40
And nothing was printed out. 41
It may look confusing, but it's fairly simple to understand. /dev/null 42
is a special file called the null device and colloquially is called the blackhole because it 43
immediately discards anything written to it. 44
By the way, if you don't have an active Internet connection to ping a Google.com, just ping your 45
loopback interface IP address. 46
This IP address.