Process and scheduled task management

catalogue

1, Relationship between programs and processes and threads

2, View process

2.1. View static process statistics ps command

2.1.1. Method 1: ps -aux

2.1.2. Method 2: PS Elf

2.2. View dynamic process ranking information top command

2.3. View process information pgrep

2.4. View process information pstree

3, Control process

3.1. Start process manually

3.2. Dispatch start

3.3. Front and back scheduling of processes

3.3.1Ctrl+Z combination key

3.3.2. Viewing a list of tasks in the background: jobs command

3.3.3. Restore the background process to the foreground run: fg command

3.4. Terminate process execution

3.4.1.Ctrl+C

3.4.2.kill, kill commands

3.4.3.pkill command

4, at one time task settings

5, crontab recurring task settings

1, Relationship between programs and processes and threads

(1) Program: binary file, static.

(2) Process: it is the process of program running, dynamic, with life cycle and running state.

(3) 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.

(4) Summary: multiple threads can be concurrent in a process. Each thread can execute different tasks in parallel, and the processes we see in the task manager are the applications we download

Of course, an application can also contain multiple processes.

2, View process

2.1. View static process statistics ps command

2.1.1. Method 1: ps -aux

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 dominated format.
-x: Displays the process information of the current user under all terminals.

(1) 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?
Briefly: 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: stopped 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 and program execution generally 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 being detected or stopped;
Z: The program should have been terminated, but its parent program can't normally terminate it, resulting in the state D of zombie program, which can't be interrupted.
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.

(2) Case list

Example 1:

View process status[ root@benet ~]#VIM a.txt is executed at another terminal:
[ root@benet ~]#PS - aux | grep a.txt # viewing status S indicates sleep status, + indicates foreground
root    4435 0.0   0.2 151752    5292 pts/1    S+    20:52    0:00 vim a.txt
root    4661 0.0   0.0 112676    996 pts/0    S+    21:05    0:00 grep --color=auto a.txt
Press ctrl+z [1] + on the vim a.txt terminal to stop vim a.txt executing on another terminal:
[ root@benet ~]#PS - aux | grep a.txt # view status T indicates stop status
root    4435 0.0   0.2 151752    5292 pts/1    T    20:52    0:00 vim a.txt
root    4675 0.0   0.0 112676    996 pts/0    S+    21:05    0:00 grep --color=auto a.txt
Note:
ctrl-c sends a SIGINT signal to terminate a process.
ctrl-z sends SIGSTOP signal to suspend a process. Place the job in the background (pause).
ctrl-d does not send a signal, but represents a special binary value, EOF. Represents input completion or logout.

Example 2:

D non interruptible state
[root@xuegod63 ~]# tar -zcvf usr-tar.gz /usr/
#Then continuously check the status at the other terminal, changing from S +, R + to D +.

2.1.2. Method 2: PS Elf

ps -elf        ###The process information in the system is displayed in long format.

-e:Display all process information in the system.
-l:Displays process information in long format.
-f:Displays process information in a complete 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.
WCHAN: if the process is sleeping, the system function name in sleep will be displayed.
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; tty1tty7 represents the local console terminal
TIME: the cumulative CPU TIME required to run the process.
CMD: start command of the 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.2. View dynamic process ranking information top command

top Shortcut keys of command full screen operation interface:

Default 3 s Refresh once, press s Modify the refresh time by space: refresh immediately.
P: Press CPU Sort.
M: Sort by memory.
T: Sort by time.
N: The key sorts according to the start time.
Numeric key 1: displays the of each kernel CPU Utilization rate.
Press u,Re enter user: specify the displayed user.
h: Available top Online help information for the program.
q: Key can exit normally top Procedure.

Note: if passed top Ranking tool finds a process CPU The occupancy rate is very high. You need to terminate the process. You can top Press in the operation interface k Key, and then it will appear above the list“ PID to signal/kill [default pid = 3180]:" Enter the information of the specified process according to the prompt PID Number and press Enter key, Appear“ Send pid 5597 signal [15/sigterm]"The prompt message for the second confirmation of the, and then press Enter Press OK to terminate the corresponding process.

 

2.3. View process information pgrep

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.4. View process information pstree

Lists process information in a tree structure

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"The command can view the process tree of the current system, including the corresponding processes 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 zfx

3, Control process

3.1. Start process manually

(1) Foreground start: the user enters a command and directly executes the program.

(2) Background startup: add the "&" symbol at the end of the command line.

cp /dev/cdrom /home/cetos7.iso &

3.2. Dispatch start

(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 processes

3.3.1Ctrl+Z combination key

Suspend the current process, that is, call it into the background and stop execution

3.3.2. Viewing a list of tasks in the background: jobs command

Common parameters

-l: Option to display the corresponding of the process at the same time PID number.

3.3.3. Restore the background process to the foreground run: fg command

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, perform the following“ fg 1"Command to suspend a previously suspended to the background wget The process is transferred 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. Terminate process execution

3.4.1.Ctrl+C

Interrupt executing command

3.4.2.kill, kill commands

(1) kill is used to terminate the process with the specified PID number.

(2) Kill is used to terminate all processes with the specified name.

(3) The - 9 option is used to force termination

(4) Case list:

[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 processes that cannot be combined normally, they can be terminated when necessary“-9"Option to force termination.
For example, the following operation shows forced termination 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 is 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 		     // Successfully terminated VIM process [1] + 2993 killed 	 vim tmpfile

3.4.3.pkill command

(1) Terminate the corresponding process according to specific conditions.

(2) Common command options

-U: Terminate the corresponding process according to the user name of the process.

-t: Terminate the corresponding process according to the terminal where the process is located.

(3) Case list

su - zfx
vim tst.txt &
[root@localhost ~]# pgrep –l -U "zfx" 	// Confirm target process related information 3773 bash
[root@localhost ~]# pkill -9 -U "zfx" 	// Forcibly terminate the process of user hackli
[root@localhost ~]# pgrep -l -U "zfx" 	// Confirm that the target process has been terminated

4, at one time task settings

(1) At command: schedule tasks at once

at [HH:MM] [yyyy-mm-dd]

(2) list of cases

① Automatically turn off the current system at 12:30 of the day

[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 //After setting the task, press Ctrl + D to submit
at> <EOT><EOT>
job 1 at Mon Aug 16 16:23:00 2021

[root@localhost ~]# cat /tmp/ps.root 	// After the scheduled time has elapsed, verify the command result 202

(2) 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 settings

(1) The crontab command is used to manage users' planned tasks and set users' periodic planned task list. Different planned tasks can be completed with different options

Manage operations.

Common options are as follows:

-e: Edit the 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 displays scheduled tasks.

-r: Delete the scheduled task list.

Use of crontab command related 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

(2) Configuration format:

(3) special expression method of event value

* Represents any time within the range.
,Represents multiple discrete time points of an interval.
- Represents a continuous time range.
/ Specifies 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

(4) Case list:

① Start sshd service automatically at 7:50 every morning and close sshd service at 22:50 every morning;

② Clear the data in the public directory / var/ftp/pub of the FTP server every five days;

③ Restart the httpd service in the system at 7:30 every Saturday;

④ 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;

[root@localhost ~]# crontab -e
①
50 7 * * *  /usr/bin/systemctl start sshd.service 
50 22 * * * /usr/bin/systemctl stop sshd.servic 

②
0 0 */5 * * /usr/bin/rm -rf /var/ftp/pub/*

③
30 7 * * 6  /usr/bin/systemctl httpd restart

④
30 17 * * 1,3,5 /usr/bin/tar jcf httpdconf.tar.bz2 /etc/httpd/

6, Summary

(1) Commands for viewing processes (ps, top, pgrep, pstree)

(2) Process control (start process, schedule process, terminate process)

(3) The at command sets the scheduled task

(4) Configuration fields for crontab scheduled tasks

Keywords: Linux

Added by pherrick on Mon, 03 Jan 2022 05:49:04 +0200