I created a bashscript that counts running instances of itself.
Here it is (in this example I am showing instances, not counting them with wc -l):
#!/bin/bash
nb=`ps -aux | grep count_itself.sh`
echo "$nb"
sleep 20
(Of course my script is called count_itself.sh)
Performing it, I expect it to return two rows, but returns three:
root@myserver:/# ./count_itself.sh
root 16225 0.0 0.0 12400 1176 pts/0 S+ 11:46 0:00 /bin/bash ./count_itself.sh
root 16226 0.0 0.0 12408 564 pts/0 S+ 11:46 0:00 /bin/bash ./count_itself.sh
root 16228 0.0 0.0 11740 932 pts/0 S+ 11:46 0:00 grep count_itself.sh
Executing it with a flag &(i.e. in the background and manually executing a bit ps -aux), it returns two what I want:
root@myserver:/
[1] 16233
root@myserver:/
root 16233 0.0 0.0 12408 1380 pts/0 S 11:48 0:00 /bin/bash ./count_itself.sh
root 16240 0.0 0.0 11740 944 pts/0 S+ 11:48 0:00 grep --color=auto count_itself.sh
My question is: Why is the execution ps -auxinside the script returning one line more than expected?
Or, in other words, why was the process with id 16226created in my first example?
EDIT (since most people seem to misunderstand my question):
, bash /bin/bash ./count_itself.sh, grep count_itself.sh.
2:
, , /bin/bash ./count_itself.sh script .