惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
G
Google Developers Blog
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
小众软件
小众软件
月光博客
月光博客
Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理
P
Proofpoint News Feed
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
The Cloudflare Blog
博客园_首页
美团技术团队
大猫的无限游戏
大猫的无限游戏
B
Blog
IT之家
IT之家
Jina AI
Jina AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - tony

The fastest way going abroad... Love is meeting the requirments, too... Fight back and hold back... Section 14: The UNIX File System Section 13: Interactive Use of the Shell Section 12: Customizing the UNIX Shell Section 11: The Execution Environment Section 10: Editing Text with EMACS Section 8: Process Control and Multitasking Section 7: Pipelines and Filters Section 6: Redirecting Input and Output Section 5: Working with Files and Directories Section 4: The UNIX Shell Section 3: Logging In and Logging Out Section 2: Accessing a UNIX System Section 1: What Is UNIX? An overview about our Martch work... Obesession... Oh,,,CHANEL...
Section 9: Interaction and Job Control
tony · 2005-03-31 · via 博客园 - tony

When you log in to a UNIX system, the kernel starts a shell for you, and connects the shell to your terminal. When you execute a command from the shell, the shell creates a child process to execute the command, and connects the child process to your terminal. By connecting the child process to your terminal, the shell allows you to send input to the child process, and receive output from it. When the child process finishes, the shell regains access to the terminal, redisplays the shell prompt, and waits for your next command.

Any task that requires you to actively participate (like word processing) must be in the foreground to run. Such jobs, termed "interactive," must periodically update the display, and accept input from you, and so require access to the terminal interface.

Other jobs do not require you to participate once they are started. For example, a job that sorts the contents of one file and places the results in another file, would not have to accept user commands, or write information to the screen while it runs. Some UNIX shells allow such noninteractive jobs to be disconnected from the terminal, freeing the terminal for interactive use.

Note that it is even possible to log out, and leave a background process running. Unfortunately, there is no way to reconnect a background job to the terminal after you've logged out.

Jobs that are disconnected from the terminal for input and output are called "background" jobs. You can have a large number of background jobs running at the same time, but you can only have one foreground job. That is because you only have one screen, and one keyboard at your terminal.

Background and foreground jobs

The process that is connected to the terminal is called the foreground job. A job is said to be in the foreground because it can communicate with the user via the screen, and the keyboard.

A UNIX process can be disconnected from the terminal, and allowed to run in the background. Because background jobs are not connected to a terminal, they cannot communicate with the user. If the background job requires interaction with the user, it will stop, and wait to be reconnected to the terminal.

Jobs that do not require interaction from the user as they run(like sorting a large file) can be placed in the background, allowing the user to access the terminal, and continue to work, instead of waiting for a long job to finish.

Starting a job in the background

To place a job in the background, simply add the ampersand character (&) to the end of your shell command.

EXAMPLE: To sort a file called "foo", and place the results in a file called "bar", you could issue the command

sort < foo > bar &

Examining your jobs

The UNIX command jobs allows you to see a list of all the jobs you have invoked from the current shell. The shell will list the job ID of each job, along with the status (running, stopped, or otherwise), and the command that started the job. The shell considers the most recently-created (or most recently-manipulated) job to be the current job, marked with a plus sign. Other jobs are referred to as previous jobs, and are marked with a minus sign. The commands related to job control will apply to the current job, unless directed to do otherwise. You may refer to jobs by job ID by using the percent sign. Thus, job 1 is referred to as %1, job 2 is %2, and so forth.

Suspending the foreground job

You can (usually) tell UNIX to suspend the job that is currently connected to your terminal by typing Control-Z (hold the control key down, and type the letter z). The shell will inform you that the process has been suspended, and it will assign the suspended job a job ID.

There is a big difference between a suspended job, and a job running in the background. A suspended job is stopped, and no processing will take place for that job until you run it, either in the foreground, or in the background.

Placing a foreground job into the background

If you started a job in the foreground, and would like to place it in the background, the first thing you must do is suspend the job with a Control-Z, freeing your terminal. Then, you can issue the UNIX command

bg

to place the suspended job in the background. The bg command can accept a job ID as an argument. If no job ID is given, bg assumes you are referring to the current (suspended) job.

Bringing a background job to the foreground

You can reconnect a background job to your terminal with the UNIX command

fg

The fg command will accept a job ID as an argument. Make sure to include the percent sign:

fg %2

will bring job 2 into the foreground. If no job ID is given, fg will assume you are referring to the current (suspended) job.

Starting a suspended job

If you have a suspended job that you'd like to resume running, first you must decide whether you want it running in the foreground, or the background. Find the job ID of the suspended job with the jobs command, and then use bg (to run the job in the background), or fg (to run the job in the foreground).