开发者

Avoid Zombie processes from the shell?

开发者 https://www.devze.com 2023-03-18 19:02 出处:网络
I\'m writing a couple shell scripts which do a few things: main.sh run under screen, it sets some variables, then calls: start.sh in the foreground (no &)

I'm writing a couple shell scripts which do a few things:

  1. main.sh run under screen, it sets some variables, then calls: start.sh in the foreground (no &)

  2. start.sh is run, it sets some Java specific variables, and launches a Java process (again in the foreground)

  3. Java runs and does its thing until receiving a 'quit' command. (also in the 开发者_开发知识库foreground

    So my process tree then looks like:

  - main.sh
    \- start.sh
      \- java

The problem is occasionally the Java program gets in a funky state where it doesn't honor your 'quit' command, and sits there merrily doing its thing. I have been saving the process ID of this Java process to a PID file, and sending -STOP(17?) -TERM(15) and KILL(9) signals to these java processes, but nothing seems to properly kill them off.

I can kill -9 the 'start.sh' process, which does kill off the process tree, but leaves the Java process in the dreaded defunct (zombie) state, to which the only solution is to reboot the server.

I was wondering if anyone had any input on how I might avoid getting into these situations, or if there is any Linux/shell feature I am missing that might prevent these zombie processes from occurring. I should also add I cannot modify the Java application code as it is a proprietary application, and I do not have the source code available.

This is running on Fedora 14 with the 2.6.35.13-92.fc14.x86_64 kernel.

Thanks in advance,


Zombie are dead. If you want them rest in place, your parents (main.sh / start.sh) have to wait / waitpid it.


Have you ever seen one of those movies where the wizard waves his wand and everyone around him freezes like a statue, and waves it again and they continue as before? That's want kill -STOP does and there's nothing a process can do to protect itself from it.

I'm surprised that your java processes become zombies because zombies are processes that have finished and are just waiting around to tell their parent process that they have finished. But the parent has to be checking. Most shells do check regularly.

If you've already killed the parent process, then the child process "changes parent" and becomes a child process of init (PID 1). Normally, init always pays attention to processes telling it they've finished.

If you kill -STOP the parent shell, it won't be able to respond to child processes notifying that they've finished so the child will become a zombie until you restart the parent with kill -CONT.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号