2_进程相关命令用法
Listing Processes
Processes相关命令的用法
-
ps aux
静态查看进程。aux前不用加横号。
-
ps -ef
静态查看进程。-ef
和aux
只是在输出格式上有所区别。vboxuser 5377 4706 0 15:59 pts/0 00:00:00 ps -ef
-
pgrep -l process's name
作用是检索进程id,同时显示进程name。for example:
in bash:
pgrep -l sshd 696 sshd
-
top
top命令动态监视进程,以及系统平均负载、cpu和内存的消耗。
-
在命令后加
&
开启后台进程,非交互式的命令会将输出打印到同一个终端。for example:
in bash:
ifconfig > output.tx 2> errors.txt & 如果不想命令输出打印到终端,可以这样做:
ping -c 1 google.com > /dev/null 2>&1 & /dev/null相当于一个空的设备,俗称文件黑洞blackhole,它会立即丢弃写入其中的任何内容。
-
jobs
命令会在方括号中[]
返回进程的jobs id以及process status。for example:
in bash:
sleep 15& sleep 10& jobs [3]- Running sleep 20&
[4]+ Running sleep 12&jobs -l [3]- 3664 Done sleep 20
[4]+ 3665 Done sleep 12这里需要注意的是jobs id是相对于本地shell终端的,而process id是由系统内核维护并且具有全局范围。
-
如果你想使bring a background running process to the foreground,使用
fg
命令,fg
即foreground。for example:
in bash:
sleep 20& [1] 3875
jobs [1]+ Running sleep 20 &
fg %1 #使后台进程[1]恢复到前台运行 sleep 20
fg
和bg
也可以用来恢复被ctrl + z
中断的进程。for example:
in bash:
==sleep== 20 ^Z
[1]+ Stopped sleep 20pgrep -l sleep 3925 sleep
jobs [1]+ Stopped sleep 20
bg %1 #在后台恢复本地进程[1] [1]+ sleep 20 &
-
nohup
will make the process to ignore all the hang up signals。for example:
in bash:
nohup sleep 123 & #使sleep命令忽略所有hang up信号 nohup: ignoring input and appending output to 'nohup.out'
close the terminal,open it again.
pgrep -l sleep 4148 sleep
the sleep process is still there!
这里注意如果想查看命令输出的结果,可以到'nohup.out'中查看。
for example:
in bash:
nohup ifconfig & cat nohup.out ...
if a process started with nohup and its parent ,which is the bash shell,gets terminated,the process will be adopted by init or systermd which runs as the first process on boot and has PID 1.
其他命令如screen、Tmux,也可以防止在终端被关闭或断开连接时,运行中的进程被终止。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决