Linux System Management

Source: Internet
Author: User
Tags arithmetic memory usage cpu usage pkill

Process Management view Process

To view a process:

ps auxps -le

To view the process tree:

pstree pstree -p // 查看每个进程的id号,会展开折叠进程pstree -u // 查看进程的用户

Top command:

    • Options
      • -D Number of seconds: Specifies that the top command is updated every few seconds. Default is 3 seconds
      • -B: Use batch mode output. General and "-N" options are shared
      • -N Count: Specifies the number of times the top command executes. General and "-B" options are shared

        top -b -n 1 > top.log   // 刷新一次i进程信息重定向到top.log日志文件中
    • Commands that can be executed in the interactive mode of the top command:
      • ? or h:x show Help for interactive mode
      • P: Sort by CPU usage, this is the default
      • M: Sort by Memory utilization
      • N: Sort by PID
      • Q: Exit Top

The first line of information is the task queue information:

content Description
12:26:46 System Current Time
Up 1 day, 13:32 System run time, the machine has been running 1 days 13 hours 32 minutes
2 users Two users are currently logged in
Load average:0.00, 0.00, 0.00 System in the previous 1 minutes, 5 minutes, 15 minutes of the average load. It is generally considered that the load is smaller than the CPU core, otherwise the system is out of load.

Second Behavior process information:

content Description
tasks:95 Total Total number of processes in the system
1 Running Number of processes that are running
94 Sleeping The process of sleep
0 stopped Processes that are stopping
0 Zombie Zombie process. If not 0, you need to manually check the zombie process

Third Behavior CPU information:

content Description
Cpu (s): 0.1%us Percentage of CPU consumed by user mode
0.1%sy Percentage of CPU consumed by system mode
0.0%ni Percentage of CPU consumed by user processes that have changed precedence
99.7%id CPU percent of idle CPU
0.1%wa CPU percentage of processes waiting for input/output
0.0%hi Percentage of CPU consumed by the hard interrupt request service
0.1%si Percentage of CPU consumed by soft interrupt request Service
0.0%st The St (steal Time) virtual percentage.

Physical memory information of the IV behavior:

content Description
mem:625344k Total Total amount of physical memory, in kilobytes
572683K used Amount of physical memory already in use
32343k Free Amount of free physical memory
64443k buffers Amount of memory as buffered

V Behavior swap partition (swap) Information:

content Description
swap:432432k Total Total size of swap partition (virtual memory)
0k used The size of the interactive partition that has been used
544562k Free Size of the idle swap partition
402354k Cached The size of the interactive partition as a cache
Kill process

To view the signal:

kill -l
Signal Code Signal name Description
1 SIGHUP This signal enables the process to shut down immediately and then restarts after the configuration file is read again.
2 SIGINT The program terminates the signal for terminating the foreground process. Equivalent to output CTRL + C shortcut key
8 SIGFPE Emitted in the event of a fatal arithmetic operation error, including not only floating-point arithmetic errors, but also all other arithmetic errors such as overflow and divisor 0
9 SIGKILL Used to immediately end the operation of the program, this signal is not the grace of the block, processing and neglect. Typically used to force a process to terminate
14 Sigalrm The clock timing signal, which calculates the actual time or always time, is used by the alarm function to use the signal.
15 SIGTERM The signal of the normal end process, the default signal of the KILL command. Sometimes if the process has been a problem, this signal is unable to terminate the process normally, we will try to sigkill signal, that is, signal 9.
18 Sigcont This signal allows the paused process to resume execution and the signal cannot be blocked.
19 SIGSTOP This signal can pause the foreground process, equivalent to the input CTRL + Z shortcut key. This signal cannot be blocked.

For example, smooth restart:

kill -HUP 1523kill -1 1523

Kill plus process number, can kill a single process, killall can kill a set of processes:

killall [信号] 进程名

Parameters:

    • -I interactive, will remind you whether to kill each process
    • -I ignores case

You can use the Pkill command to kick out the logged-in User:

// 查看当前登录用户w// 根据tty号踢出用户pkill -9 -t [tty]pkill -9 -t pts/1
Modify Process Priority

Using the PS view process first, you can see the PRI and NI two properties.

The priority of the process is played by the PRI and NI, but the PRI value cannot be modified, and the value of NI can be modified, which actually takes the PRI value. The smaller the value, the higher the priority.

The nice command modifies NI values while creating a new process, but cannot modify the NI value of an existing process.

nice -n -5 service httpd start

You can use Renice to modify the priority of an existing process, but you can only modify one process at a time:

renice -10 [PID]

Considerations for modifying NI values:

    • The range of NI values is 20 to 19;
    • The average user adjusts the NI value range from 0 to 19, and can only adjust its own process;
    • Ordinary users can only increase the NI value, but not lower, such as the original NI value of 0, you can only adjust to greater than 0;
    • The root user is able to set the process NI value to negative, and can adjust any user's process.
    • PRI (final value) = PRI (original value) + NI
    • The user can only modify the value of NI and cannot modify the PRI directly
Introduction to Job Management

Precautions:

    • Current login terminal, can only manage the work of the current terminal, can not manage the work of other login terminals
    • Commands placed in the background must be able to run for a period of time so that we can catch and manipulate the work (e.g. LS meaningless)
    • Commands to be executed in the background no, uh, the one that interacts with the foreground user or needs the foreground input (top, vi), otherwise it can only be paused in the background and cannot be executed.
Method

There are two ways to put a process into the background:

    • +&: Put into background execution
    • ctrl + z: Pause on background

Use jobs commands to view background work, plus -l parameters to display the working PID.

Recovery process:

    • fg %工作号: Revert to foreground, work number such as [1], not PID, asfg %1
    • bf %工作号: Revert to Background

Two commands if %工作号 the process of recovering + sign is not represented.

Background command out of login terminal
    • The first method is to add a command to the file that needs to be executed in the background /etc/rc.local . The commands in the file are executed at system startup and are not dependent on the terminal.
    • The second approach is to use a system timer task that allows the system to execute a background command at a specified time. (Not recommended)
    • The third method is to use the Nohup command. Such asnohup /root/run.sh &
View system resources vmstat (important)

Monitoring system resources:

vmstat [刷新延时 刷新次数]vmstat 1 3  //每1秒刷新一次,总共刷新3秒

Get input as follows:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st 0  0 7846652 136672  27584 655328    5    8    21    36    3    7  3  1 95  0  0 0  0 7846652 128356  27600 663732    4    0     4   264 1127 3026  3  1 96  0  0 0  0 7846652 128356  27600 663740    8    0     8     0 1162 3053  2  1 97  0  0
    • Procs: Process Information field
      • R: The number of processes waiting to run, the greater the number, the busier the system
      • B: The number of processes that cannot be awakened, the greater the number, the busier the system
    • Memory: Information field
      • Swap: Usage of virtual memory, in kilobytes
      • Free: idle memory capacity, in kilobytes
      • Buff: Buffered memory capacity, in kilobytes
      • Cache: Cached memory capacity, in kilobytes
    • Swap: The information field of the swap partition
      • Si: Number of data from disk swap to in-memory, in kilobytes
      • So: The number of bytes of data exchanged from memory to disk, in kilobytes. The larger the two numbers, the more often the data needs to be exchanged between disk and memory, and the performance of the system is worse.
    • IO: Disk read-Write information field
      • BI: The total amount of data read from a block device, in blocks
      • Bo: The total amount of data written to the block device, in units of blocks. The larger the two numbers, the busier the I/O on behalf of the system
    • System: Systems Information Field
      • In: Number of processes interrupted per second
      • CS: The number of event switches performed per second. The larger the two numbers, the more busy the communication on behalf of the system to the interface device.
    • CPU:CPU Information fields
      • US: Percentage of CPU time consumed by non-kernel processes
      • Sy: Percentage of CPU time consumed by kernel processes
      • ID: Percentage of idle CPU
      • WA: Percentage of CPU waiting for I/O consumption
      • ST: CPU accounted for by virtual machine
DMESG (important)

To view kernel information:

dmesgdmesg | grep CPU
Free (important)

To view memory usage status:

free [-b|-k|-m|-g]
    • -B: Displayed in bytes
    • -K: Displayed in kilobytes, default is in KB
    • -M: Displayed in megabytes
    • -G: Displayed in GB
CPUinfo

To view CPU information:

cat /proc/cpuinfo

procDirectory to store the memory information.

Uptime

View login information, equivalent to the first line of the top command.

Uname

View system-related information about the kernel.
Parameters:

    • -A: View all relevant information about the system
    • -r: View kernel version
    • -S: View kernel name
File

To view the number of operating system bits:

file /bin/ls
Lsb_release

To view the system release version:

lsb_release -a
Lsof

Lists information for a process call or open file.
Parameters:

    • -C string: Only files opened with a process beginning with a string are listed
    • -u user name: List only files opened by a user's process
    • -P PID: Lists files opened by a PID process

Examples of Use:

lsof | more         # 查询系统中所有进程调用的文件lsof /sbin/init     # 查询某个文件被哪个进程调用lsof -c httpd       # 查看httpd进程调用了哪些文件lsof -u root        # 按照用户名,查询某用户的进程调用的文件名
System timed tasks at one time scheduled tasks

View at service:

chkconfig --list | grep atd     // at服务是否安装service atd state   // at服务是否启动service atd start   // 启动at服务

Access access control for at:

    • If there are files in the system /etc/at.allow , only /etc/at.allow users who are written to the file (whitelist) can use the AT command (the /etc/at.deny file is ignored)
    • If there are no files in the system /etc/at.allow and only /etc/at.deny files, then /etc/at.deny the user in the Write file (blacklist) cannot use the AT command. does not work for root.
    • If none of the two files exist in the system, only the root user can use the AT command. So be sure to have a At.deny file, even if it's empty.

AT command:

    • At [Options] Time
    • Options:
      • -M: When the at job is finished, the user who executes the AT command is notified by email whether or not the command has output
      • -C Job Number: shows the actual content of the at job
    • Time:
      • hh:mm Example: 02:30
      • hh:mm Yyyy-mm-dd for example 02:30 2013-07-25
      • HH:MM[AM|PM] [month][date] For example 02:30 July 25
      • Hh:mm[am|pm]+[minutes|hours|days|weeks] For example now + 5 minutes

Example:

at now + 2 minutes  // 只告诉时间会在后面提示输入命令at> /root/hello.sh
at 02:00 2013-07-26

To view scheduled tasks:

atq         // 查看定时任务at -c 1     // 查看任务详情

To delete a task:

at rm [工作号]
Crontab Cycle Timer Task

Access control:

    • When there is a file in the system /etc/cron.allow , only the user who writes the file can use the crontab command, and the user who does not write cannot use the crontab command. Also, if you have this file, the file is /etc/cron.deny ignored and /etc/cron.allow the file is given a higher priority.
    • When there are only files in the system /et/cron.deny , the user who writes to this file cannot use the crontab command, and the Crontab command is available to users who do not write to the file.
crontab [选项]

Options:

    • -E: Edit crontab timed task, Format: * * * * * * [command]
    • -L: Query crontab task
    • -R: Delete all crontab tasks for the current user

Precautions:

    • crontabis a user-bound, unable to perform an identity-inconsistent command
    • All six options cannot be empty and must be filled in. If you are unsure, use "*" to represent any time.
    • crontabScheduled tasks, the minimum effective time is the minute, the maximum time range is the month. As of 2018, 3 points, 30 minutes, 30 seconds, are not recognized at any time.
    • When defining a time, the dates and weeks are best not to appear in a timed task because they are all in days, and both days are in effect, making it very easy for administrators to mess up.
    • It is best to use absolute paths in a timed task, whether it is a direct write command or a command written in a script.
System timed Tasks

There are two ways to perform system timing tasks:

    • The first is to copy the script that needs to be executed regularly to /etc/cron.{daily,weekly,monthly} any of the directories
    • The second type is /etc/crontab to modify the configuration file, the command format* * * * * user command
Anacron Configuration

The role of Anacron is to perform scheduled tasks that are not performed in a failure after a system recovery.

Anacron Detection cycle:

    • Anacron will be tested for a day, seven days, one months
    • A file exists in the system /var/spool/anacron/ directory cron.{daily,weekly,monthly} to record the last time Cron was executed
    • Compared to the current time, if the difference of two time exceeds the specified Anacron value, it proves that there is a crontab task executed.

The difference between CentOS 6.x:

    • In the old CentOS version, /etc/cron.{daily,weekly,monthly} These directories will be called by Cron and will be called by Anacron, which is easy to repeat.
    • In CentOS 6.x, it is only called by Anacron and avoids repeated execution.
    • In CentOS 6.x, Anacron is no longer a service, but a system command

Anacron configuration file:

vi /etc/anacrontab
    • RANDOM_DELAY=45: Maximum random delay
    • Start_hours_range=3-22:anacron Execution time range is 3:00-22:00
    • 1 5 cron.daily Run-parts--report/etc/cron.daily
    • 7 cron.weekly run-parts--report/etc/cron.weekly
    • @monthly cron.monthly run-parts--report/etc/cron.monthly
      • Days forced Delay (min) Job name actual execution of the command

Cron.daily work to illustrate the execution process:

    • The time of the /var/spool/anacron/cron.daily last Anacron execution in the first read
    • Compared to the current time, if the difference of two time is more than 1 days, the cron.daily work is performed.
    • This job can only be done between 03:00-22:00.
    • Force delay of 5 minutes for work and 0-45 minutes for random delay
    • Use the Nice command to specify the default priority and use the run-parts script to execute /etc/cron.daily all executables in the directory.

Linux System Management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.