From the operating system perspective, a job is a user task running in a computer system. In Unix/Linux systems, a job can be defined as one or more commands entered in the command line. Linux is a real multi-user and multi-task operating system, so you can run multiple tasks at the same time. However, when you start a job on the foreground, both the standard input and the standard output are locked, they are exclusive to the current job until the job ends. Therefore, Linux defines two types of jobs: foreground jobs and background jobs. One front-end job We generally runProgramThey are all foreground jobs. Other jobs cannot be started during the running process, and it is quite simple to start foreground jobs. Enter the command and press Enter. ★Suspends a foreground job To suspend a foreground job, press Ctrl + Z. This is useful in full-screen editing mode. you can pause the editing job to complete other tasks. After returning to the terminal, we can execute jobs-L. View information about the suspended job. ★Resume a suspended job To resume a suspended job, you only need to execute the foreground command FG on the terminal. ★Terminate a foreground job To terminate a foreground job, press Ctrl + C. After the job is terminated, press enter to activate the command line prompt. 2. background jobs When a job takes a long time, we may need to run the command in the background to complete other tasks. ★Start a background job To start a background job, you only need to add the Symbol & behind the command &. Note that if the job has parameters, the & sign follows the last parameter. No space is left between the end and the end of the command.
Note that foreground and background jobs share resources such as keyboards and monitors. background jobs and foreground jobs compete for CPU time slices, therefore, any messages sent by the background job to the monitor will be mixed with messages from the current job, which may be very messy soon. A background job must be moved to the foreground to request user input. It is strongly recommended that the input and output of background jobs must be redirected. It is best to redirect the terminal output file or the null device. You can try the background ping command without parameters, such as ping localhost!
★Suspend, restart, and terminate background jobs To suspend a background job, use the Stop command, restart the BG command, and terminate the kill command. Each of the three commands requires a job number of %. You can use jobs to view the job number. ★Job frontend and backend status Switch A suspended job can be switched between the frontend and backend statuses. Once a job is suspended, you can use the BG command to execute the job in the background. Move a background task to the foreground and use the FG command. ★Multi-Background job When multiple background jobs are running, the job number is used to mark the jobs to be processed. Three Job Queue Run the jobs [-L] command to view the current job and its status. Each job has a corresponding job number, current value, and running status. ★Current Value The + sign in the job list. The-sign is the current value flag. + "#" Indicates that the current default job is changed. "-" indicates that the current default job is replaced by the current default job after the current default job is completed. Here =,-indicates a process priority. . When a job is suspended, it is called the default current value flag. If multiple jobs are suspended, the corresponding id value is assigned in the order of suspension. The first sign to be suspended is the plus sign, the earlier version is the minus sign, which is easy to understand by priority. ★Use job number You can add the job number parameter to the preceding commands. The job number starts with "%" and is separated by a space. The default job number can be % + or %. The job that contains the current value of the minus sign can be %-expressed. For example, end the current default job: Kill % or kill % +. ★Job status Lifecycle A job may be in one of three states: foreground, background, and suspended. When a job is started, it runs on the foreground. During the running period, the job can be suspended, terminated, or run until the job stops automatically. Pending jobs can also be transferred to the background for further execution. A job can be executed with manual intervention from creation to completion. We can better control our job tasks so that Linux can better serve us and improve work efficiency. This article focuses on simple concepts, so the key is to practice it on your own. Of course, this is just a reference for new users. Masters can pass! |