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

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security 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).