1

What I want to do is to create a child process inside a script, and

  1. the script is not blocked and will continue to run, and
  2. the child process output (from printf in C code) prints to the shell, and
  3. the child process is killed when the script ends.

When I ran "command &", it did 1) and 2). When I ran "nohup command", it did 1).

Are there any commands that I can use for this purpose?

fiedel
  • 31
  • 6
  • What do you mean by having the output print "to the shell"? Do you mean to the user's terminal (which `command &` *should* do)? – Gordon Davisson Dec 07 '22 at 04:00
  • Yes. Sorry for the mistake: `command &` did 1 and 2 instead of 1 and 3. Killing the script doesn't kill the child process. – fiedel Dec 07 '22 at 04:12
  • Do the answers to ["How do I kill background processes / jobs when my shell script exits?"](https://stackoverflow.com/questions/360201/how-do-i-kill-background-processes-jobs-when-my-shell-script-exits) or ["Terminate running commands when shell script is killed"](https://stackoverflow.com/questions/1644856/terminate-running-commands-when-shell-script-is-killed) do what you need? – Gordon Davisson Dec 07 '22 at 05:01
  • If you write the code logic in pseudo-code, fine-grained for each task that you are trying to accomplish, in the correct sequence and in the correct context, then having that worded so that it does exactly what you want it to do will, almost explicitly, tell you WHAT you need to code for each of those, not the HOW. The HOW is the nitty gritty of coding. What you have outlined above in your question does not demonstrate that level of detail on the task breakdown. If you give that a try, the solution will almost pop out of the page at you. Good luck with your apprenticeship! – Eric Marceau Dec 07 '22 at 22:37

1 Answers1

0

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
Eric Marceau
  • 1,601
  • 1
  • 8
  • 11