Graceful shutdown support

I'm added some codes for graceful shutdown support, may be helpful.  :P