0
$cat t.sh grep "12" test | wc -c 

I want to know how many processes will be created when it runs. I used

./t.sh;ps -aux | grep "t.sh" 

but it didn't work because "./t.sh" had run over when ps was working.

How can I reach this goal?

3 Answers 3

1

Depends on the system you are running on. If you have strace you can trace all the fork system calls. problem is though that somesystems use fork, some vfork and some clone, you will have to experiment. On linux:

strace -c -f -evfork ./t.sh 

should give you a summary. -c gives a count, -f means "follow" child processes, and -evfork means trace the vfork kernel call. The output goes to stderr, but you can redirect it to a file using the -o option (there are some neat tricks you can do if you specify a named pipe here).

Sign up to request clarification or add additional context in comments.

Comments

1

Your mistake is the semicolon. It tells the shell to wait until t.sh finishes before running grep. Use ampersand instead to tell the shell to run t.sh in the background. Also instead of grep you'd be better off using pstree to see the list of processes spawned by your t.sh script like this:

$ ./t.sh & pstree $(pgrep t.sh) 

Comments

0

you can try
./t.sh;ps -aux | grep -E "t.sh" -E match exactly what you need and nothing else, I'm not on linux right now so I can't be sure, but there's something to do with that option

1 Comment

-E option to grep means use extended regular expressions, rather than basic regular expressions. grep -E is equivalent (and on Linux exactly the same) as egrep.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.