Why doesn't “echo” show up in “ps”?

谁说我不能喝 提交于 2019-12-14 02:16:51

问题


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.

I later changed dummy script by replacing date -u with echo "what is going on"

     #!/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

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?


回答1:


echo is a builtin in bash:

$ type echo
echo is a shell builtin

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.

You can run the non-builtin echo explicitly:

command echo "What is going on"

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



来源:https://stackoverflow.com/questions/28840191/why-doesnt-echo-show-up-in-ps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!