I have a perl script which i’d like to spawn a process. It can take a while and most times the parent script will exit. How do I spawn this process so that when the parent is gone it wont turn into a zombie or a defunct process when its done?
edit: I think ive found two methods. Hopefully someone could tell me which one is more appropriate?
- setting $SIG{CHLD} = ‘IGNORE’;
- use POSIX ‘setsid’;
edit: The spawned process is also going to be another perl script.
A process becomes as zombie when it exits and before its parent process picks up its status with
wait(). When one process forks another and then exits, the child becomes a parent of pid 1 (classically “init”) which immediately reaps the process state. So usually the problem is the reverse of what you describe: The child becomes a zombie (since the parent was not written to deal withSIGCHLDand callwait()) but when the parent exits the zombie is inherited byinitand immediately reaped. In fact, the usual solution to decouple a child process fully from its parent (“daemonize”) involves intentionally forking and having the intermediate process exit so that the daemon is immediately a child ofinit.Edit: If you’re in shell and want to achieve this effect, try
(subprocess &). The parenthesis create a subshell which executessubprocessin the background and then immeidately exits.