Linux 系统管理
Linux 系统管理
PS 查看进程状态
1 | ps [options] |
Options:
-A,-e,a Select all processes. Identical to -e.
-a Select all processes except both session leaders and processes not associated with a terminal.
-d Select all processes except session leaders.
g Really all, even session leaders.
-N,--deselect Select all processes except those that fulfill the specified conditions (negates the selection).
T,t Select all processes associated with this terminal.
r Restrict the selection to only running processes.
x list all processes owned by you and others.
结果:
USER: 进程拥有者
PID: pid
%CPU: 占用的 CPU 使用率
%MEM: 占用的内存使用率
VSZ: 占用的虚拟内存大小
RSS: 占用的内存大小
TTY: 终端的次要装置号码 (minor device number of tty)
STAT: 该进程的状态:
D: 无法中断的休眠状态 (通常 IO 的进程)
R: 正在执行中
S: 静止状态
T: 暂停执行
Z: 不存在但暂时无法消除
W: 没有足够的内存分页可分配
<: 高优先序的进程
N: 低优先序的进程
L: 有内存分页分配并锁在内存内 (实时系统或捱A I/O)
START: 进程开始时间
TIME: 执行的时间
COMMAND:所执行的指令
free 查看内存状态
1 | free |
sleep 命令延迟执行
1 | sleep time; cmd |
kill 杀死进程
1 | kill [-<signal>] pid |
renice (alter priority of running processes)
-n, --priority priority
-g, --pgrp
-p, --pid
-u, --user
change the priority of the processes with PIDs 987 and 32, plus all processes owned by the users daemon and root:
renice +1 987 -u daemon root -p 32
top 显示进程实时状态
top
h 查看帮助
man top 查看man page
一些指标
%CPU -- CPU Usage
%MEM -- Memory Usage (RES)
CODE -- Code Size (KiB)
COMMAND -- Command Name or Command Line
DATA -- Data + Stack Size (KiB)
Flags -- Task Flags
NI -- Nice Value
NU -- Last known NUMA node
P -- Last used CPU (SMP)
PGRP -- Process Group Id
PR -- Priority
RES -- Resident Memory Size (KiB)
SHR -- Shared Memory Size (KiB)
SID -- Session Id
SUID -- Saved User Id
SWAP -- Swapped Size (KiB)
TGID -- Thread Group Id
TIME -- CPU Time
TIME+ -- CPU Time, hundredths
USED -- Memory in Use (KiB)
S -- Process Status
The status of the task which can be one of:
D = uninterruptible sleep
I = idle
R = running
S = sleeping
T = stopped by job control signal
t = stopped by debugger during trace
Z = zombie
us, user : time running un-niced user processes
sy, system : time running kernel processes
ni, nice : time running niced user processes
id, idle : time spent in the kernel idle handler
wa, IO-wait : time waiting for I/O completion
hi : time spent servicing hardware interrupts
si : time spent servicing software interrupts
st : time stolen from this vm by the hypervisor
htop
btop++
前后台切换
1 | ctrl+z -> bg -> |
jobs 查看挂起到后台的进程
systemctl 服务管理
1 | # 修改服务状态 |
1 | systemctl enable clamd@scan.service |
Unit(系统资源):
Service unit:系统服务
Target unit:多个 Unit 构成的一个组
Device Unit:硬件设备
Mount Unit:文件系统的挂载点
Automount Unit:自动挂载点
Path Unit:文件或路径
Scope Unit:不是由 Systemd 启动的外部进程
Slice Unit:进程组
Snapshot Unit:Systemd 快照,可以切回某个快照
Socket Unit:进程间通信的 socket
Swap Unit:swap 文件
Timer Unit:定时器
1 | # 列出正在运行的 Unit |
配置文件的格式
systemctl cat atd.service
[Unit]
Description=ATD daemon
[Service]
Type=forking
ExecStart=/usr/bin/atd
[Install]
WantedBy=multi-user.target
Target : 一个 Unit 组,包含许多相关的 Unit
1 | # 查看当前系统的所有 Target |
日志
1 | # 查看所有日志(默认情况下 ,只保存本次启动的日志) |
其他系统管理命令
systemd-analyze命令用于查看启动耗时
1 | systemd-analyze # 查看启动耗时 |
hostnamectl命令用于查看当前主机的信息
1 | # 显示当前主机的信息 |
localectl命令用于查看本地化设置
1 | # 查看本地化设置 |
timedatectl命令用于查看当前时区设置
1 | # 查看当前时区设置 |
loginctl命令用于查看当前登录的用户
1 | # 列出当前session |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 QUANWEI!