Linux Shell study note 4

Source: Internet
Author: User

6th hours Process

6.1 start a process

In UNIX, whenever a command is executed, it creates or starts a new process.

For example, when you use the LS command to list a directory, a process is started.

 

The operating system uses a five-digit ID called PID or process ID to track processes. Each process in the system has a unique PID.

The PID may eventually be repeated because all possible numbers are used up, so the next PID starts from scratch.

There are no two identical PID processes in the system at any time, because the PID is UNIX used to track every process.

 

6.1.1. Foreground Process

By default, each process started by the user runs on the foreground. The process obtains input from the keyboard and sends the output to the screen.

When running a command, you cannot run other commands (start any other process ). In this case, you can enter a command,

It is not displayed until the preceding command is executed.

UNIX provides some measures to start the process in the background, suspension the foreground process, and move the process between the foreground and background.

 

6.1.2 background process

A background process does not need to be connected to the keyboard when running. If the background process requires a keyboard input, wait.

The advantage of running a process in the background is that you can run other commands, and you do not need to wait for one process to complete and then start another.

 

The simplest way to start a background process is to add a & Symbol after the command

Example: $ ls Cho *. Doc &

 

Background process to be entered

If the user runs a background process that requires input and does not redirect it to a read file instead of a keyboard input, the process stops.

If you set process and task monitoring to available, a message is returned when you press ENTER or start a command at an empty command prompt.

 

1. Move a foreground process to the background

In addition to running a process in the background, you can also move a foreground process to the background.

When a foreground process is running, shell does not process any new commands. Before entering a new command, the user must suspension the foreground process to get a prompt.

 

When the current process is suspended, a command prompt allows you to enter other commands. The original process is still in memory, but does not occupy the CPU time.

To restore the foreground process, you have two options: the background and the foreground.

The BG command allows the user to set the hanging process to the background; The FG command can return the process to the foreground.

Use Ctrl + Z to stop a process. BG resumes a process.

 

2. Move a background to the foreground

When a process is in the background or suspended, you can use the FG command to move it to the front.

By default, the last process suspended or moved to the background will be moved to the foreground.

 

3. Keep the background process running (nohup command)

When you exit the system or the connection is unexpectedly interrupted, you can prevent a background process from terminating. The nohup command prevents user processes from receiving hup signals.

Just put nohup in front of the running command.

Since nohup is designed to run without a terminal, it wants the user to redirect the output to a file. If this is not done, nohup will automatically redirect to

It is called nohup. Out.

 

4. Wait for the background process to end (wait command)

There are two ways to wait for a process to end before doing other things. You can press enter several minutes until you get a complete message. Or use the wait command

There are three methods to use wait: no option, process ID, and task id prefixed with "% ".

Example: $ wait % 1

You cannot enter other commands until the task ends.

 

6.2 list Running Processes

A user can start a process on the frontend or backend, suspension them, and move them between the frontend and backend. But how can I know which processes are running?

You can use jbos or PS commands;

6.2.1 ps command

By default, a running process is displayed. It can also accept many different options.

For each running process, it provides four pieces of information: PID, tty (terminal for running the process), CPU time consumed by the process, and command name.

In PS, the-F option is the longest to use and provides more information.

The information list is as follows:

Column Name Description

User ID of the UID Process

PID process ID

Ppid parent process ID (the process ID that starts it)

CPU usage time of the C Process

Stime process start time

CMD command to start the process

 

6.3 kill a process)

$ Kill % 1

% 1 indicates the task number

You can use the process ID instead of the % task label to kill a specific process. To use the process ID to kill a process, run the following command:

$ Kill 6639 # (PID)

 

6.4 Parent and Child Processes

In the above $ PS-F example, each process is assigned two IDs: process ID (PID) and parent process ID (ppid ). Each user process in the system has a parent process.

Most of the commands run by the user regard shell as the parent process.

The shell parent process is usually an operating system or terminal communication process.

 

6.4.1 task id and process ID

The background process and hanging process are usually operated by the task label. This number is different from the process ID because it is used for a short period of time.

 

6.4.2 subshells

When a user runs a shell script, in addition to any commands in the script, a copy of another shell interpreter is created.

The new shell is called a subshells, just like a sub-directory that is contained in another directory.

Example: shell script file psit

#! /Bin/KSh

PS-Ef | grep Root

Exit 0

When running, psit returns the following results:

$ Psit

Root 9830 3662 0 22:57:00 pts/6 KSh psit

Root 9831 9830 0 22:57:01 pts/6 Ps-ef

Root 9832 9830 0 22:57:01 pts/6 grep Root

Root 3662 3657 0 22:57:01 pts/6-KSh

As shown in the preceding figure, when running the psit script file, a parent process KSh psit is generated, and all commands executed will become sub-processes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.