Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .