| 1 | #!/bin/bash |
| 2 | # spawn.sh |
| 3 | |
| 4 | |
| 5 | PIDS=$(pidof sh $0) # Process IDs of the various instances of this script. |
| 6 | P_array=( $PIDS ) # Put them in an array (why?). |
| 7 | echo $PIDS # Show process IDs of parent and child processes. |
| 8 | let "instances = ${#P_array[*]} - 1" # Count elements, less 1. |
| 9 | # Why subtract 1? |
| 10 | echo "$instances instance(s) of this script running." |
| 11 | echo "[Hit Ctl-C to exit.]"; echo |
| 12 | |
| 13 | |
| 14 | sleep 1 # Wait. |
| 15 | sh $0 # Play it again, Sam. |
| 16 | |
| 17 | exit 0 # Not necessary; script will never get to here. |
| 18 | # Why not? |
| 19 | |
| 20 | # After exiting with a Ctl-C, |
| 21 | #+ do all the spawned instances of the script die? |
| 22 | # If so, why? |
| 23 | |
| 24 | # Note: |
| 25 | # ---- |
| 26 | # Be careful not to run this script too long. |
| 27 | # It will eventually eat up too many system resources. |
| 28 | |
| 29 | # Is having a script spawn multiple instances of itself |
| 30 | #+ an advisable scripting technique. |
| 31 | # Why or why not? |