0

I would like to run multiple processes where each process is running another one, so it will create a long process branch.

I've tried:

$ bash -c '(bash -c "sleep 20"); sleep 20' &
$ pstree $(pgrep -fn bash)

which creates parent and one child process, but is there any trick or easier way of doing that it would generate further 10-20 processes down without struggling with the syntax too much?

  • you could try forking and set a limit (e.g. each forked process adds one to an integer count and stops forking at a certain threshold to not forkbomb yourself) – Fiximan Aug 27 '15 at 9:27
1

You can create a recursive script. eg in file /tmp/run

#!/bin/bash
depth=${1:-5}
f(){
 let depth--
 if [ $depth -gt 0 ]
 then $0 $depth
 else sleep 10
 fi
}
f

then chmod +x /tmp/run and do /tmp/run 10.

  • Great, I've modified a bit and posted in here. – kenorb Aug 27 '15 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.