Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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