I've a scirpt name server.sh
#!/bin/bash
process_count=$(ps aux | grep server.sh | grep -v grep | wc -l )
echo "total process running:"
echo $process_count
... other script code
when I run script I get output as
./server.sh
total process running:
2
Why do I get process count as 2 instead of 1? I only have one script running and have also excluded grep process. Even using pgrep -f server.sh and excluding pgrep gives 2 as process count.
pgrep
would never return its own PID unless you ask it for the PIDs of processes called "pgrep" explicitly. – Kusalananda Aug 27 '21 at 15:53bash
forks a copy of the shell to run it. – Kusalananda Aug 27 '21 at 16:11