CentOS system management commands

Source: Internet
Author: User
Tags pkill dmesg

CentOS system management commands
Process Management Overview

A process is a program or command being executed. Every process is a running corpse with its own address space and occupies certain system resources.

Role of process management
  • Server Health Check
  • View All PROCESSES IN THE SYSTEM
  • Kill Process
View Process

View all processes

Ps command
  • View all processes in the system using the BSD operating system format
  • Ps-le: view all processes in the system and use the standard Linux Command Format
    • Option
      -A displays all processes of a terminal, except sessions
      -U: displays the owner user and memory usage of the process.
      -X: process with no control terminal displayed
      -L long format display. Show more detailed information
      -E: all processes are consistent with-.
/* Which USER generates the PID process ID Number % percentage of CPU resources occupied by the CPU process? The higher the CPU usage, the more resources consumed by the process % The percentage of physical memory occupied by the MEM process. The higher the CPU usage, the more resources VSZ processes consume the virtual memory, the unit of kb rss processes occupy the actual physical memory, and the unit of kb tty processes run on which terminal. The tty1-tty7 represents the local console terminal, the tty1-tty6 is the local character interface terminal, tty7 is the graphical terminal, pts/0-255 represents the virtual terminal, remote terminal tty1-tty7 can be through CTL + ALT + F1 ~ CTL + ALT + F7 to switch. TTY display? It indicates that the process is started by the kernel, not by the terminal. STAT Process status. Common statuses: R running, S sleep, T stopping, s containing sub-processes, and + at the startup TIME of the background START process, which consumes the CPU computing TIME, note that the COMMAND name */$ ps auxUSER PID % CPU % mem vsz rss tty stat start time COMMANDroot 1 0.0 0.4 185316 4116 is not generated by the system time command? Ss June 24 0:23/sbin/init splroot 2 0.0 0.0 0 0? S June 24 0:00 [kthreadd] root 3 0.0 0.0 0 0? S June 24 4:07 [ksoftirqd/0] root 5 0.0 0.0 0 0? S <June 24 0:00 [kworker/0: 0 H] root 7 0.1 0.0 0 0? S June 24 16:09 [rcu_sched]...
Pstree command
  • Pstree
    • Option-p display process PID-u display process user
$ pstreesystemd─┬─ModemManager─┬─{gdbus}        │              └─{gmain}        ├─NetworkManager─┬─dhclient        │                ├─dnsmasq        │                ├─{gdbus}        │                └─{gmain}        ├─accounts-daemon─┬─{gdbus}        │                 └─{gmain}        ├─acpid        ├─5*[agetty]        ├─avahi-daemon───avahi-daemon        ├─colord─┬─{gdbus}...
Top Command

View System Health Status

  • Top
    • Option-d seconds indicates the number of seconds that the voting command is updated. The default value is 3 seconds-B uses the batch processing mode for output. Average and-nOption-n times specifies the number of times the top Command is specified. Average and-bOption combination
  • What commands can be executed in the interactive mode of top commands? Or h indicates that the help P in interactive mode is sorted by CPU usage. By default, M is sorted by memory usage. N is sorted by PID and q is exited from top.

The first line is the task queue information.

ContentDescription12:26:46 system current time up 1 day, system running time, the local machine has been running 1 day 13 hours 32 minutes 2 users currently logged on to two users load average: 0.00, 0.00, 0.00 average load of the system in the previous 1 minute, 5 minutes, 15 minutes. Generally, when the task is less than 1, the load is small. If the value is greater than 1, the system has exceeded the load.

Second-Action Process Information

ContentDescriptionTasks: 95 tatal system in the process of the total number of processes 1 running is running the number of 94 sleeping process 0 stopped is stopping the process 0 zombie botnet. If it is not 0, manually check the zombie Process

Note:

Orphan process: if a parent process exits and one or more child processes are still running, those child processes will become orphan processes. The orphan process will be adopted by the init process (process number 1) and collected by the init process.

Zombie Process: A process uses fork to create a child process. If the child process exits and the parent process does not call wait or waitpid to obtain the status information of the child process, the sub-process descriptor is still stored in the system. This process is called a zombie process.

Line 3 CPU Information

ContentDescriptionCpu (s ): 0.1% us CPU usage in user mode 0.1% sy CPU usage in system mode 0.0% ni CPU usage in user processes that have changed their priorities 99.7% id idle CPU usage 0.1% wa waiting for input/ CPU usage of the output process 0.0% hi hardware terminal request service CPU usage 0.1% si soft interrupt request service CPU usage 0.0% stst (Steal time) virtual time percentage. That is, the percentage of time the virtual CPU waits for the actual CPU when there is a virtual machine

Row 4 physical memory information

ContentDescriptionMem: The total physical memory of 625344 k total. The unit is KB571504k used. The number of physical memory used is 53840. The number of free physical memory is. We use virtual machines. In total, only MB of memory is allocated, therefore, only 65800 MB of idle memory is used as the buffer for k buffers.

Note:

Buffers: Accelerate writing. For example, to save the file content, it is true that the buffer of the file process is written first, and then the hard disk is written when the file is closed and the system is idle.

Fifth behavior swap partition (swap) Information

ContentDescriptionSwap: 524280 k total Swap partition (Virtual Memory) total size OK used the size of the used interactive partition is 524280 k free swap partition size 409289 k cached as the cache swap partition size

Note:

Cached: Accelerate reading. For example, when reading a hard disk file, you can cache some files in the memory. You do not need to access the hard disk every time to speed up reading files.

Top content mainly displays the load average, cpu idle rate, memory idle, and Swap partition idle.

// Change the top refresh time to 1 s, but it is not recommended that the top itself consume resources $ top-n 1 // you can only see a few programs in the terminal when using top, therefore, use-B to save the process to the file $ top-B-n 1> top. log $ cat top. log
Kill command for Process Termination
  • Kill-l
    • View available process signals
Signal codeSignal nameDescription1SIGHUB this signal causes the process to immediately shut down, and then re-read the configuration file and restart the 2SIGINT program to terminate the foreground process. It is equivalent to the 8SIGFPE shortcut for the output ctl + c when a fatal arithmetic operation error occurs. It includes not only floating point operation errors, but also overflow and Division 0, and all other arithmetic errors. 9SIGKILL is used to immediately end the program. This signal cannot be blocked, processed, or ignored. It is generally used to forcibly terminate a process. The 14SIGALRM timing signal calculates the actual time or clock time. The alarm function uses the 15SIGTERM signal to terminate the process normally. The default signal of the kill command is used. Sometimes if the process has encountered a problem, the signal cannot terminate the process normally, so we will try the SIGKILL signal, that is, the signal 9. 18SIGCONT this signal can resume execution of the paused process. This signal cannot be blocked. 19SIGSTOP this signal can pause the foreground process, which is equivalent to entering the ctl + z shortcut key. This signal cannot be blocked.

SIGHUB signal if the apache server re-sets the configuration file, you need to restart the apache service, if you use the service command stop, start to close the service, resulting in poor user experience. You can use the Kill-HUB pid command to reload the configuration of the apache service for smooth restart. However, if the apache service has multiple service processes, you can use the killall command. Smooth restart does not cause Server login users to be disconnected.

SIGKILL forces Process Termination, kill-9 2236

// View the available process signal $ kill-l // force terminate the process. Note that the pid cannot be the process name $ kill-9 2237 after kill.
Killall command
  • Killall [Option] [Signal] process name
    • Option-I interactive, ask if you want to kill a process-I ignore the case sensitivity of the process name
// Killall-9 httpd
Pkill command
  • Pkill [Option] [Signal] process number
    • Option-t terminal number: Kick out the user according to the terminal number

Note: The pkill and killall commands are basically the same.

// View the current logon user $ w 19:53:45 up, 1 USER, load average: 0.00, 0.01, 0.05 user tty from login @ idle jcpu pcpu WHATroot tty1-3: 02 m 23.87 s 0.23 s-bashroot pts/0 192.168.44.1 04:47 0.00 s 0.22 s 0.00 s wroot pts/0 192.168.44.1 04:47 0.00 s 0.22 s 0.00 s-bash // only root users can kill local user # pkill-9-t tty1
Introduction to modifying process priorities of process priorities

Linux is a multi-user, multi-task operating system, and many processes are notified to run in Linux. However, the CPU can only calculate one command within a unified clock cycle. The process priority determines the processing sequence of each process.

$ Ps-leF s uid pid ppid c pri ni addr sz wchan tty time limit 4 S 0 1 0 0 80 0-29895 -? At 00:00:02 systemd1 S 0 2 0 0 80 0-0 -? 00:00:00 kthreadd1 S 0 3 2 0 80 0-0 -? 00:00:00 ksoftirqd/01 S 0 5 2 0 60-20-0 -? 00:00:00 kworker/0: 0H1 S 0 7 2 0 80 0-0 -? 00:00:03 rcu_sched1 S 0 8 2 0 80 0-0 -? 00:00:00 rcu_bh1 S 0 9 2 0-40--0 -? 00:00:00 migration/05 S 0 10 2 0-40--0 -? At 00:00:00 watchdog/05 S 0 11 2 0 80 0-0 -? 00:00:00 kdevtmpfs1 S 0 12 2 0 60-20-0 -? 00:00:00 netns // PRI stands for Priority, and NI stands for Nice. Both values are priorities. The smaller the number, the higher the priority of the process.
Several Notes for changing the NI value
  • The value range of NI is-20 to 19.
  • The average user can adjust the NI value range from 0 to 19, and can only adjust their own processes
  • Normal users can only increase the NI value rather than decrease it. For example, if the original NI value is 0, it can only be adjusted to a value greater than 0.
  • The root user can set the process NI value to a negative value, and can adjust any user's Process
  • PRI (final value) = PRI (original value) + NI
  • The user can only modify the NI value, but cannot directly modify the PRI
Nice command
  • Nice [Option] the nice command can directly assign the NI value to the newly executed command, but it cannot modify the NI value of an existing process.
    • Option-n NI value to assign NI value to the command
// The NI value of a process can be modified only when it is started. It cannot be modified when the process is running # nice-n-5 service httpd start
Renice command
  • Renice [Priority] PID renice command is to modify the NI value of an existing process
// Use renice to modify the NI value of an existing process # renice-10 2125
Work Management Overview

Job Management refers to managing multiple jobs at the same time on a single logon terminal (that is, the shell Interface for Logon.

Notes
  • The current login terminal can only manage the work of the current terminal, but cannot 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 capture and operate on this job.

  • Commands executed in the background cannot interact with the foreground or need to be input in the foreground. Otherwise, the commands executed in the background can only be paused, but cannot be run.

Work Management Method process put into the background
  1. Use & put commands in the background
  2. Use the ctrl + z shortcut to save the command to the background
// Put the command in the background and execute $ tar-zcf etc.tar.gz/etc & // press the ctl + z shortcut key in the background to suspend $ topctl + z in the background
View background work
  • Jobs
    • Option-l display the PID of the job. Note: "+" indicates the last job to be placed in the background. It is also the work that is replied by default when the job is replied ." -"Number" indicates the work of putting the last and second digits into the background.
$ Top & $ top // ctrl + z put in the background // view the background processes. Both top processes are stopped because top displays the processes to the foreground users and interacts with the foreground, you cannot run $ jobs [1]-Stopped top [2] + Stopped top $ vi abc & $ jobs [1] Stopped top [2]-Stopped top [3] + Stopped in the background vi abc
Resume paused background work to the foreground for execution
  • Fg % work number
    • Parameter % work Number % can be omitted, but pay attention to the difference between work and PID
Resume paused tasks in the background to be executed in the background.
  • Bg % work number
    • Parameter % work Number % can be omitted, but pay attention to the difference between work and PID

Note: commands executed in the background cannot interact with the foreground, otherwise they cannot be executed in the background.

Introduction to running background commands out of the logon Terminal

Put the command in the background and can only be executed on the current logon terminal. Once you exit or close the terminal, the background program stops.

How to run background commands without logging on to the terminal
  • Add the command to be executed in the background to the/etc/rc. local file.
  • The system scheduled task allows the system to execute a background command at a specified time.
  • We recommend that you use the nohub command.
  • Daemon, such as mysqld.
// Execute the rc. local file $ cat/etc/rc. localtouch/var/lock/subsys/local // when the system starts. Note: The file is not created here and the timestamp of the file is changed.
// Execute for. sh # vi for. sh # in Terminal 1 #! /Bin/bashfor (I = 0; I <= 1000; I = I + 1) do echo 11>/root/. log sleep 10 done # nohup/root/. sh & // close Terminal 1 // open Terminal 2, ps aux can see. sh is still running
System resource view vmstat command to monitor system resources

Vmstat [Refresh Interval refresh times]

$ vmstat 1 3procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st 0  0  70280 133280  13140 303244    0    4    60    16   53  121  1  1 98  0  0 0  0  70280 133264  13140 303248    0    0     0     0   40  201  1  0 99  0  0 0  0  70280 133264  13140 303248    0    0     0     0   43  183  1  0 99  0  0
  • Process Information Field-r: Number of processes waiting for running. The larger the number, the more busy the system is.-The number of processes that cannot be awakened by B. The larger the number, the more busy the system is.
  • Memory Information Field-usage of swpd virtual memory, unit KB-free idle memory capacity, unit KB-buff buffer memory capacity, unit KB-cache buffer memory capacity, unit: KB
  • The information field of the swap partition-si refers to the number of data transferred from the disk to the memory. Unit: KB-so refers to the amount of data exchanged from the disk to the disk. Unit: KB. The larger the two numbers, it indicates that the data needs to be frequently exchanged between the disk and memory, and the worse the system performance.
  • Io disk read/write information field-the total amount of data that bi reads from Block devices. The unit is the total amount of data that block-bo writes to block devices. The unit is block. The larger the number of times, the more busy the system I/O.
  • System Information Field-in: Number of interrupted processes per second-number of time switches performed by cs per second. The larger the two numbers, the communication between the system and the interface device is very busy.
  • CPU Information Field-us percentage of CPU computing time consumed by non-kernel processes-sy percentage of CPU computing time consumed by kernel processes-id percentage of idle CPU-wa consumption during I/O CPU usage-Percentage of CPU occupied by the st Virtual Machine
Dmesg kernel detection information during startup
$ dmesg |grep CPU
Free command to view memory usage status
  • Free [-B |-k |-m |-g]
    • Option-B is displayed in bytes.-k is displayed in KB. The default value is KB.-m is displayed in MB.-g is displayed in GB.
/* Total memory used free idle memory shared by multiple processes buffers buffer memory cached cache memory default unit is KB */$ free -m total used free shared buff/cache availableMem: 983 545 124 6 313 273 Swap: 1906 68 1838
View CPU Information
$ cat /proc/cpuinfo
Uptime command

Display the system startup time and average load, that is, the first line of the top command. The w command can also see this data.

$ Uptime 10:10:47 up 10:11:35, 1 user, load average: 0.18, 0.10, 0.09 $ w up, 1 user, load average: 0.09, 0.09, 0.09 user tty from login @ idle jcpu pcpu WHATroot tty7: 0 a 16 17: 20 m 49.38 s 0.28 s/sbin/upstart -- user
View System and kernel information
  • Uname [Option]
    • Option-a view all system information-r view kernel version-s view kernel name
Determine the number of digits of the current system
  • File/bin/ls
View the release version of the current Linux System
  • Lsb_release-
Lists information about files opened or used by a process.
  • Lsof [Option] lists information about a process calling or opening a file.
    • Option-c string only lists files opened by processes starting with a string-u user name only lists files opened by a user's processes-p pid list files opened by a PID Process
// Query the files called by all processes in the query system $ lsof | more // query which process calls a file $ lsof/sbin/init // check which files are called by the httpd process $ lsof- c httpd // follow the user name, query the files called by a user process $ lsof-u root

The difference between cache and buffer is simply that cache is used to accelerate data "read" from the hard disk, while buffer is used to accelerate data "write" to the hard disk.

System scheduled task
  • At one-time scheduled task
  • Crontab cyclic scheduled task
  • Crontab settings of the system
  • Anacron Configuration
At command to confirm at Installation
// Whether the at service is installed # chkconfig -- list | grep atd # service atd status
At Access Control
  • If/etc/. allow file, only write/etc/. users in the allow file (whitelist) can use the at command (/etc/. deny files are ignored)

  • If there is no/etc/at. allow file in the system and only the/etc/at. deny file is available, users in the/etc/at. deny file (blacklist) cannot use the at command.Does not work for root

  • If the two files do not exist in the system, only the root user can use the at command.

At command

At [Option] Time option-m when at is finished, whether or not the command has output, the user-c work number that uses e-mail notification to execute the at command shows the actual content of the at work time:-HH: MM for example: 02: 30-HH: MM YYYY-MM-DD For example: 02: 30 2013-07-25-HH: MM [am | pm] [month] [date] example: 02: 30 July 25-HH: MM [am | pm] + [minutes | hours | days | weeks] example: now + 5 minutes

Example
$ At now + 2 minutesat>/root/hello. sh>/root/hello. logat> <EOT> // restart at $ at 2013-07-26at>/bin/syncat>/sbin/shutdown-r now
Other at management commands
  • Atq

    • Query at tasks on the current server
  • Atrm [Work number]

    • Deletes a specified at task.
Crontabcrond service management and access control
// Check whether the crond service is installed and started # service crond restart # chkconfig crond on
Access Control
  • When there is a/etc/cron. allow file in the system, only users who write this file can use the crontab command. users who do not write this file cannot use the crontab command. Similarly, if this file exists, the/etc/cron. deny file will be ignored. The/etc/cron. allow file has a higher priority.

  • When the system only contains the/etc/cron. deny file, the user who writes the file cannot use the crontab command. If the file is not written, the crontab command can be used.

User crontab settings
  • Crontab [Option]
    • Option:-e: Edit crontab scheduled task-l query crontab task-r: delete all crontab tasks of the current user
// Edit the crontab command $ crontab-e ***** command
ProjectDescriptionRangeNumber of minutes in the first * hour * number of minutes in the second * number of hours in the day * Number of days in the month * number of months in the year * number of months 1-12 in the year the number of weeks in a week is 0-7 (both 0 and 7 represent Sunday)Special symbolsDescription* Represents any time. For example, the first "*" indicates that the execution is performed once every minute in an hour, indicating the discontinuous time. For example, the "0, 12, 16 *** command" indicates that the command is executed at 08:00, and every day-representing the continuous time range, for example, the "0 5 ** 1-6 command" means to execute the command at 05:00 from Monday to Saturday */n, which indicates how often the command is executed. For example, the "*/10 *** command" indicates that the command is executed every 10 minutes.
// Cron command, which is executed at 00:00 on the 1st and 15th of every month. // note: the day of the week and the number of the week are not recommended to appear at the same time. They define the day as the day. Very confused. 0 0 111*1 command // example // write 111 */5 *** echo>/root/test every 5 minutes
Crontab considerations
  • The six options cannot be blank and must be filled in. If "*" is not specified, it indicates any time.

  • Crontab scheduled task. The minimum effective time is minute and the maximum time range is month. If you want to execute it at a time on January 1, 2018, it cannot be identified at 03:30:30.

  • When defining the time, it is recommended that the date and week do not appear in a scheduled task, because they are all in the unit of day, the shovel is easier to make administrators confused.

  • In a scheduled task, whether it is to write commands directly or in a script,It is best to use absolute paths.

Note: The environment variables of the scheduled task are not necessarily the same as those saved in $ PATH. Therefore, use the absolute PATH in crontab.

Crontab settings set by the System

"Crontab-e" is the command executed by each user. That is to say, different user identities can execute their own scheduled tasks. However, some scheduled tasks need to be executed by the system. We need to edit the configuration file/etc/crontab.

// Here, crontab is a scheduled task of a common user $ crontab-e // configuration file $ vim/etc/crontabSHELL =/bin/bashPATH =/sbin in the system scheduled task // CentOS 5: /bin:/usr/sbin:/usr/binMAILTO = rootHOME =/# run-parts01 *** root run-parts/etc/cron. hourly02 4 *** root run-parts/etc/cron. daily22 4 ** 0 root run-parts/etc/cron. weekly42 4 1 ** root run-parts/etc/cron. monthly
// View the directories and files related to crontab in/etc # ls/etc/croncron. d/cron. hourly/crontab cron. daily/cron. monthly/cron. weekly/
How to execute scheduled tasks of the system
  • Manually execute scheduled tasks

  • System scheduled task

    • The first is to copy the script that needs to be periodically executed to any of the/etc/cron. {daily, weekly, monthly} directories.

    • The second is to modify the/etc/crontab configuration file.

Anacron Configuration

Anacron is a scheduled task that is missed during system shutdown. It can be executed after the system is started.

Anacron detection cycle
  • Anacron uses one day, seven days, and one month as the detection cycle

  • The cron. {daily, weekly, monthly} file exists in the/var/spool/anacron/directory of the system to record the time when cron was last executed.

  • Compared with the current time, if the difference between the two times exceeds the specified time difference of anacron, it proves that cron tasks are not executed.

Differences between CentOS 6.x
  • In the old CentOS version, the/etc/cron. {daily, weekly, monthly} directories will be called by cron and anacron, which is easy to execute repeatedly.

  • In CentOS 6. x, it will only be called by anacron, avoiding repeated execution.

  • In CentOS 6. x, anacron is no longer a service, but a system command.

Anacron configuration file
$ Vi/etc/anacrontab #/etc/anacrontab: configuration file for anacron # See anacron (8) and anacrontab (5) for details. SHELL =/bin/shPATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin: /usr/binHOME =/rootLOGNAME = rootRANDOM_DELAY = 45START_HOURS_RANGE = 3-22 # These replace cron's entries1 5 cron. daily run-parts -- report/etc/cron. daily7 10 cron. weekly run-parts -- report/etc/cron. weekly @ monthly 15 cron. monthly run-parts -- report/etc/cron. monthly # Number of days force delay (minutes) the actual execution of the work name command
Cron. daily job to explain the execution process
  • First, read/var/spool/anacron/cron. daily the last time anacron was executed.

  • Compared with the current time, if the difference between the two times exceeds one day, cron. daily will be executed.

  • The task can only be executed -.

  • The forced delay time is 5 minutes, and then the random delay is 0-45 minutes.

  • Use the nice command to specify the default priority and run the run-parts script to execute all executable files in the/etc/cron. daily directory.

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.