I am running a cron on every two minutes. From the cron I am running other shell scripts in background with "&". After cron exits spawned shell script process still runs, this is desired behavior. But does it lead to any zombi process when child script exits. Do I need to conside any special cases?

link|improve this question

50% accept rate
feedback

1 Answer

up vote 2 down vote accepted

it probably will (I should check to be sure) but in any case you can store the PIDs of the forked processes and kill them on exit:

my_forked_process & my_forked_pid=$!

since you said "other shells" I would assume there's a few so we should use an array:

my_forked_pids=()
my_forked_process1 & my_forked_pids+=($!)
my_forked_process2 & my_forked_pids+=($!)

then you kill them like this on exit:

for pid in "${my_forked_pids[@]}"; do
    kill "$pid"
done

[bonus] if you wrap the previous on a function you can set a trap to run it when the script exits regardless of the moment (safety for when it's killed prematurely):

trap cleanup_function 0

0 is a special signal that means "on exit"

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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