diff --git a/main/dnsmgr.c b/main/dnsmgr.c
index 1a5fd154e278a3494b0caa14609d6e5253ea0903..bfba4714d9aa7c318669f80bf7b44322f9059837 100644
--- a/main/dnsmgr.c
+++ b/main/dnsmgr.c
@@ -408,6 +408,26 @@ static struct ast_cli_entry cli_reload = AST_CLI_DEFINE(handle_cli_reload, "Relo
 static struct ast_cli_entry cli_refresh = AST_CLI_DEFINE(handle_cli_refresh, "Performs an immediate refresh");
 static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the DNS manager status");
 
+static void dnsmgr_shutdown(void)
+{
+	ast_cli_unregister(&cli_reload);
+	ast_cli_unregister(&cli_status);
+	ast_cli_unregister(&cli_refresh);
+
+	/* Destroy refresh thread. */
+	ast_mutex_lock(&refresh_lock);
+	if (refresh_thread != AST_PTHREADT_NULL) {
+		/* wake up the thread so it will exit */
+		pthread_cancel(refresh_thread);
+		pthread_kill(refresh_thread, SIGURG);
+		pthread_join(refresh_thread, NULL);
+		refresh_thread = AST_PTHREADT_NULL;
+	}
+	ast_mutex_unlock(&refresh_lock);
+
+	ast_sched_context_destroy(sched);
+}
+
 int dnsmgr_init(void)
 {
 	if (!(sched = ast_sched_context_create())) {
@@ -417,6 +437,9 @@ int dnsmgr_init(void)
 	ast_cli_register(&cli_reload);
 	ast_cli_register(&cli_status);
 	ast_cli_register(&cli_refresh);
+
+	ast_register_atexit(dnsmgr_shutdown);
+
 	return do_reload(1);
 }
 
diff --git a/main/event.c b/main/event.c
index 4c231fe91f2d9b5d13ec43649b98d391fd12162a..2c4670b52e2930cc876be1a2702e14cecc5235a0 100644
--- a/main/event.c
+++ b/main/event.c
@@ -1817,6 +1817,8 @@ static void event_shutdown(void)
 	struct ast_event_sub *sub;
 	int i;
 
+	ast_cli_unregister_multiple(event_cli, ARRAY_LEN(event_cli));
+
 	if (event_dispatcher) {
 		event_dispatcher = ast_taskprocessor_unreference(event_dispatcher);
 	}