I'm writing a bash
script named test.sh
as below on my Ubuntu:
#!/bin/bash
while true; do
echo 'abc' # pwd, df...
sleep 1
done
When I execute ./test.sh
at one terminal, I open another terminal to execute the commands below:
$ pgrep test
31110
$ ps -ef | grep sleep
me 31140 31110 0 20:58 pts/1 00:00:00 sleep 1
me 31142 16389 0 20:58 pts/0 00:00:00 grep --color=auto sleep
$ ps -ef | grep sleep
me 31146 31110 0 20:58 pts/1 00:00:00 sleep 1
me 31148 16389 0 20:58 pts/0 00:00:00 grep --color=auto sleep
$ ps -ef | grep sleep
me 31150 31110 0 20:58 pts/1 00:00:00 sleep 1
me 31152 16389 0 20:58 pts/0 00:00:00 grep --color=auto sleep
So, the PID of the process ./test.sh
is 31110
, and when I execute the command ps -ef | grep sleep
, I get many processes of sleep 1
(PIDs are 31140
, 31146
, 31150
...), which are all the child-processes of the process ./test.sh
.
Well, for now it seems that I can understand everything, the child-processes of sleep 1
come from that loop of while true
.
However, when I try to ps -ef | grep echo
, I get nothing. I've also tried to execute other commands, such as pwd
, df
, but they can't be grep
ed either.
So my question is why the command sleep
is an independent process whereas other commands aren't.
pwd
,echo
), or runs too quickly for you to spot them (df
).ps aux | grep CMD
... Ok, I can live with that.du
. Maybedu /
?df
to a pipe with no reader to halt it:mkfifo /tmp/f; df > /tmp/f; rm /tmp/f
. You'll need to kill it with e.g. ^C orcat /tmp/f
to release the block on the pipe.