title: Identify and close an SSH tunnel from R
body: <p>I need to connect in R to a DB that uses an SSH tunnel. Based on googling SO tells me that I <a href="https://stackoverflow.com/questions/19551095/r-ssh-tunnel-mysql">have to create an SSH tunnel first</a>, then connect to my DB, but there is little on how to safely close the tunnel after executing my SQL.</p>
  
  <p>Example:</p>
  
  <pre><code>if(!require(RPostgreSQL)) install.packages("RPostgreSQL")
  
  # Create tunnel
  myssh&lt;-"ssh -f &lt;server_user&gt;@&lt;server_ip&gt;  -L &lt;unused_local_port&gt;:localhost:&lt;database_remote_port&gt; -N"
  system(myssh)
  
  # Connect to DB
  drv  &lt;- dbDriver("PostgreSQL")
  conn &lt;- dbConnect(
    drv,
    host = "127.0.0.1", user = "postgres",
    password = "", dbname = "mydb",
  )
  
  # How to close tunnel?
  </code></pre>
  
  <p><strong>How to correctly detect and close the SSH tunnel created by R?</strong></p>
  
  <p>PS I'm on Windows!</p>