简介
进程应该是Linux操作系统里面最重要的一块内容了,通过进程的调度支撑着整个系统的运行管理,平时工作的时候也不可避免的需要涉及到进程相关的操作,比如启动服务、查看服务、终止服务等。
示例
启动进程
- 编写C程序代码
- 编译C程序
gcc main.c -o process_test
- 启动C程序
./process_test
#include <stdio.h>
int main()
{
printf("linux process!\n");
while(1);
}
查看进程
ps命令
输出进程当前状态的快照
- ps -ef
- ps aux
[xiaofeng@localhost ~]$ ps aux|grep process_test
xiaofeng 3811050 99.9 0.0 4360 772 pts/1 R+ 10:57 6:00 ./process_test
pgrep命令
通过进程名或者属性进行查找
- pgrep process_test
- pgrep -u root process_test
[xiaofeng@localhost ~]$ pgrep process_test
3811050
pidof命令
通过进程名查找进程ID
- pidof process_test
[xiaofeng@localhost ~]$ pidof process_test
3811050
pstree命令
按树状显示进程信息
- pstree -p
[xiaofeng@localhost ~]$ pstree -p|grep process
| |-sshd(3810404)---sshd(3810614)---bash(3810615)---process_test(3811050)
top命令
显示进程当前运行信息
- top | grep process_test
[xiaofeng@localhost ~]$ top -p 3811050
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 12.6 us, 0.0 sy, 0.0 ni, 87.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 64150.6 total, 15183.0 free, 6708.2 used, 42259.4 buff/cache
MiB Swap: 32276.0 total, 32157.9 free, 118.1 used. 56689.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3811050 xiaofeng 20 0 4360 772 700 R 99.7 0.0 16:31.22 process_test
/proc/{pid}目录
根据进程ID查看文件得到进程启动命令行、oom等信息
- cat /proc/3811050/cmdline
[xiaofeng@localhost ~]$ ls /proc/3811050/ -l
total 0
dr-xr-xr-x. 2 xiaofeng xiaofeng 0 Mar 17 11:10 attr
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 autogroup
-r--------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 auxv
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 cgroup
--w-------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 clear_refs
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 10:57 cmdline
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 comm
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 coredump_filter
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 cpu_resctrl_groups
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 cpuset
lrwxrwxrwx. 1 xiaofeng xiaofeng 0 Mar 17 11:16 cwd -> /home/xiaofeng/workspace/C/processmng
-r--------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 environ
lrwxrwxrwx. 1 xiaofeng xiaofeng 0 Mar 17 10:58 exe -> /home/xiaofeng/workspace/C/processmng/process_test
dr-x------. 2 xiaofeng xiaofeng 0 Mar 17 11:16 fd
dr-x------. 2 xiaofeng xiaofeng 0 Mar 17 11:16 fdinfo
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 gid_map
-r--------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 io
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 limits
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 loginuid
dr-x------. 2 xiaofeng xiaofeng 0 Mar 17 11:16 map_files
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 maps
-rw-------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 mem
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 mountinfo
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 mounts
-r--------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 mountstats
dr-xr-xr-x. 5 xiaofeng xiaofeng 0 Mar 17 11:16 net
dr-x--x--x. 2 xiaofeng xiaofeng 0 Mar 17 11:10 ns
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 numa_maps
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 oom_adj
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 oom_score
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 oom_score_adj
-r--------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 pagemap
-r--------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 patch_state
-r--------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 personality
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 projid_map
lrwxrwxrwx. 1 xiaofeng xiaofeng 0 Mar 17 11:16 root -> /
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 sched
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 schedstat
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 sessionid
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 setgroups
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 smaps
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 smaps_rollup
-r--------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 stack
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 10:57 stat
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 10:59 statm
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 10:57 status
-r--------. 1 xiaofeng xiaofeng 0 Mar 17 11:16 syscall
dr-xr-xr-x. 3 xiaofeng xiaofeng 0 Mar 17 11:10 task
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 timens_offsets
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 timers
-rw-rw-rw-. 1 xiaofeng xiaofeng 0 Mar 17 11:16 timerslack_ns
-rw-r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 uid_map
-r--r--r--. 1 xiaofeng xiaofeng 0 Mar 17 11:16 wchan
停止进程
kill命令
基于进程ID终止进程
- kill -l
- kill -9 {pid}
[xiaofeng@localhost ~]$ kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
[xiaofeng@localhost ~]$
[xiaofeng@localhost ~]$ pidof process_test
3812534
[xiaofeng@localhost ~]$
[xiaofeng@localhost ~]$ kill -9 3812534
[xiaofeng@localhost ~]$
[xiaofeng@localhost ~]$ pidof process_test
[xiaofeng@localhost ~]$
pkill命令
基于进程名称终止进程
- pkill -9 process_test
[xiaofeng@localhost ~]$ pidof process_test
3812802
[xiaofeng@localhost ~]$
[xiaofeng@localhost ~]$ pkill -9 process_test
[xiaofeng@localhost ~]$
[xiaofeng@localhost ~]$ pidof process_test
[xiaofeng@localhost ~]$
killall命令
基于进程名称终止进程
- killall -9 process_test
[xiaofeng@localhost ~]$ pidof process_test
3813043
[xiaofeng@localhost ~]$
[xiaofeng@localhost ~]$ killall -9 process_test
[xiaofeng@localhost ~]$
[xiaofeng@localhost ~]$ pidof process_test
[xiaofeng@localhost ~]$
总结
- 了解Linux进程的概念,简单来说就是程序执行的载体。
- 了解Linux进程的基本操作,如启动、查看、停止。
- 了解Linux进程资源排查,如内存占用、CPU占用等。
- 更高级的进程调度控制,如nice、renice、chrt有兴趣可以另行深入了解。