title: How to kill a process on a remote server using python paramiko
body: <p>I have python script that connects to a remote server with lenny operating system. It runs a process in background using following line:</p>
  
  <pre><code>shell.send("cd /my/directory/; nohup ./exec_name &gt; /dev/null 2&gt;&amp;1 &amp;\n")
  </code></pre>
  
  <p>Then after some other codes, it sends a <code>kill</code> command to the server to stop process execution; here's the code:</p>
  
  <pre><code>shell.send("kill -9 process_pid \n")
  </code></pre>
  
  <p>It returns no error, but doesn't kill the process and it's still alive in the system. I also tried <code>killall -9 process_name</code>, but I got the same result. Any help?</p>
  
  <p>For more information, here's the code for connecting to the server:</p>
  
  <pre><code>ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(hostname = "host_ip", username = "un"], password = "up")
  channel = ssh.get_transport().open_session()
  pty = channel.get_pty()
  shell = ssh.invoke_shell()
  </code></pre>
  
  <p>I should mention that the user has root privileges.</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>I forgot to say that I tried this:</p>
  
  <pre><code>ssh.exec_command("kill -9 process_pid \n")
  </code></pre>
  
  <p>But it returned this error:</p>
  
  <pre><code>SSHClient is not active right now.
  </code></pre>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>As @JimB mentioned in the comment, the problem about <code>exec_command</code> is that the transport has been staled. I made a temporary SSH connection and killed the process by that; it was successful. But I'm still searching for a better way. </p>