Skip to content

Instantly share code, notes, and snippets.

@FooBarWidget
Created January 21, 2015 13:09
Show Gist options
  • Save FooBarWidget/55d76c9660f6dda7370c to your computer and use it in GitHub Desktop.
Save FooBarWidget/55d76c9660f6dda7370c to your computer and use it in GitHub Desktop.
Zombie process test
#!/usr/bin/env python3 -u
# This shell script simulates a CGI web server.
# Save as 'testprocess.py' and run with:
# docker run -i --rm ubuntu:14.04 python3 -u < testprocess.py
import os, signal, time
# The web server spawns a CGI script, which in turn spawns a long-running 'grep',
# simulated here by 'sleep 999'.
print("Spawning CGI script")
pid = os.fork()
if pid == 0:
# Child
os.setpgrp()
os.execlp("bash", "bash", "-c", "set -e && sleep 999")
print("CGI script running as PID %d" % pid)
time.sleep(1)
print()
print("Current processes:")
os.system("ps aux")
print()
print("Terminating CGI script")
os.killpg(pid, signal.SIGTERM)
os.waitpid(pid, 0)
time.sleep(1)
print()
print("Current processes:")
os.system("ps aux")
print()
print("See the [sleep] <defunct> there? That's a zombie process.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment