1. First, let's take a simple look at the concepts related to programs and processes: some binary files for users. Process: some information related to the program that is loaded into the memory after the program runs. Such as process id PID and memory usage. A program is a static concept. it is a Software Asset. Information & n
1. let's take a simple look at the concepts related to programs and processes.
Program: binary files used by users.
Process: some information related to the program that is loaded into the memory after the program runs. Such as process id PID and memory usage.
A program is a static concept. as a software resource, a process is a program execution process. it is a dynamic concept and has a certain life cycle. it is generated and extinct dynamically.
There is no one-to-one correspondence between programs and processes. A program can be shared by multiple processes. On the other hand, a process can execute several programs sequentially in the activity.
Parent process: the first process when the program runs can be understood as the parent process.
Sub-process: a process derived from a program process is called a sub-process.
When the parent process is terminated, the child process also terminates. But the child process is terminated, and the parent process is not necessarily terminated.
2. task management
Work Management is the behavior management of multiple jobs simultaneously under a single terminal interface in the Bash environment. For example, after logging on to Bash, you need to compile software, copy files, edit files, and so on at the same time. This requires job management. Of course, you can also open several more terminals to do the above work at the same time. However, after all, the terminal is sometimes used up, or the number of use or connections is limited, it is possible that only one terminal can be opened to work for you.
Here we also need to know what the foreground and background processes are.
Foreground: for example, after entering a command at the shell prompt, create a sub-process, run the command, and shell waits for the command to exit, and then returns to give a prompt to the user. This command is run asynchronously with shell, that is, on the foreground. you cannot execute another command before it is completed.
Background: for example, after entering a command at a shell prompt, if a sub-process created by shell is followed by an &, run this command without waiting for the command to exit, and directly return a prompt to the user. This command is run in sync with shell, that is, in the background. However, the background process must be non-interactive. Then, the background process cannot be terminated using Ctrl + c.
Related commands
Run the command in the background, that is, add "&" to the end of the command.
Process suspension and termination
Pause: Ctrl + z
Termination: Ctrl + c
Process Recovery
Resume to the foreground and continue running: fg
Resume to the background and continue running: bg
View background processes: jobs
The following is an example.
[Root @ yufei ~] # Cp-r/usr // tmp &
[1] 1627
Here we put the copied command in the background for operation. his PID is 1627, and the task number is 1.
[Root @ yufei ~] # Jobs
[1] + Running cp-I-r/usr // tmp &
The command is running.
If you set the command to the foreground, use the following command:
[Root @ yufei ~] # Fg % 1
Cp-I-r/usr // tmp
Use Ctrl + z to throw the command to the background and pause it.
^ Z
[1] + Stopped cp-I-r/usr // tmp
View task status
[Root @ yufei ~] # Jobs
[1] + Stopped cp-I-r/usr // tmp
Let it continue to work
[Root @ yufei ~] # Bg % 1
[1] + cp-I-r/usr // tmp &
We can see that the task continues.
[Root @ yufei ~] # Jobs
[1] + Running cp-I-r/usr // tmp &
If jobs is used for viewing after the task is completed, a Done prompt is displayed.
[Root @ yufei ~] # Jobs
[1] + Done cp-I-r/usr // tmp
Note: Even if the program is put in the background for execution, if your terminal is disconnected, the program executed in the background will also be interrupted. The following methods can be used to solve this problem.
1. implement it with scheduled tasks (this is not much to say, because it is common)
2. use nohup: nohup [commands and parameters] &
[Root @ yufei ~] # Nohup cp-r/usr // tmp &
[1] 1699
[Root @ yufei ~] # Nohup: ignoring input and appending output to 'nohup. out'
[Root @ yufei ~] # Jobs
[1] + Running nohup cp-r/usr // tmp &
[Root @ yufei ~] # Exit
Log on again.
[Root @ yufei ~] # Jobs
We can't see anything at this time, but we can find it with ps.
Ps-ef | grep cp
Root 1699 1 10? 00:00:06 cp-r/usr // tmp
The process ID is still 1699, which is the same as the above. This means that even though the terminal exits, the task is not interrupted and continues to be executed.
From Yufei blog