Process and Scheduled Task Management

Preface

Programs and Processes

program

1. Executable code and data stored in hard disk, shutdown, etc.
2. Statically saved code
3. Binary files, 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 running a program, with dynamic life cycle and running state

thread

1. It is the smallest unit that the operating system can operate and schedule, is included in the process, and is the actual unit of operation in the process.
2. A process has at least one execution thread
3. Threads are also known as lightweight processes
4. Threads are part of the process

Relationships among Programs, Processes, Threads

Threads are included in the process and are the actual units of operation in the process. Threads are also the basic units of the operating system that can run independently, also known as lightweight processes.
Multiple threads can concurrently execute different tasks in one process, and the processes we see in Task Manager are generated by the applications we download
Of course, an application can also contain multiple processes

View process

View process information ps

View static process statistics

ps-aux

[root@local]# ps -aux  
###Show process letters in a simple list

Common parameters:
-a: Displays all process information under the current terminal, including processes of other users. And " x"Options combine to display all process information in the system
-u: Output process information in a user-centric format
-x: Displays process information for the current user under all terminals

In the output information above, the first behavior is the list title, where the meanings of the fields are described below

USER: 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: The amount of virtual memory (KB) used by the process

RSS: The amount of physical memory occupied by the process (KB)

TTY: Indicates on which terminal the process is running. Processes that are not started from the terminal display as?
Briefly: pts is a remote login terminal ctrl+ALT F1-F6 tty1 image interface 2 and 6 character interface? Processes executed by the system itself

STAT: The state of the process (D: uninterruptable sleep state: R: running state; S: dormant state, can be awakened; T: stopped state, may be paused in the background or the process is in tracking debugging state: Z: zombie process, the process has been aborted, but some programs are still in memory)

<: Indicates that the process is running at a high priority

N: Indicates that the process is running at a low priority

L: 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, program execution stops normally

START: When the process was started

TIME: CPU time consumed by the process

COMMAND: Name of the command that started the process

Common combinations of options in ps-aux

D: System Daemon

T: Style, program execution stops normally

R: The program is currently running or can be run

S: The program is currently sleeping (idle!). But it can be waked up by some signals. T: The program is currently being detected or stopped

Z: The program should have terminated, but its parent program could not terminate him properly, causing the zombie program state D to be uninterruptible

ps -elf

[root@local]# ps -elf   
###Process information in the system will be displayed in a long format

Common parameters:
-e:Show all process information in the system
-1:Show process information in long format
-f:Display process information in full format

Explanation of columns
F: System tag assigned by the kernel to the process.

S: Status of the process

UID: User who started these processes

PID: Process ID of the process

PPID: Process number of the parent process (if the process was started by another process)

C: CPU utilization in the process life cycle

PRI: Priority of the process (the larger the number represents the lower priority)

NI: The value of modesty is used to participate in determining priority

ADDR: Memory address of the process

SZ: Approximate size of swap space required if the process is swapped out

WCHAN: Displays the name of the system function in sleep if the process is sleeping

STIME: System time at process startup

TTY: Terminal device at the time the process starts. pts/0255 represents a virtual terminal, typically a remotely connected terminal; tty1 tty7 represents the local console terminal

TIME: Cumulative CPU time required to run the process

CMD: Process Start Command

Zombie
A process is finished, but if the parent process of the process has ended first, it will not become a zombie process, because when each process ends, the system scans all processes running in the current system to see if any process is a child of the process that just ended, and if so, init takes over it. Become its parent process, and init reclaims the resources it consumes when the child process exits. But when a child process ends before the parent process, and the parent process does not recycle the child process, freeing up the resources occupied by the child process, the child process becomes a zombie process.

View process dynamics

[root@localhost ~]# top  ###Process information in the system will be displayed in a dynamic format

top Shortcuts for command full screen operation interface:
Default 3 s Refresh once, press s Modify refresh time by space: refresh immediately
P: Press CPU sort
M: Sort by memory
T: Sort by time
p: process IP,View the status of a process
N: Keys are sorted by startup time
 Number Key 1: Display the CPU Usage u/U: Specify the user to display
h:Available top Online help information for programs
q:Key can exit normally top program


Among the above output information, the first part shows summary information such as system tasks (Tasks), CPU usage, memory usage (Mem), swap space (Swap), and so on. Under the summary information, the ranking of the current process is displayed in turn. The meaning of the relevant information is described below

uptime
13:22:30
current time

up 20days
System runtime, indicating that this server has been running for 20 days

2 user
Current number of logged-in users

load average: 0.06, 0.60, 0.48
System load, which is the average length of the task queue. The three values are 1 minute, 5 minutes, 15 minutes ago and now average

System Tasks information;
Total, total number of processes;
Running, the number of running processes;
sleeping, the number of processes that are dormant;
stopped, the number of processes aborted;
zombie, number of deadly unresponsive processes

CPU usage information:
us, user occupancy;
sy, kernel occupancy;
ni, priority scheduling occupancy;
id, idle CPU;
wa, I/O waiting for occupation;
hi, hardware interrupt occupancy;
si, software interrupt occupancy;
st, virtualization usage. To understand the percentage of CPU idle, look primarily at the%id section

Memory occupancy (Mem) information;
Total, total memory space;
Free, free memory;
Used, used memory;
buff/cache, the sum of buffers of physical and swap memory.

Swap space usage
Total, total swap space;
Free, free swap space;
Used, used swap space;
avail Mem, available physical space.

Detailed description:
Tasks: Total 481 total processes
Number of processes running 1 running
480 sleeping Processes
Number of processes stopped by 0 stopped
Number of 0 zombie zombie processes
Cpu(s): 0.0% us Percentage of CPU used by system user processes
Percentage of CPU occupied by processes in 0.0% sy kernel Priority scheduling occupancy
Percentage of CPU occupied by processes that changed priority within 0.0% ni user process space
98.7% id idle CPU percentage
Total time 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 Hard interrupt Time-consuming Hard interrupt occupancy
Hard interrupt, percentage of CPU
0.0% si Soft interrupt consumes time Software interrupt occupancy
Soft interrupt, percentage of CPU
0.0 st (steal) virtualization occupation. To understand the percentage of CPU idle, look primarily at the%id section
st: virtual machines steal physical time
Mem: 3861516k total physical memory
Total physical memory used by 710488k user
Total free memory 1412956k
The amount of memory 1738072k buff/cache used as the kernel cache. And free-k
Swap: Total 3071996k Exchange Area
Total exchange area used by 0k user
Total 3071996k free free exchange area
What is the total available memory for 2825600 avail Mem

Line 7 Process Information
PID process id

User name of USER process owner

PR priority (dynamically adjusted by the kernel), users cannot

NI process priority. nice value. Negative values indicate high priority, positive values indicate low priority, users can adjust themselves

VIRT (virtual memory usage) Virtual memory is all memory (ps marked VSZ) being used by the process VIRT:virtual memory usage Virtual memory
1. The "required" virtual memory size of the process, including libraries, code, data used by the process, etc.
2. If a process requests 100m of memory but actually uses only 10m, it will grow
100m, not actual usage

RES (resident memory usage) is the physical memory used by processes. Actual Utility Memory (RSS in ps) RES:resident memory usage resident memory
1. The current size of memory used by the process, excluding swap out
2. Sharing with other processes
3. If you request 100m memory, you will actually use 10m, which will only increase 10m, as opposed to VIRT
4. Regarding the memory occupied by libraries, 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 processes, the shared memory of other processes is also included.
2. Although the process only uses functions of several shared libraries, it contains the size of the entire shared library.
3. Formula for calculating the physical memory size of a process: RES-SHR 4, swap out, it will fall

S Process Status
D = Uninterruptible Sleep
R=Running or Running S=Sleeping
T=Tracked/Stopped Z=Zombie

Percentage of CPU time consumed by%CPU since last update

Percentage of physical memory used by the%MEM process

Total CPU time used by TIME+ processes in 1/100 seconds

COMMAND command name/command line

View process information pgrep

Query process PID information based on specific conditions

Common parameters
-l:Options output the corresponding process name and PID
-U: Options Query Processes for a Specific User
-t: Options Query Processes Running at a Specific Terminal

Example 1
pgrep -l "log"

Example 2
 pgrep -l -U teacher -t tty1

View process information pstree

List process information in a tree structure

The pstree command only displays the names of processes by default

Common parameters:
-p: Options can be listed together PID Number
-u: Options to list corresponding user names
-a: Options to list complete command information
 Perform the following " pstree -aup"The command can view the process tree of the current system.
Include the corresponding for each process PID Number, username, complete command, etc.
As you can see from the output, systemd The process is Linux The "ancestor" of all processes in the operating system
 
Example 1
pstree -aup lu

Control process

Manual startup

A user can start at least one process by manually entering a command or path to an executable program. Depending on whether the process needs to occupy the current command terminal, manual startup can be divided into foreground startup and background startup.
Foreground startup: Users enter commands and execute programs directly.

Background startup: add the'&'symbol at the end of the command line

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

Schedule Start

1. Use the at command to set up one-time scheduled tasks
2. Use the crontab command to set up periodically scheduled tasks

Front and Background Scheduling of Processes

Ctrl+z key combination
Suspend the current process, which calls it into the background and stops execution

jobs command
View the task list in the background

Common parameters
-l: Options can display the corresponding for this process at the same time PID Number

fg command

Resume background processes to foreground, specifying task number

With the bg (BackGround) command, tasks suspended in the background (such as hanging by pressing the Ctrl+Z key) can be resumed and operations continue in the background.

Use the fg command (ForeGround, foreground) to restore background tasks to foreground

The bg and fg commands need to specify the task number of the background process as a parameter unless there is only one task in the background.

For example, executing the following "fg 1" command can bring a previously suspended wget process back to the foreground for execution

[root@localhost ~]# jobs
[1]-   Stopped                 cp /dev/cdrom mycd.iso 
[2]+  Stopped                 top

[root@localhost ~]# fg 1

Terminate the running of a process

Ctrl+c key combination
Interrupting an executing command

Kill, kill all commands

  • kill is used to terminate the process with the specified PID number.
  • killall is used to terminate all processes with the specified name.
  • The -9 option is used to force termination
Terminate process using kill Command
[root@localhost ~]# pgrep -l "sshd"	 ###Query the PID number of the target process
5822 sshd

[root@localhost ~]# kill 5822	###Terminate process with specified PID

[root@localhost ~]# pgrep -l "sshd"	###Verify that the process has terminated (no results at query time)
Force termination with'-9'option
[root@localhost ~]# vim testfile  &	     ###Open vim program and suspend as 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		###Attempt to end process normally

[root@localhost ~]# jobs -l			###However, vim process was not found to have exited
[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
Terminate process using killall command
[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 process information to terminate
[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
Terminate process using pkill command

The pkill command can terminate a specific process based on the name of the process, the user running the process, the terminal on which the process resides, and many other properties. Most of the options are similar to the pgrep command, such as'-U'(named user),'-t' (specified terminal) and so on, which are very convenient to use. For example, to terminate a process initiated by user lu, including a login 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"	###Force termination of user hackli's process

[root@localhost ~]# pgrep -l -U "lu"	###Confirm that the target process has been terminated

One-time Task Settings

at command

One-time Scheduled Tasks

at [HH:MM] (time) [yyyy-mm-dd] (date, year, month, day)

Press Ctrl+D to submit when the task is set up

Example:

[root@kgc /]# date
2021 Monday, 16 08: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 the root user in the system at this point in time and save the value to the / tmp/ps.root file
at> <EOT>                                
###Press Ctrl+D to submit when the task is set up

job 1 at Mon Aug 16 16:23:00 2021

[root@localhost ~]# cat /tmp/ps.root	
###Verify command results after the scheduled time has elapsed

Delete at task with specified number

Use the atrm command. Deleted at tasks will not be executed and will not appear in the results of the atq command display, but tasks that have already been executed cannot be deleted. The specific operations are as follows:

[root@localhost ~]# atrm 2 //Delete Article 2 at Scheduled Task

[root@localhost ~]# atq //Confirm that Article 2 task has been deleted

crontab Periodic Task Configuration

Use the crontab command to manage the user's scheduled tasks. Setting the user's periodic scheduled task list is mainly done by the crontab command. Combining different options, different scheduled task management operations can be completed.

Common options:

-e: Edit Scheduled Task List
 
-u: Specifies which user the managed scheduled task belongs to, defaults to the current user (myself), generally only root Users have permission to use this option (for editing, deleting scheduled tasks for other users)
 
-l: List Displays Scheduled Tasks
 
-r: Delete Scheduled Task List

Use of crontab command options

crontab -u User name   ###cron service for specified XX users

crontab -l         ###List details of cron under current user

crontab -u zx -l   ###List details of cron for zx users

crontab -r         ###Delete cron content for all users, executed by normal users, and delete only their own cron content

crontab -r -u zx   ###Delete cron content for zx users

crontab -e         ###Edit cron service

Configuration Format

Special representations of event values

* :    Represents any time in the range

, :    Multiple discontinuous points of time representing intervals

- :    Represents a continuous time range

/ :    Time and frequency of specified interval

Example:

0 17 * * 1-5        Monday to Friday 17/day:00,
 
30 8 * * 1,3,5      Every Monday, Wednesday and Friday at 8:30
 
0 8-18/2 * * *      8 Every 2 hours from point to 18
 
0 * */3 * *         Every 3 days

for example

[root@localhost ~]# crontab -e 

50 7 * * *  /usr/bin/systemctl start sshd.service   
50 22 * * * /usr/bin/systemctl stop sshd.servic    ###Automatically start sshd service at 7:50 a.m. and shut down sshd service at 22:50 a.m.
 
0 0 */5 * * /usr/bin/rm -rf /var/ftp/pub/*    ###Empty data in FTP server public directory/var/ftp/pub every five days   
 
30 7 * * 6  /usr/bin/systemctl httpd restart     ###Restart the httpd service on your 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 Mondays, Wednesdays, and Fridays each week, the following scheduled tasks can be set by root users through crontab using the tar command to automatically back up the / etc/httpd directory

summary

  • Commands to view processes (ps, top, pgrpe, pstree)
  • Control (start process, schedule process, terminate process)
  • at command set scheduled task
  • Configuration fields for List itemcrontab empty task

Keywords: Linux

Added by white99 on Mon, 03 Jan 2022 13:54:23 +0200