1. Process management

1.1 process

  • A process is a program or command that is executing. Each process is a running entity that has its own address space and occupies a certain amount of system resources
  • A process can be understood as a program that is executing

1.2 Process Management Overview

  • Check the status of the server
  • View all processes in the system
  • Kill a process, only if it cannot be shut down

1.3 Viewing Process Commands (ps)

  • ps auxView all processes in the system in BSD format
  • ps -leView all processes in the system in Linux standard format
  • TTY is an abbreviation for TeleType, which used to refer to a TeleType, which used a printer keyboard to read and send messages over a serial line
  • PTS (pseudo[‘su:doʊ]-terminal slave) is a so-called pseudo-terminal or virtual terminal

1.3.1 options

parameter meaning
-a Displays all processes of a terminal
-u The user name and memory usage of a process are displayed
-x Displays processes that do not control terminals
-l More detailed information is displayed in long format

1.3.2 Result Field Meaning

data meaning
USER Which user created the process
PID ID of the process
%CPU Indicates the percentage of CPU resources occupied by the process. The higher the CPU usage, the more system resources are consumed
%MEM Percentage of the physical memory occupied by the process. The higher the memory usage, the more system resources are consumed
VSZ Percentage of virtual memory occupied by the process, in kilobytes
RSS This process occupies the actual physical memory size, in KB
TTY The terminal on which the process runs. tty1Tty7 indicates the local control terminal, TTy1Tty6 is the character terminal and TTy7 is the graphics terminal. PTS /0 to 255 represent virtual terminals,? Indicates that the terminal is started by the system
STAT Process status
START Start time of the process
TIME This process consumes CPU computing time. A higher value indicates a higher consumption of system resources
COMMAND The name of the command that generated this process

1.3.3 Process Status (STAT)

parameter meaning
R(Runing) run
S(Sleep) dormancy
T(Terminated) stop
S(Son) Include child process
+ In the background

1.4 pstree

  • Pstree [option]
    • -p Displays the PROCESS PID
    • -u Displays the user who owns the process

1.5 Viewing Processes (Top)

  • top
top -b -n 1 > top.txt
Copy the code

1.5.1 options

options meaning
-b Output using batch mode, general and-nTogether with
-n Number of times, specifies the number of times the top command is executed. In general the-bCombination of options
-d Number of seconds, specifying that the top command is updated every few seconds. The default is 3 seconds

1.5.2 Commands in Interactive Mode

options meaning
? Or h Display help for interaction mode
P Sort by CPU usage, which is the default
M Sort by memory usage
N Sorting by PID
q Out of the top

1.5.3 status bar

1.5.3.1 Task queue information of the first action
content instructions
12:12:12 Current system time
up 1 day 5:33 System running time, the machine has been running for 1 day, 5 hours and 33 minutes
2 users Currently, two clients are logged in
load average 0 0 0 Average system load of the previous 1, 5, and 15 minutes. It is generally considered that less than 1 hour the load is small, and greater than 1 exceeds the load
1.5.3.2 Second process information
content instructions
Tasks: 100 total The total number of processes in the system
1 running Number of running processes
94 sleeping Sleep progression
0 stopped The process being stopped
0 zombie Zombie process. Check if it’s not zero
1.5.3.3 CPU information of the third behavior
content instructions
The Cpu (s) : 0.1% of the us CPU usage in user mode
0.1% sy CPU usage in system mode
0.0% ni The percentage of CPU occupied by user processes that changed priority
99.7% id CPU percentage of idle CPU
0.1% wa Percentage of CPU occupied by processes waiting for input/output
0.1% of hi Percentage of CPU occupied by hard interrupt request services
0.1% si Percentage of CPU used by soft interrupt request services
0.0% of st Steal Time (ST) Indicates the percentage of virtual time when there is
1.5.3.4 Behavior 4 Physical memory information
content instructions
Mem: 1030720k total Physical memory problems in kilobytes
551860k used Amount of physical memory that has been used
478860k free Number of free physical memory: The VM allocates 1024 MB of memory, uses 538 MB of memory, and is free 467 MB of memory
43180k buffers The amount of memory used as a buffer to store data that needs to be written to the hard disk to speed up data writing
1.5.3.5 Behavior 4 swaps partition information
content instructions
Swap: 2047992k total Total swap partition (virtual memory) size
536k used The size of the swap partition in use
2047456k free Size of the free swap partition
368164k cached Data that needs to be read frequently is read from the hard disk to the memory, which speeds up data reading

1.6 Killing a Process (kill)

1.6.1 Process signals

  • kill -lView available process signals
The signal code The signal name instructions The sample
1 SIGHUP This signal causes the process to shut down immediately, then override to read the configuration file and restart, smoothing the restart Kill -1-hup Indicates the process ID
2 SIGINT Program termination signal, used to close foreground processes, equivalent to CTRL + C
9 SIGKILL Used to terminate a program immediately. This signal cannot be blocked, processed, or ignored
15 SIGTERM Signal to end a process normally, default signal of the kill command. The SIGKILL signal is only attempted if it cannot be aborted properly

1.6.2 Killing a Process

sleep.sh

#!/bin/bash
i=0
while [ $i -le 1000 ]
 doEcho $(date) 1 s done sh sleep sleep. Sh & ps - ef | grep sleep. Sh kill the processCopy the code

2. View system resources

2.1 the vmstat

  • Monitor system resource usage
  • Vmstat [Refresh delay number of times]
vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0    532 329932  99388 459768    0    0    16    81   59   50  3  1 96  0  0    
Copy the code

2.1.1 (Procs) Process information field

classification parameter meaning
procs r The larger the number of processes waiting to run, the busier the system
procs b The number of processes that cannot be wakened. The larger the number, the busier the system

2.1.2 Memory (Memory Information Field)

classification parameter meaning
memory swpd Size of the Swap space used, in KB
memory free Free memory capacity (unit: KB
memory buff Memory capacity of the buffer, in KB
memory cache Memory capacity of the cache, in KB

2.1.3 Swap (Swap Partition Information)

  • If the higher si and SO numbers indicate that data is frequently exchanged between disk and memory, the worse the system performance will be
classification parameter meaning
swap si(in) The amount of data exchanged from disk to memory, in kilobytes
swap so(out) The amount of data exchanged from memory to hard disk, in kilobytes

2.1.4 I/O (Disk Read and Write)

  • The larger the number of BI and BO is, the busier the DISK I/O is
classification parameter meaning
io bi(in) A problem reading data from a block device in blocks
io bo(out) The amount of data written to a block device, in blocks

2.1.5 System (System Information Field)

  • The larger the number of IN and CS is, the busier the communication between the system and the interface device is
classification parameter meaning
system in(interrupt) Number of interrupted processes per second
system cs(switch) Number of event switches per second

2.1.6 CPU(CPU information field)

classification parameter meaning
CPU us(user) Percentage of CPU elapsed time consumed by non-kernel processes
CPU sy(system) The percentage of CPU processing time consumed by the kernel process
CPU id(idea) Percentage of idle CPU
CPU wa(wait) Percentage of CPU consumed waiting for I/O
CPU st(steal) Percentage of cpus stolen by virtual machines

2.3 free

  • Check memory usage
  • free [-b|-k|-m|-g]
  • options
    • -b Is expressed in bytes
    • -k Indicates the unit of KB bytes
    • -m The unit is MB bytes
    • -g The unit is GB bytes
# free -m
             total       used       free     shared    buffers     cached
Mem:          1006        687        319          0         98        449
-/+ buffers/cache:        139        866
Swap:         1999          0       1999
Copy the code

2.3.1 the first line

classification parameter meaning
total The total number of memory
used Amount of memory that has been used
free The amount of free memory
shared The amount of memory shared by multiple processes
buffers Buffer memory
cached Number of cached memory

The second line 2.3.2

parameter algorithm meaning
– buffers/cache Used-buffers-cached in the first line Used to subtract the amount of cache and buffer memory
+ buffers/cache Free +buffers+cached in the first line Free plus the amount of cache and buffer memory

2.3.3 the third line

classification parameter meaning
total The default unit is K
used Number of swaps in use. The default unit is K
free The default unit is K

2.6 Viewing Kernel Information

  • uname
# uname -a
Linux localhost 2.632.-279.el6.i686 #1 SMP Fri Jun 22 10:59:55 UTC 2012 i686 i686 i386 GNU/Linux
# uname -s
Linux
# uname -r
2.632.-279.el6.i686
Copy the code

2.7 Viewing the Bits of the OPERATING system

file /bin/ls
Copy the code

2.8 Viewing release Versions

lsb_release -a
Copy the code

2.9 Viewing Release Versions

yum install redhat-lsb -y
lsb_release -v
Copy the code

3. System scheduled tasks

  • Some tasks, such as backing up databases, need to be performed when the system is idle

3.2 crontab

  • You can periodically execute scheduled tasks
  • cron
systemctl restart crond.service
Copy the code

3.2.3 crontab set

  • Crontab [option]
  • options
    • -e Edits scheduled crontab tasks
    • -l Queries crontab tasks
    • -r Deletes all crontab tasks of the current user
* * * * * Perform the taskCopy the code
3.2.3.1 grammar
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
Copy the code
3.2.3.2 position
project meaning The scope of
The first star The minute of an hour 0 ~ 59
The second star The hour of a day 0 ~ 23
The third star The day in the middle of January 1 to 31
A fourth star Month in a year 1 ~ 12
5 star Day of the week 0 ~ 6
3.2.3.3 Special Symbols
symbol meaning example
* Represents any time For example, the first star would be executed every minute for an hour
. Stands for discontinuous time For example, “1,2,3 “means 1 minute,2 minutes,3 minutes per hour
Represents a continuous time range For example, “1-5 ** **” indicates that the command is executed from the first minute to the fifth hour
*/n Represents how often it is executed Such as”/ 10“That means executing an order every 10 minutes
0, 0,1,10 times 1 On the 1st and 10th of each month, it will be executed at 00:00 on the 1st of each week
3.2.3.4 case
symbol meaning
10 * 22 It is executed at 22:10 every day
15 1 0 Run at 15:0 on 1 of a week
0 5 5, 10 5am on the 5th and 10th of each month
10 5 1-5 Orders are executed Monday to Friday at 5:10 a.m
/ 10 10 Every day at 10 am, every 10 minutes
3.2.3.5 Precautions
  • All options cannot be blank and must be filled in
  • The minimum unit of crontab is minute, and the maximum unit is day
  • Use absolute paths when writing commands or scripts

3.3 System Scheduled Tasks

  • crontab -eThe commands are executed by users. Different users can execute their own scheduled tasks
  • If the system needs to perform scheduled tasks, you can edit them/etc/crontabfile
  • /etc/crontabYou can specify the shell, path, mail send, and home directory

3.3.1 the/etc/crontab

  • Modify the/etc/crontabThe configuration file
5 5 * * * echo `date` >> /root/date.log
Copy the code

4. Combat missions

4.1 monitor nginx

nginx.sh

#!/bin/bash
local nginx
nginx=`ps -ef |grep nginx|grep -v grep|wc -l`
 if [ $nginx -gt 2]; then echo"your nginx is running"
    exit 0
 else
    /bin/systemctl start nginx.service
    exit 1
fi
Copy the code

4.2 Monitoring mysql

mysql.sh

#!/bin/sh
PortNum=`netstat -lnt|grep 3306|wc -l`
if [ $PortNum -eq 1 ]
then
 echo "mysqld is running."
else
 echo "mysqld is stoped."
fi
Copy the code

4.3 mysql Backup Scripts

mysql_backup.sh

#!/bin/bash
DATE=$(date +%F_%H-%M-%S)
HOST=127.0. 01.
DB=test
USER=root
PASS=abcd1#EFG
MAIL="[email protected]"
BACKUP_DIR=/data/db_backup
if [ ! -d "$BACKUP_DIR"]; then mkdir -p $BACKUP_DIR fi SQL_FILE=${DB}_FULL_$DATE.sql BAK_FILE=${DB}_FULL_$DATE.zip cd $BACKUP_DIRif mysqldump -h$HOST -u$USER -p$PASS -B $DB > $SQL_FILE; then
  zip $BAK_FILE $SQL_FILE && rm -rf $SQL_FILE
  if [ ! -s $BAK_FILE ]; then
    echo "$DATE backup failed" | mail -s "Backup failed" $MAIL
  fi
else
  echo "$DATE backup failed" | mail -s "Backup failed" $MAIL
fi
find $BACKUP_DIR -name '*.zip' -ctime +14 -exec rm {} \;
Copy the code