When starting a process in the background, you will see two numbers printed in the terminal. Each 1
background process is identified by a job id 2
besides its PID. The first number is the jobid and the second one is the process id. 3
If you hit the enter key and the process has already finished successfully, you will get the same job 4
id and the word "done". 5
If, from the same terminal, you start more processes in the background, you can list their job and 6
process IDs using the jobs command. I'm starting two processes in the background. 7
And I'm running the jobs command. It is displaying the jobs id between square brackets, the process state and 8
the command that has created the process. If you add the minus L option, it will display the process 9
id as well. 10
Note that the jobs are maintained by the current shell, so the job id is local to the current shell, 11
or you can have the same job id in different shell stations. 12
But the process id is maintained by the system kernell and has a global scope. 13
You can bring a background running process to the foreground any time by running the fg command. 14
fg stands for foreground. 15
I’m running a new command in the background and then I’ll bring it to the foreground: sleep 10 & 16
and jobs. Its job id is 1 so 17
the percent sign and the job id. And the command was brought to the foreground. 18
It was done quickly because it waited only for 10 seconds. 19
But you can use a larger number; you can suspend the process running in the foreground any time by 20
pressing Ctrl +Z 21
I'm starting a new process, this time in the foreground. 22
And I'm suspending it by pressing Ctr +Z. Note that the process was not terminated or discarded from 23
the ram, but suspended or temporarily stopped. 24
I can see the process in the process list. 25
It's there! I can resume it any time in the background by executing the bg command or in 26
the foreground by executing the fg command. 27
In this example, I'll resume it in the background. 28
First, I'm listing the jobs and I'm resuming it by running bg%1. 29
It was resumed in the background. 30
Or I resume the process in the foreground; I'm running the sleep command again with a random number, I'm 31
pressing control Z. 32
The process was stopped. 33
I'm listing the jobs and I'm resuming it in the foreground. 34
fg % 1 35
Now, it was resumed in the foreground. 36
Let's move on and see what happens to a process if we close the terminal, log out or disconnect if it's 37
a remote connection. 38
So sleep 123 39
And I'm closing that terminal. 40
I'm opening it again and search for the process. 41
And we noticed that the process is not running anymore. 42
It was killed. 43
Let's do the same 44
just that this time, we'll start the process in the background. 45
It was started in the background, I'm closing the terminal, I'm opening it again and searching for 46
the process. 47
And it's gone. 48
I've got the same result. 49
The process was killed. 50
When you close a terminal, log out or disconnect the HUP signal, which stands for “hang up”, 51
is sent to the process and the process will get killed. 52
This becomes problematic because if you run a command over ssh and your connection drops or you 53
log out, the station is terminated and all the processes executed from the terminal will receive the 54
hang up signal and stop. 55
There are lots of programs like backup scripts that take a lot of time to complete and you don't want 56
to wait for them. To avoid this issue 57
you use nohup command which will make the process to ignore all the hang up signals. 58
The syntax for no hup is nohup and the command you want to execute in the background without getting 59
impacted by the terminal closing or by the disconnection. 60
So no hup sleep 61
123& 62
I'm closing the terminal and I'm opening it again, and I'm searching for the process. 63
This time, the process is still running. 64
It was not terminated. 65
Take a closer look at this command! 66
The output displayed on the screen is: nohup ignoring input and appending output to 'nohup.out' 67
You normally run a command using nohup and then disconnect. 68
When you return, you want to see its output; nohup redirects the command output to a file called nohup.out. 69
The file is created in the current working directory or if the user running the command doesn’t 70
have the write permission to the current directory, the file is created in the user’s home directory. 71
This is the file! Let's see an example! 72
nohup ifconfig & 73
And cat nohup.out 74
The file will contain the output of the command. 75
If a process is started with nohup and its parent, which is the bash shell, gets terminated, the process 76
will be adopted by init or systemd which runs as the first process on boot and has PID 1. 77
Note that there are several alternative programs that you can use to avoid the command to be terminated 78
when you close the terminal or get disconnected. Some of such well-known programs are Screen and Tmux. 79
Take a look at them because they are really useful.