LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2006, 09:39 AM   #1
Black Chaos
LQ Newbie
 
Registered: Jan 2005
Posts: 15

Rep: Reputation: 0
How to prevent zombie process?


shell_script.sh
Code:
#!/bin/bash
cd /var/www/html/
php somescript.php &
php otherscript.php &
exit 0
When I run that with just a ./shell_script.sh it runs just fine. But when I run it from cron I get a zombie process of sh. Would anyone know a fix for this?

Thanks.
 
Old 11-16-2006, 10:39 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
zombie processes are processes that have terminated, but have not had the wait() system call made for them (to process their error level and finalise removal from the process table).

As a programmer, you are expected to make sure that if your programs spawn children, that the wait() system call is made for them, just like you should always make sure you call close() on file handles.

The shell has a built-in command called "wait", which you could call like this:
Code:
#!/bin/bash
cd /var/www/html/
php somescript.php &
php otherscript.php &
wait  
# $? is the return status of the last process "wait"ed for
exit $?
This would make the shell wait until all child processes have finished and will call the wait system call for them.

If you want the script itself to run in the background (so you can close the terminal which you run it from), call it like this:
Code:
nohup shell_script.sh &
 
Old 11-17-2006, 09:22 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Yes. When you created your process-tree, the one spawned two children and then died. Those orphans became children of cron, which does not know to wait for them.

When a process dies, it becomes a "zombie" specifically so that its parent can collect ("reap") its ending-status. (Processes that have no live parent become orphans of "process #1 (init)", which immediately reaps them.)

With the change as-proposed, the first process launches two children, then waits for them to complete. It probably doesn't care about their ending status, but by waiting, it causes them to be reaped.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Zombie Process N|k0N Linux - Security 8 09-22-2007 02:01 AM
Zombie process - XMMS FeaRx Debian 3 11-04-2005 03:27 AM
zombie process ihatecomputers Linux - Software 2 03-24-2005 07:09 AM
Zombie Process Handling kghoshal Linux - Newbie 2 07-21-2003 09:10 AM
Zombie process and dialup pbm Linux - General 2 06-27-2002 10:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration