为什么不“回声"?出现在"ps"中? [英] Why doesn't "echo" show up in "ps"?

查看:66
本文介绍了为什么不“回声"?出现在"ps"中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解ps命令中显示的内容.为了测试我的理解,我在虚拟脚本下创建了

I am having great difficulty in understanding what shows up on ps command. To test my understanding I created below dummy script

 #!/bin/bash
 for i in {1..100000}
 do
  date -u
  date -u
  date -u
 done

运行此脚本时,我打开了一个新终端并重复执行

while running this script I opened a new terminal and executed repeatedly

ps -eaf | grep date | grep -v grep

并且我能够在输出中确定过程的日期.

and I was able to date process in the output.

我后来通过将 date -u 替换为 echo正在发生什么事情"

     #!/bin/bash
     for i in {1..100000}
     do
      echo "What is going on"
      echo "What is going on"
      echo "What is going on"
     done

在运行更新的虚拟脚本时,我打开了一个新终端并重复执行

while running the updated dummy script, I opened a new terminal and executed repeatedly

ps -eaf | grep echo | grep -v grep

,并且在输出中从未显示回声.为什么是这样?我怀疑原因是该脚本是bash脚本,可能是由于使用内置回显,因此未在ps输出中显示.我对么?我在这里想念什么?

and echo was never shown in output. Why is this? I suspect the reason is the script being a bash script, may be it is using builtin echo therefore it was not displayed in ps output. Am I correct? What am I missing here?

推荐答案

echo 是bash内置的:

$ type echo
echo is a shell builtin

这意味着在运行 echo 时不会创建新进程.所有的工作都由 bash 进程完成,效率更高.

That means that a new process is not created when echo is run. All the work is done by the bash process instead, which is way more efficient.

您可以显式运行非内置的 echo :

You can run the non-builtin echo explicitly:

command echo "What is going on"

而是分叉并执行/bin/echo ,并使其显示在 ps 中.

This forks and execs /bin/echo instead, letting it show up in ps.

这篇关于为什么不“回声"?出现在"ps"中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆