{"id":119,"date":"2016-05-16T00:18:00","date_gmt":"2016-05-16T00:18:00","guid":{"rendered":"http:\/\/www.scm.keele.ac.uk\/staff\/stan\/?p=119"},"modified":"2016-05-16T00:18:00","modified_gmt":"2016-05-16T00:18:00","slug":"closing-sockets-without-killing-processes","status":"publish","type":"post","link":"https:\/\/machinemachines.org\/index.php\/2016\/05\/16\/closing-sockets-without-killing-processes\/","title":{"rendered":"How can I close a tcp\/ip socket without killing the owning process?"},"content":{"rendered":"<p><strong>Problem:<\/strong> a program has a socket open and you want to force it to close without killing the owning process.<\/p>\n<p><strong>Solution:<\/strong> get the file descriptor of the socket, debug the process and manually call <em><a href=\"http:\/\/linux.die.net\/man\/2\/close\">close<\/a><\/em> on the file descriptor.<\/p>\n<p>On Linux systems:<\/p>\n<ol>\n<li>Find the offending process: <code>netstat -np<\/code><\/li>\n<li>Find the socket file descriptor: <code>lsof -np $PID<\/code><\/li>\n<li>Debug the process: <code>gdb -p $PID<\/code><\/li>\n<li>Close the socket: <code>call close($FD)<\/code><\/li>\n<li>Close the debugger: <code>quit<\/code><\/li>\n<li>Profit.<\/li>\n<\/ol>\n<p>From <a href=\"http:\/\/superuser.com\/a\/668155\">here<\/a>.<\/p>\n<p>On OSX the incantations are differently formed:<\/p>\n<ol>\n<li>Find the offending process and file descriptor: <code> lsof -ni TCP<\/code><\/li>\n<li>Debug the process: <code>lldb -p $PID<\/code><\/li>\n<li>Close the socket: <code>call (int)close($FD)<\/code><\/li>\n<li>Close the debugger: <code>quit<\/code><\/li>\n<li>Profit.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Problem: a program has a socket open and you want to force it to close without killing the owning process. Solution: get the file descriptor of the socket, debug the process and manually call close on the file descriptor. On Linux systems: Find the offending process: netstat -np Find the socket file descriptor: lsof -np&hellip;&nbsp;<a href=\"https:\/\/machinemachines.org\/index.php\/2016\/05\/16\/closing-sockets-without-killing-processes\/\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">How can I close a tcp\/ip socket without killing the owning process?<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","iawp_total_views":789,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"featured_image_src":null,"author_info":{"display_name":"as","author_link":"https:\/\/machinemachines.org\/index.php\/author\/stan\/"},"_links":{"self":[{"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/comments?post=119"}],"version-history":[{"count":0,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/posts\/119\/revisions"}],"wp:attachment":[{"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/media?parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/categories?post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinemachines.org\/index.php\/wp-json\/wp\/v2\/tags?post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}