The following will perform the task that you are attempting.
Start the script in one terminal.
Open another terminal to enter
ps -ef | grep 'child.sh' | grep -v grep
to show the child process. Then, to see that the child is active, enter
tail -f child.out
Let the main script run a few cycles (about 15 sec) to see the behaviour then in the terminal of the main script, you can enter "<ctl-C>" to stop that.
You will notice that the child is no longer writing to child.out.
Repeating the command
ps -ef | grep 'child.sh' | grep -v grep
will quickly show that the child.sh script is no longer running.
#!/bin/bash
CHILD_SCRIPT="`pwd`/child.sh"
### NOTE: special meaning for having 'EnDoFiNpUt' wrapped in double-quotes
cat >"${CHILD_SCRIPT}" <<"EnDoFiNpUt"
#!/bin/sh
iter=1
while true
do
printf "`date` -- $0[stdOUT]: iteration #${iter} ...\n"
printf "`date` -- $0[stdERR]: iteration #${iter} ...\n" >&2
iter=`expr ${iter} + 1 `
sleep 3
done
EnDoFiNpUt
#cat "${CHILD_SCRIPT}"
chmod 750 "${CHILD_SCRIPT}"
### Control where script sends output; don't let nohup use default.
nohup "${CHILD_SCRIPT}" >child.out 2>child.err &
PID=$!
trap "kill -9 ${PID}" EXIT
while true
do
printf "\t Parent script running ...\n"
sleep 1
done