Index: headers.h
===================================================================
--- headers.h	(revision 148)
+++ headers.h	(revision 149)
@@ -99,6 +99,7 @@
 #include <net/if.h>
 #include <ifaddrs.h>
 #include <fcntl.h>
+#include <signal.h>
 #endif
 #ifdef BSD
 #include <netinet/in.h>
Index: util.h
===================================================================
--- util.h	(revision 148)
+++ util.h	(revision 149)
@@ -140,6 +140,7 @@
 extern bool fPrintToConsole;
 extern bool fPrintToDebugger;
 extern char pszSetDataDir[MAX_PATH];
+extern bool fRequestShutdown;
 extern bool fShutdown;
 extern bool fDaemon;
 extern bool fCommandLine;
Index: net.cpp
===================================================================
--- net.cpp	(revision 148)
+++ net.cpp	(revision 149)
@@ -1163,6 +1163,8 @@
         // Wait and allow messages to bunch up
         vnThreadsRunning[2]--;
         Sleep(100);
+        if (fRequestShutdown)
+            Shutdown(NULL);
         vnThreadsRunning[2]++;
         if (fShutdown)
             return;
Index: init.cpp
===================================================================
--- init.cpp	(revision 148)
+++ init.cpp	(revision 149)
@@ -10,7 +10,6 @@
 
 
 
-
 //////////////////////////////////////////////////////////////////////////////
 //
 // Shutdown
@@ -57,11 +56,16 @@
     }
 }
 
+void HandleSIGTERM(int)
+{
+    fRequestShutdown = true;
+}
 
 
 
 
 
+
 //////////////////////////////////////////////////////////////////////////////
 //
 // Start
@@ -130,6 +134,14 @@
 #ifndef __WXMSW__
     umask(077);
 #endif
+#ifndef __WXMSW__
+    // Clean shutdown on SIGTERM
+    struct sigaction sa;
+    sa.sa_handler = HandleSIGTERM;
+    sigemptyset(&sa.sa_mask);
+    sa.sa_flags = 0;
+    sigaction(SIGTERM, &sa, NULL);
+#endif
 
     //
     // Parameters
Index: util.cpp
===================================================================
--- util.cpp	(revision 148)
+++ util.cpp	(revision 149)
@@ -11,6 +11,7 @@
 bool fPrintToConsole = false;
 bool fPrintToDebugger = false;
 char pszSetDataDir[MAX_PATH] = "";
+bool fRequestShutdown = false;
 bool fShutdown = false;
 bool fDaemon = false;
 bool fCommandLine = false;
