1

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 consider any special cases?

2

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"

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.