Shell script learning notes (8)-process management and work control

Source: Internet
Author: User

Process management and work control

Use of ps, top, and htop commands

Transmit signal

You can use the kill command to manage the signal sent by the process. The basic principle for managing processes is

1. First, send a signal to the process so that the process can do what it should do. If the process does not listen to the command, or loses control, then send a mandatory signal. The advantage is that the process should have a buffer time before the end, such as clearing the disk, locking the file, and recording the process number file. Otherwise, when the process ends abnormally, if these files are left on the host and the program is to be executed the next time, it may fail to be started.
2. Adjust the priority of processes so that each process can be properly arranged and all work can be completed as expected. The priority of the process going to the execution schedule (schedule) and becoming the nice value. The nice value is usually between-20 and ~ Between 20. The smaller the nice value, the higher the priority. Only root users can adjust the range from-20 ~ The nice value of 0. Users with General permissions can only adjust the nice value of their processes to 0 ~ 20. In other words, only the root user can increase the priority of the process. Generally, the user can only lower the priority of the process. Nice command and renice command. Nice is used in unexecuted programs, and renice is used in executed processes.

Nice-n nice value specifies that the priority of a program execution is a nice value. -20 ~ 19, but only root can use-20 ~ 0. If-n is omitted and the nice value is not specified, the default value 10 is used.
Renice nice value-p PID-g group-u user name-p-g-u three options, can be used separately or in combination

PRI is also a representation of process priorities. Unlike nice (NI), PRI is always expressed as 0 or positive: PRI = NI + 20

Work Control

In bash interactive mode, you can pause a process and resume it later. This mechanism allows users to manipulate processes and becomes a job control)
Example

#!/bin/bash

for ((;1;))
do
sleep 10
done


Organize

Common work control commands
& Drop the instruction to the background for execution
Bg throws the paused work to the background and uses ctrl + z to pause the process. Then, it works with the bg command to throw the work to the background, which is the same as executing the "command &".
Fg pulls the paused work back to the front-end for execution
Jobs list jobs or jobs-l
Disown Removal
Kill sends signals to processes or work
Wait waits for the sub-process to end and returns the execution status of the last command.
Suspend pause the current shell. Suspend suspends a non-login shell and suspend-f suspends the current login shell. The suspended shell continues running only when it receives the CONT signal.

Process replacement

 
Process replacement allows us to put the standard output, one to many processes as the input; or multiple processes get the output, and then put it to a program for processing.
Replacement usage:
1 command <(command line) uses the device file/dev/fd/n or the cache file to output the process to the <left-side command to read
2> (command line) This is to provide data to the processes in () through the device file/dev/fd/n or the cache disk.

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.