1, View process
1.1 relationship between procedure and process
program
1. Executable code and data stored in hard disk, disk closing and other media
2. Statically saved code
3. Binary file, static
process
1. Program code running in CPU and memory
2. Dynamically executed code
3. Parent and child processes (each program can create one or more processes)
4. It is the process of program operation, with dynamic life cycle and operation state
thread
1. It is the smallest unit that the operating system can perform operation scheduling. It is included in the process and is the actual operation unit in the process
2. A process has at least one execution thread
3. Threads are also called lightweight processes
4. A thread is an internal part of a process
Relationship between program, process and thread
Thread is included in the process. It is not only the actual operation unit in the process, but also the basic unit that can run independently in the operating system. It is also called lightweight process
Multiple threads can be concurrent in a process, and each thread can execute different tasks in parallel, while the processes we see in the task manager are generated by the application we download
Of course, an application can also contain multiple processes
2, View process
2.1 viewing process information ps
2.1.1 viewing static process statistics
[root@localhost ~]# ps -aux ###The process information is displayed as a simple list Common parameters: -a: Display all process information under the current terminal, including the processes of other users. And“ x"When the options are combined, all process information in the system will be displayed -u: Output process information in a user oriented format -x: Displays the process information of the current user under all terminals
Common option combinations:
In the above output information, the first line list title, in which the meaning of each field is described as follows
USER: the name of the USER account that started the process
PID: the digital ID number of the process in the system, which is unique in the current system
%CPU: percentage of CPU usage
%MEM: percentage of memory used
VSZ: amount of virtual memory used by the process (KB)
RSS: amount of physical memory occupied by this process (KB)
TTY: indicates which terminal the process runs on. Processes that are not started from the terminal are displayed as?
Jane said: pts is the remote login terminal ctrl+ALT F1-F6 tty1 image interface 2 and 6 character interface? The process executed by the system itself
STAT: the state of the process (D: non interruptible sleep state: R: running state; S: in sleep state and can be awakened; T: stop state, which may be suspended in the background or the process is in tracking and debugging state: Z: Zombie process, the process has been suspended, but some programs are still in memory)
D: System daemon
T: Mode, program execution, general stop
R: The program is currently in operation or can be operated
S: The program is currently sleeping (it can be said to be idle!), But it can be awakened by some signals. T: The program is currently detecting or stopping
Z: The program should have been terminated, but its parent program cannot normally terminate it, resulting in the non interruptible state of zombie (zombie) program state D
They mean the following:
<: indicates that the process is running on a high priority
N: Indicates that the process is running on a low priority
50: Indicates that the process has pages locked in memory
s: Indicates that the process is a control process
l: Indicates that the process is multithreaded
+: indicates that the current process is running in the foreground
D: System daemon
T mode, general stop of program execution
START: the time when the process was started
TIME: CPU TIME consumed by the process
COMMAND: the name of the COMMAND that started the process
[root@localhost ~]# ps -elf ###The process information in the system is displayed in a long format Common parameters: -e:Displays all process information in the system -1:Display process information in long format -f:Display process information in full format
Explanation of each column:
F: The system token assigned by the kernel to the process.
S: Status of the process
UID: the user who started these processes
PID: process ID of the process
PPID: the process number of the parent process (if the process was started by another process)
C: CPU utilization in the process lifecycle
PRI: priority of the process (the higher the number, the lower the priority)
NI: the humility value is used to participate in the decision of priority
ADDR: memory address of the process
SZ: the approximate size of the swap space required if the process is swapped out
If WCHAN: the name of the process in the sleep system
STIME: system time when the process started
TTY: terminal device when the process starts. pts/0255 represents a virtual terminal, which is generally a remote connected terminal; Tty1 and tty7 represent local console terminals
TIME: the cumulative CPU TIME required to run the process
CMD: start command of process
Zombie process:
A process has ended, but if the parent process of the process has ended first, the process will not become a zombie process, because when each process ends, the system will scan all processes running in the current system to see if any process is a child process of the process just ended. If so, init will take over it, Become its parent process. After the child process exits, init will reclaim the relevant resources it occupies. However, when the child process ends before the parent process, and the parent process does not recycle the child process and release the resources occupied by the child process, the child process will become a zombie process
2.1.2 viewing process dynamic information
[root@localhost ~]# top ###The process information in the system is displayed in a dynamic format top Shortcut keys of command full screen operation interface: Default 3 s Refresh once, press s Modify refresh time by space: refresh now P: Press CPU sort M: Sort by memory T: Sort by time p: process IP,View the status of a process N: The key sorts according to the start time Numeric key 1: displays the of each kernel CPU Utilization rate u/U: Specifies the user to display h:Available top Online help information for the program q:Key can exit normally top program
In the above output information, the first part displays the summary information of system Tasks, CPU occupation, memory occupation (Mem), Swap space (Swap), etc., and the ranking of the current process is displayed below the summary information. The meaning of relevant information is expressed as follows
uptime
13: 22:30 current time
The up 20days system running time indicates that the server has been running for 20 consecutive days
2. Number of currently logged in users
load average: 0.06, 0.60, 0.48 system load, that is, the average length of the task queue. The three values are the average values from 1 minute, 5 minutes and 15 minutes ago to now
System Tasks information: total, total processes; Running, the number of running processes; sleeping, the number of dormant processes; stopped, the number of aborted processes; zombie, number of dead unresponsive processes
CPU occupation information: us, user occupation; sy, kernel occupancy; ni, priority scheduling occupancy; id, idle CPU; wa, I/O waiting for occupation; hi, hardware interrupt occupation; si, software interrupt occupation; st, virtualization occupancy. To understand the percentage of idle CPU, mainly look at the% id section
Memory usage (Mem) information: total, total memory space; Free, free memory; Used, used memory; buff/cache, the sum of physical memory and swap memory buffers.
Swap Occupation: total, total swap space; Free, free swap space; Used, used swap space; Available MEM, available physical space.
Details:
Tasks: 481 total number of processes
1 running the number of running processes
480 number of processes sleeping
0 stopped the number of processes stopped
0 zombie zombie zombie processes
Cpu(s): 0.0% us system user process CPU usage percentage
Percentage of CPU used by processes in 0.0% sy kernel priority scheduling
Percentage of CPU occupied by processes with changed priority in 0.0% ni user process space
98.7% id idle CPU percentage
The total amount of time that 0.0% wa CPU waits for I/0 to complete
Test:
Terminal 1: execute: top
Terminal 2: dd if=/dev/zero of=/a.txt count=10 bs=100M
Terminal 3: dd if=/dev/zero of=/a.txt count=10 bs=100M
0.0% hi (understand) hardware interrupt consumption time hardware interrupt occupation
Hard interrupt, percentage of CPU
0.0% si (understand) soft interrupt consumption time software interrupt occupation
Soft interrupt, percentage of CPU
0.0 st (steal) virtualization occupancy. The percentage of idle CPU depends on the% id
st: the time when the virtual machine steals the physical
Mem: 3861516k total physical memory
710488k total physical memory used
1412956k free total free memory
1738072k buff/cache is used as the amount of memory for the kernel cache. And free -k
Swap: 3071996k total exchange area
Total number of exchange areas used by 0k used
3071996k total free swap area
What is the total available memory of 2825600 available MEM
Line 7 process information:
PID process id
USER user name of the process owner
PR priority (dynamically adjusted by the kernel), the user cannot
NI process priority. nice value. A negative value indicates high priority and a positive value indicates low priority, which can be adjusted by the user
VIRT (virtual memory usage) virtual memory refers to all memory being used by the process (ps is marked as VSZ)
1. The "required" virtual memory size of the process, including the library, code, data, etc. used by the process
2. If a process requests 100 m of memory, but actually uses only 10 m, it will grow
100m instead of actual usage
RES (resident memory usage) is the physical memory used by the process. Actual practical memory (ps marked as RSS) RES: resident memory usage
1. The amount of memory currently used by the process, excluding swap out
2. Share containing other processes
3. If you apply for 100m of memory and actually use 10m, it will only increase by 10m, which is opposite to VIRT
4. As for the memory occupied by the library, it only counts the memory occupied by the loaded library files
SHR shared memory size, in kb SHR: shared memory
1. In addition to the shared memory of its own process, M also includes the shared memory of other processes
2. Although the process uses only a few shared library functions, it contains the size of the entire shared library
3. The formula for calculating the physical memory occupied by a process: RES – SHR 4. After swap out, it will be reduced
S process status
D = uninterrupted sleep state
R = running or operable S = sleeping
T = tracked / stopped Z = dead stop
%Percentage of CPU time consumed since last CPU update
%Percentage of physical memory used by MEM process
Total CPU TIME used by TIME + process, unit: 1 / 100 second
COMMAND command name / COMMAND line
2.1.3 viewing process information pgrep
Query process PID information according to specific conditions
Common parameters -l:Option to output the corresponding process name and PID -U: Option to query the processes of a specific user -t: Option to query the processes running on a specific terminal Example 1 pgrep -l "log" Example 2 pgrep -l -U teacher -t tty1
2.1.4 viewing process information pstree
Lists process information in a tree structure
pstree The command displays only the name of each process by default Common parameters: -p: Options can be used to list the corresponding PID number -u: Option to list the corresponding user names -a: Option to list complete command information Perform the following“ pstree -aup"Command can view the process tree of the current system, Including the corresponding of each process PID Number, user name, complete command and other information. As can be seen from the output results, systemd The process is indeed Linux The "ancestor" of all processes in the operating system Example 1 pstree -aup lu
3, Control process
3.1 manual start
At least one process can be started by the user manually entering the command or the path of the executable program. According to whether the process needs to occupy the current command terminal, manual startup can be divided into foreground startup and background startup.
Foreground start: the user enters a command and directly executes the program.
Background startup: add the "&" symbol at the end of the command line
cp /dev/cdrom /home/cetos7.iso &
3.2 dispatching startup
1. Use the at command to set a one-time scheduled task
2. Use the crontab command to set periodic scheduled tasks
3.3 front and back scheduling of process
Ctrl+z
Suspend the current process, that is, call it into the background and stop execution
jobs command
View a list of tasks in the background
Common parameters -l: Option to display the corresponding of the process at the same time PID number
fg command
To restore the background process to the foreground, you can specify the task sequence number
Using bg (BackGround) command, you can resume the tasks suspended in the BackGround (such as pressing Ctrl + Z to suspend) and continue to perform operations in the BackGround
Use the fg command (ForeGround, ForeGround) to restore the background task to the ForeGround
Unless there is only one task in the background, both bg and fg commands need to specify the task number of the background process as a parameter.
For example, execute the following "fg 1" command to transfer the wget process suspended to the background back to the foreground for execution
[root@localhost ~]# jobs [1]- Stopped cp /dev/cdrom mycd.iso [2]+ Stopped top [root@localhost ~]# fg 1
3.4 terminating the process
Ctrl+c
Interrupt executing command
Kill, kill commands
-
kill is used to terminate the process with the specified PID number.
-
Kill is used to terminate all processes with the specified name.
-
-The 9 option is used to force termination
use kill Command to terminate the process [root@localhost ~]# pgrep -l "sshd" ###Query the PID number of the target process 5822 sshd [root@localhost ~]# kill 5822 ###Terminates the process for the specified PID [root@localhost ~]# pgrep -l "sshd" ###Confirmation process terminated (no result in query)
For system processes that cannot be terminated normally, if necessary, they can be forcibly terminated in combination with the "- 9" option. For example, the following operation shows the process of forcibly terminating the vim process
[root@localhost ~]# vim testfile & ###Open the vim program and suspend it as a test [1]+ Stopped vim testfile [root@localhost ~]# jobs –l ###Query the PID number of the target process [1]+ 7095 Stopped vim testfile [root@localhost ~]# kill 7095 ###Try to end the process normally [root@localhost ~]# jobs -l ###However, it was found that the vim process did not exit [1]+ 7095 Stopped vim tmpfile [root@localhost ~]# kill -9 7095 ###Force termination of target process [root@localhost ~]# jobs -l ###vim process terminated successfully [1]+ 2993 Killed vim tmpfile
use killall Command to terminate the process [root@localhost ~]# vim testfile1 & ###Suspend the first vim test process [1]+ Stopped vim testfile1 [root@localhost ~]# vim testfile2 & ###Suspend the second vim test process [2]+ Stopped vim testfile2 [root@localhost ~]# jobs -l ###Confirm the process information to be terminated [1]- 7144 Stopped vim testfile1 [2]+ 7153 Stopped vim testfile2 [root@localhost ~]# killall -9 vim ###Terminate multiple processes by process name [root@localhost ~]# jobs -l [1]- 7144 Killed vim testfile1 [2]+ 7153 Killed vim testfile2
Use the pkill command to terminate the process
Using pkill command, a specific process can be terminated according to the name of the process, the user running the process, the terminal where the process is located and other attributes. Most options are basically similar to pgrep command, such as "- U" (specified user), "- t" (specified terminal) and other options, which are very convenient to use. For example, to terminate processes started by the user lu, including logging into the Shell, you can do the following
su - hmj vim tst.txt & [root@localhost ~]# pgrep –l -U "lu" ###Confirm target process related information 3773 bash [root@localhost ~]# pkill -9 -U "lu" ###Forcibly terminate the process of user hackli [root@localhost ~]# pgrep -l -U "lu" ###Confirm that the target process has been terminated
4, One time task settings
at command
One time scheduled task
at [HH:MM](Time) [yyyy-mm-dd](Date (mm / DD / yyyy) After setting the task, press Ctrl+D Key combination commit
Example:
[root@kgc /]# date 2021 Monday, August 16:20:58 +00 [root@kgc /]# at 16:23 2021-08-16 at> pgrep -U root | wc -l > /tmp/ps.root ###Count the number of processes run by root user in the system at this time point, and save the value to / tmp/ps.root file at> <EOT> ###After setting the task, press Ctrl + D to submit job 1 at Mon Aug 16 16:23:00 2021 [root@localhost ~]# cat /tmp/ps.root ###Verify the command results after the scheduled time
To delete the at task with the specified number, you can use the atrm command. The deleted at task will not be executed and will not be displayed in the display result of atq command, but the executed task cannot be deleted. The specific operations are as follows:
[root@localhost ~]# atrm 2 / / delete the second at scheduled task [root@localhost ~]# atq / / confirm that task 2 has been deleted
5, crontab recurring task configuration
The crontab command is used to manage users' scheduled tasks and set users' periodic scheduled task list. Different scheduled task management operations can be completed by combining different options
Common options: -e: Edit scheduled task list -u: Specifies the user to which the managed scheduled task belongs. By default, it is for the current user (yourself). Generally, only root Users have permission to use this option (for editing and deleting scheduled tasks of other users) -l: The list shows scheduled tasks -r: Delete scheduled task list
Use of crontab command options
crontab -u user name ###Specify the cron service of XX user crontab -l ###List the details of cron under the current user crontab -u zx -l ###List details of cron under zx user crontab -r ###Delete the cron content of all users. Ordinary users can only delete their own cron content crontab -r -u zx ###Delete zx user's cron content crontab -e ###Edit cron service
Configuration format
Special representation of event values
* : Represents any time within the range , : Represents multiple discrete time points of an interval - : Represents a continuous time range / : Specify the time and frequency of the interval
Example: 0 17 * * 1-5 17 every day from Monday to Friday:00, 30 8 * * 1,3,5 Every Monday, Wednesday and Friday at 8:30 0 8-18/2 * * * 8 Every 2 hours between 00:00 and 18:00 0 * */3 * * Every 3 days
Case:
[root@localhost ~]# crontab -e 50 7 * * * /usr/bin/systemctl start sshd.service 50 22 * * * /usr/bin/systemctl stop sshd.servic ###Every morning, the sshd service is automatically started at 7:50 and closed at 22:50 0 0 */5 * * /usr/bin/rm -rf /var/ftp/pub/* ###Empty the data in the FTP server public directory / var/ftp/pub every five days 30 7 * * 6 /usr/bin/systemctl httpd restart ###Restart the httpd service in the system every Saturday at 7:30 30 17 * * 1,3,5 /usr/bin/tar jcf httpdconf.tar.bz2 /etc/httpd/ ###At 17:30 p.m. on Monday, Wednesday and Friday, use the tar command to automatically back up the / etc/httpd directory. The root user can set the following scheduled tasks through crontab
6, Summary
1. Commands for viewing processes (ps, top, pgrep, pstree)
2. Process control (start process, schedule process, terminate process)
3. Set scheduled tasks with the at command
4. Configuration field of crontab scheduled task