Linux learning
linux Process
Process concept
From the perspective of users: a process is a running program.
From the perspective of operating system: when the operating system runs a program, it is necessary to describe the running process of the program through a structural task_struct {}, collectively referred to as PCB, so for the operating system, the process is the PCB(process control block) program control block
The process description information includes: identifier PID, process status, priority, program counter, context data, memory pointer, IO status information and accounting information. All need to be scheduled by the operating system.
Displays the processes executed by the system
Display the process executed by the system: ps -a # Display all process information of the current terminal ps -u # Display process information in user's format ps -x # Displays the parameters of the background process
Instruction description
instructions | meaning |
---|---|
System V | Display style |
USER | User name |
PID | Process number |
%CPU | CPU usage |
%MEM | Percentage of physical memory used |
VSZ | Occupied virtual memory size |
RSS | Occupied physical memory size |
TT | Terminal name, abbreviation |
STAT | Process status, where s-sleep, s-indicates that the process is the leader of the session, N-indicates that the process has a lower priority than the normal priority, R-running, D-short-term waiting, Z-dead process, T-tracked or stopped, etc |
STARTED | Start time of the process |
TIME | CPU time used by the process |
COMMAND | The instructions and parameters used to start the process will be truncated and displayed if they are too long |
Displays the current process
Display the current process in full format: ps -ef # Displays the current process in full format ps -e # Show current process ps -f # Full format
Instruction description
instructions | meaning |
---|---|
UID | User id |
PID | Process id |
PPID | Parent process id |
C | The CPU is used to calculate the execution priority. A higher value indicates that the process is CPU intensive, and the execution priority will be reduced |
STIME | Time of system startup |
TTY | Complete terminal name |
TIME | CPU time used by the process |
CMD | The instructions and parameters used to start the process |
Terminate process
Display the current process in full format: kill [option] Process number # Kill process by process number killall Process name # Kill the process through the process name. Wildcards are supported kill/killall -9 # Force immediate kill of process
Process tree
Display process information in tree format: pstree [option] # Display the process information in tree format. If the command cannot be found, use yum install psmisc to download it pstree -p # Display process number pstree -u # Display user name
Service management
The essence of a service is a process, but when running in the background, it usually listens to a port and waits for requests from other programs, such as mysqld and sshd. Therefore, it is also called a daemon.
service management instruction
After CentOS 7, many services no longer support service, but systemctl
The service managed by the service instruction is in / etc / init D view
service service name [option] # Options include start, stop, restart, and status
Operation level of service
linux services have seven levels of operation
// Run level 0: Shut down 1: single user [Retrieve lost password] 2: There is no network service in multi-user state 3: Multi user network service status 4: The system is not used and reserved for users 5: Graphical interface 6: System restart // The common operation levels are 3 and 5. You can also specify the default operation level, which will be demonstrated later init level // Switch level command systemctl get-default //Get current level systemctl set-default //Set default level
Service Management (chkconfig)
The chkconfig command can be used to set the self start / shut-down for each running level of the service
After CentOS 7, many services no longer support chkconfig, but systemctl
The services managed by the chkconfig command are in / etc / init D view
chkconfig --list | grep [option] // View services chkconfig service name --list // View services chkconfig --level Run level service name on/off // Set whether the service starts automatically at this level
After using the chkconfig command to set the service, you need to reboot the machine to take effect
Service Management (systemctl)
The services managed by the systemctl command can be viewed in / user/lib/systemd/system
// Basic grammar systemctl [option] service name // Set the service information, such as start, stop, status, restart, and take effect temporarily // systemctl sets the self start state of the service systemctl list-unit-file [|grep service name] // Check the service startup status and grep filter systemctl enable service name // Set service startup systemctl disable service name // Turn off service and start up systemctl is-enabled service name // Query whether a service starts automatically
Service management port operation (firewall)
Open or close the specified port
netstat -anp | more // View port information // firewall instruction firewall-cmd --permanent --add-port=port/agreement // open port firewall-cmd --permanent --remove-port=port/agreement // Close port firewall-cmd --reload // Reload to take effect firewall-cmd --query-port=port/agreement // Check whether the port is open
Dynamic monitoring system
The top instruction is very similar to the ps instruction. They are both used to display the executing process. The difference is that top can update the executing process after it is executed for a period of time.
Basic syntax:
top [options]
Option description
option | meaning |
---|---|
-d seconds | Specify the top instruction, which is updated every few seconds. The default is 3 seconds |
-i | Make top not show any idle or dead processes |
-p | Monitor the status of a process by specifying the monitoring process id |
Interactive operation instructions:
After using the top instruction to view the process, you can use the interactive instruction to operate
option | meaning |
---|---|
P | Sort by cpu usage. The default is this sort |
M | Sort by memory usage |
N | Sort by PID |
U u ser 1 | Monitor the progress of user 1 |
k | Terminate process |
q | Exit top |
View network conditions
Basic grammar
netstat [option]
Option description
option | meaning |
---|---|
-an | Arrange the output in a certain order |
-p | Displays which process is calling |
Software package management
rpm
RPM is the abbreviation of red hat package manager. Although the name of this file format is marked with the logo of red hat, its original design concept is open, including the distribution versions of Linux such as OpenLinux, S.u.S.E. and Turbo Linux, which can be regarded as a recognized industry standard.
// Basic grammar rpm -qa // Query all installed rpm packages. You can use more paging and grep filtering to view them rpm -q Package name // Query whether the software package is installed rpm -qi Package name // Query package information rpm -ql Package name // Query the files in the package rpm -qf File full pathname // Query the package to which the file belongs rpm -e Package name // Uninstall package rpm -e --nodeps Package name // Force uninstall package rpm -ivh Package full path name // Install rmp package // Where i=install, v=verbose prompt, h=hash progress bar
yum
Yum (full name: Yellow dog Updater, Modified) is a Shell front-end package manager in Fedora, RedHat and CentOS. Based on RPM package management, it can automatically download and install RPM packages from the specified server, automatically handle dependencies, and install all dependent software packages at one time without cumbersome downloading and installation at one time.
// Basic grammar yum list | grep Software name // Query the yum server for the required installed software yum install Software name // Install the specified software
shell programming
Bash is one of the most popular shell scripting languages in the Linux world. I think (this is my own view) the reason is that by default, the bash shell allows users to easily navigate through historical commands (previously executed), while ksh requires Make some adjustments to the profile, or remember some "magic" key combinations to review the history and correct the commands.
shell scripts are also like interpreters, but they are often used to call externally compiled programs. It then captures the output, exits the code, and processes it as appropriate.
How shell scripts are executed
Script format requirements
The script should be #/ Start with bin/bash
The script needs to have executable permissions
Examples
#!/bin/bash ehco "hello" ehco 'hello' ehco hello
Execution method:
Directly under the script directory/ The file name can be executed, but you need to have execution permission
The full path of sh file can also be executed
shell variable
shell variable introduction:
1.linux is divided into system variables and user-defined variables
2. System variables: $HOME,$PWD,$USER,$SHELL, etc
3. Display all variables of the current system: set
Definition of shell variable:
Define variable: variable name = value
Undo variable: unset variable name
Declare static variables: readonly variables. Static variables cannot be undone
Definition rules of variables:
Variables can consist of numbers, letters, and underscores, but cannot begin with a number.
There must be no spaces on either side of the equal sign
Variable names are generally capitalized
Assign the return value of the command to the variable:
A=`date ` / / in backquotes, run the command inside and assign the return value to a
A=$(date) / / run the command inside and assign the return value to a
environment variable
// Basic grammar export Variable name=Variable value // Output as environment variable source configuration file // Make the modified configuration information take effect immediately echo $Variable name // Output variable // Multiline comments for shell scripts << ! // A comment must start with another line Note Content ! // There is another line at the end
Position parameter variable
When we execute a shell script, we need to obtain the parameter information of the command line, so we can use the location parameter variable.
For example:/ shell-test.sh 1 2, which is a command line for executing shell script. You can use positional parameter variables to obtain parameter information in the script
// Basic grammar $n // n is a number, $0 represents the command itself, $1 to $9 represent the following 9 parameters in turn, and more than the 10th parameter needs to be contained in braces, such as $* // Represents all parameters in the command line, and regards all parameters as a whole $@ // Represents all parameters in the command line. Treat each parameter differently $# //This command represents the number of all parameters on the command line
Predefined variables
The designed variables can be used directly in the shell script
// Basic grammar $$ // Get the id of the current process $! // Get the id of the last process in the background $? // Get the return status of the last command. If the value is 0, it will run correctly, and if it is not 0, it will fail command & // Use the & symbol in the script to indicate that the command is running in the background
operator
Operation in shell
// Basic grammar 1.$((expression))perhaps $[expression]perhaps expr expression // expression 2.be careful expr There should be spaces between operators. You can use``Assign the return value to the variable 3.expr Operator in the expression of\*,/,%Indicates multiplication and division
Process judgment
// Basic grammar // First: if [ Conditional judgment ];then program fi // Second: if [ Conditional judgment ] then program elif [ Conditional judgment ] then program fi // Notes: (1) there must be a space between the brackets and the conditional expression; (2) if the conditional expression is not empty, return true // Branch condition judgment // And // Mode 1 if [ c1 -a c2 ]; then ... fi // Mode 1 if [ c1 ] && [ c2 ]; then ... fi // or // Mode 1 if [ c1 -o c2 ]; then ... fi // Mode 2 if [ c1 ] || [ c2 ]; then ... fi
Common condition judgment
option | meaning |
---|---|
= | Determine whether the string is the same |
-lt | less than |
-le | Less than or equal to |
-eq | be equal to |
-gt | greater than |
-ge | Greater than or equal to |
-ne | Not equal to |
-le | Less than or equal to |
-eq | be equal to |
-gt | greater than |
-ge | Greater than or equal to |
-r | Do you have the habit of reading documents |
-x | Do you have the habit of executing documents |
-w | Do you have the habit of writing documents |
-f | Whether the file exists and is a formal file |
-e | Does the file exist |
-d | Does the file exist and is a directory |
case in process control
#! /bin/bash case $1 in [a-z] | [A-Z]) echo "English characters" ;; [0-9]) echo "number" ;; [,.!?]) echo "Symbol" ;; *) echo "I don't know" esac
for process control
for((i=0;i<=10;i++)) do S=$(($S+i)) done echo $S
while process control
for((i=0;i<=10;i++)) do S=$(($S+i)) done echo $S
shell Function
Read read console input
//Basic grammar read (Options) (parameters) //Examples #!/bin/bash read -t 5 -p "Please enter the first number:" NUM1 read -t 5 -p "Please enter the second number:" NUM2 SUM=$(($NUM1+$NUM2)) // Assign SUM of two numbers to SUM echo "The sum of the two numbers is:$SUM"
option | meaning |
---|---|
-p | Specifies the indicator when reading |
-t | Specifies the read wait time |
System function
dirname basic syntax:
Returns the path of the absolute path file and the file name
Basic syntax of basename:
Returns the file name of the absolute path, including the extended name
Custom function
Basic grammar
function name() {
Function body
}
name parameter / / call the function
// Examples #!/bin/bash # Calculate the sum of two numbers function getSum() { SUM=$(($1+$2)) echo "And are: $SUM" } getSum $1 $2 // Call function
Backup database
// Example of backing up mysql database #Directory where backup data is stored BACKUP=/data/backup/db/ #current time DATETIME=$(date +%Y-%m-%d_%H%M%S) echo $DATETIME #Database address, user name, password, backed up database HOST=localhost USER=root PWD=123456fls DATABASE=Students #Create backup folder if [ ! -d ${BACKUP}/${DATETIME} ] then mkdir -p "${BACKUP}/${DATETIME}" echo "The backup file is created as follows: ${BACKUP}/${DATETIME}" else echo "The backup file is: ${BACKUP}/${DATETIME}" fi #Backup database mysqldump -u$USER -p$PWD -h${HOST} -q -R -B ${DATABASE} | gzip > ${BACKUP}/${DATETIME}/$DATETIME.sql.gz # Package the backup folder into a tar file cd ${BACKUP} tar -zcvf ${DATETIME}.tar.gz ${DATETIME} # Delete corresponding directory rm -rf ${BACKUP}/${DATETIME} # Delete the files backed up 3 minutes ago, - atime 0 indicates the files accessed within 24 hours, - exec indicates the execution of the following commands, and the slash is prevented; No. escape find $BACKUP -atime 0 -name "*.tar.gz" -exec rm -rf {} \;