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 sshd696 sshd
-
toptop命令动态监视进程,以及系统平均负载、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 sleep3925 sleep
jobs[1]+ Stopped sleep 20
bg %1 #在后台恢复本地进程[1][1]+ sleep 20 &
-
nohupwill 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 sleep4148 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,也可以防止在终端被关闭或断开连接时,运行中的进程被终止。
浙公网安备 33010602011771号