Learning bash notes-process Processing

Source: Internet
Author: User

Learning bash notes-process Processing
1. process ID and job number. When you run the command after attaching the & amp; #, the shell will respond as follows:
$ Ls &
[1] 3318
[1] indicates the job number and 3318 indicates the process number. When a background process is completed, shell will provide the job ID information, as shown in the following figure: [1] + Done ls -- color = auto
If a job exits in a non-zero state, shell indicates that the job exits. 2. Job control job numbers allow them to control jobs in shell commands. Once a job runs in the background, you can keep it running, put it on the foreground, or send a signal to it. . The front-end and back-end built-in fg commands place background jobs to the front-end. If you use fg without parameters, shell will put the background job to the foreground. If there are multiple jobs in the background, shell will pick out the latest jobs running in the background and put them to the foreground. If you want to put other jobs to the foreground, you need to give the job command with the percent sign % in front, or use the job number with the percent sign % in front, or the process ID without the percent sign. You can use the command jobs to list background jobs. Example: yanwenjie @ ywjpc :~ /Ctest $./&
[1] 3481
Yanwenjie @ ywjpc :~ /Ctest $./B &
[2] 3482
Yanwenjie @ ywjpc :~ /Ctest $./c &
[3] 3483
Yanwenjie @ ywjpc :~ /Ctest $ jobs
[1] Running./&
[2]-Running./B &
[3] + Running./c &
-P option only lists process numbers:
$ Jobs-p
3481
3482
3483
If you type fg, c is put on the foreground because it is the latest job running in the background. If you type fg % B or fg % 2, B enters the foreground. You can also use % + to reference the latest job that has been put on the background and %-to reference the next job that has been recently put on the background. Here is B.
The following lists several methods to reference background jobs: % N job no. N % string jobs whose command starts with string %? String the command contains the string job % + recently called background job % same as %-second recently called background program 2. 2. to suspend a job, you must press ctrl-z to stop it. Shell will send the following message: $./
^ Z
[1] + Stopped./
Then, return to the shell prompt, and restore a host-mounted job so that it can continue running on the foreground. Type fg. If you have multiple pending jobs, you can use
Fg of the industry name or serial number.
Yanwenjie @ ywjpc :~ /Ctest $ jobs
[1] Stopped./
[2]-Stopped./B
[3] + Stopped./c
Yanwenjie @ ywjpc :~ /Ctest $ fg % 1
./
If you press ctrl-z followed by bg, the job is run in the background.
Yanwenjie @ ywjpc :~ /Ctest $ jobs
[1] Stopped./
[2]-Stopped./B
[3] + Stopped./c
Yanwenjie @ ywjpc :~ /Ctest $ bg % 2
[2]-./B &
Yanwenjie @ ywjpc :~ /Ctest $ jobs
[1]-Stopped./
[2] Running./B &
[3] + Stopped./c3. signal 3. 1. When the control key signal is ctrl-c, shell sends an INT signal to the current job. When ctrl-z is entered, shell sends TSTP. You can also send a QUIT signal to the current job by pressing ctrl -\. You can use the stty Command Option to customize the control key for sending signals. This varies with the system. The common syntax is stty signame char. When signame is used, the letter name. char is a control character. You can use the ^ symbol to indicate control followed by control characters. For example, to set the INT key to ctrl-x on most systems, you can use stty intr ^ x. 3.2.kill you can use the built-in shell command kill to send a signal to any process you create. The kill parameter is the process ID and job number. By default, kill sends the TERM signal, which is the same as the INT signal sent using ctrl-c. The following is an example of kill. Here is a process a with process number 2680 and job number 1. You can start using the following command:
#./&
[1] 2680

# Kill % 1

[1] + Terminated./

If you do not see the message, the TERM signal will interrupt the job and you will try again in the next step:

Kill-QUIT % 1

If the job is normal, you will see:

[1] + Quit (core dumped)./

If QUIT is not running properly, use KILL as the second method:

# Kill-KILL % 1

[1] + Killed./

3.3.traptrap built-in commands enable you to capture specific signals and process them in your own way. Trap built-in commands allow you to complete this function in a shell script. Trap Syntax: trap cmd sig1 sig2... it means that when sig1 and sig2 are received, run cmd and resume execution. After cmd is completed, the script resumes execution after the command is interrupted. Cmd can be a script or function. Sigs can be specified by name or number. Example: trap "echo 'you hit ctrl-C'" INT
While true; do
Sleep 60
Done
Run: #./a. sh
^ Cyou hit ctrl-c
^ Cyou hit ctrl-c
After pressing ctrl-c, the script will not stop running, but the sleep command will exit, and the script will loop back to start another sleep. 3. 4. The process ID variable $ is a special shell variable. The value is the process ID of the current shell. For example, the following Script: echo $
While true; do
Sleep 10
Done
Execution result: root @ ywjpc:/home/yanwenjie/bashtest #./a. sh &
[1] 3258
Root @ ywjpc:/home/yanwenjie/bashtest #32583. 5. Reset the trap signal another exception of the trap command occurs when the dashes are specified as command parameters. It will reset the behavior when receiving the signal to the default relief, usually the termination of the process. For example, a. sh is as follows: $ cat a. sh
Trap "echo 'ctrl c is wrongly ed'" INT
I = 5
While [$ I-gt 0]; do
Sleep 5
I = $(I-1 ))
Done
Trap-INT
I = 5
While [$ I-gt 0]; do
Sleep 5
I = $(I-1 ))
Done
Execute the Script: $./a. sh
^ Cctrl c is already ed
^ Cctrl c is already ed
^ Cctrl c is already ed
^ Cctrl c is already ed
^ Cctrl c is already ed
^ C4. the following script is used as the synergy program:
Alice & hatter at this time, the last command in the script in hatter. The above code can work normally only when alice completes the first. If alice is still running when the script is complete, it becomes an orphan. There is one way to ensure that the script is not completed before alice completes: the built-in command wait. When no parameter is provided, wait indicates waiting until all background jobs are completed. Therefore, make sure that the above Code works properly. Add the following wait: alice & hatter wait. If hatter completes the process first, the parent shell waits until alice finishes. 5. sub-shell5.1. the most important thing about sub-shell inheritance is that they have obtained or inherited the following features from their parent shell: standard input, standard output, and standard error of the current directory environment variable, and any other open file descriptor. Ignored signal. The sub-shell does not inherit the following content from its parent shell: shell variables are not ignored for Signal Processing 5. 2. the nested sub-shell does not need to be placed in a separate script. You can also subscribe the shell in the same script as the parent shell. Some shell code can be placed in parentheses, and the code will run in the subshell. We call it a nested subshell. Example: (while read line; do echo $ line done) | the code in the dc parentheses runs as a separate process. This is generally not as efficient as a command block. Sub-shell and command block have very few functional differences. The main difference between them is the scope; that is, some definitions in this range are known, such as shell variables and signal traps. First, the code in the nested sub-shell is subject to the above sub-shell inheritance rules. In addition to the variables defined in the external shell, the block can be seen as a code unit that inherits all the content of the external shell. Second, the variables and signal traps defined in a command block are known for the shell code after the block, but not in the sub-shell.

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.