0

I have python script that connects to a remote server with lenny operating system. It runs a process in background using following line:

shell.send("cd /my/directory/; nohup ./exec_name > /dev/null 2>&1 &\n")

Then after some other codes, it sends a kill command to the server to stop process execution; here's the code:

shell.send("kill -9 process_pid \n")

It returns no error, but doesn't kill the process and it's still alive in the system. I also tried killall -9 process_name, but I got the same result. Any help?

For more information, here's the code for connecting to the server:

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()

I should mention that the user has root privileges.

EDIT 1:

I forgot to say that I tried this:

ssh.exec_command("kill -9 process_pid \n")

But it returned this error:

SSHClient is not active right now.

Edit 2:

As @JimB mentioned in the comment, the problem about exec_command 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.

Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131
  • But you are sending the real process id right? Not the string 'process_pid'? – Wolf Feb 03 '14 at 14:49
  • I think so, but let me double-check! – Zeinab Abbasimazar Feb 03 '14 at 14:51
  • Yes, it's actually the real process ID. – Zeinab Abbasimazar Feb 03 '14 at 15:02
  • 2
    Not really an answer to your question, but have you considered using fabric instead? It makes using paramiko much easier :) – Wolph Feb 03 '14 at 15:11
  • "I should mention that the user has root privileges." That is actually bad, I think. It could have appropriate `sudo` privileges, but I would consider it dangerous too. In this context, I think, no special power is needed, as user is allowed to kill processes that he owns. At least on every linux system I worked on. Have you tried issuing an equivalent `ssh` remote command from another system by hand ( something like `ssh name@remote "kill pid"`)? The environment set up for interactive ssh, and non-interactive differ a bit on the receiving end. It might turn out to be not python related. – luk32 Feb 03 '14 at 15:21
  • @Wolph, I have tried `fabric` before, but it does not sufficient for my program. There are some constraints that interfere my work. – Zeinab Abbasimazar Feb 03 '14 at 15:31
  • @luk32, I tried to make an SSH connection from another system and kill the process by the above command and it was successful. – Zeinab Abbasimazar Feb 03 '14 at 15:31
  • @ZeinabAbbasi You are ambiguous. Did you issue a remote command, or used interactive ssh connection? There is (or can be) little a difference between interactive and non-interactive ssh mode. – luk32 Feb 03 '14 at 15:38
  • @luk32, I'm sorry, maybe I just misunderstood your comment. Actually I am talking about a remote command. But I also tried an interactive SSH connection. The former was unsuccessful and the later was successful. I hope I was clear. – Zeinab Abbasimazar Feb 03 '14 at 15:53
  • 1
    What is the output of, and exit_status of `exec_command`? Also, 'SSHClient is not active right now.' means that you're using a stale transport. Get a transport from your current connection. – JimB Feb 03 '14 at 21:37
  • @JimB, There is no error code; the exact phrase is this: `Exception paramiko.SSHException: SSHException('SSH session not active',)`. But thank you for denoting about stale transport. I made a new temporary SSH connection and it successfully killed the process; but I still for a better way to do this, don't you have anything in mind? – Zeinab Abbasimazar Feb 04 '14 at 05:58
  • Don't use shell unless you absolutely have to, exec_command is much easier to reason about. You're still not calling get_pty on the same channel as invoke_shell. If the exec_command is failing, and you have to reconnect, then something else went wrong; check your logs. – JimB Feb 04 '14 at 16:33
  • @JimB, thanks for your notes. I will debug the code again. – Zeinab Abbasimazar Feb 05 '14 at 05:50

0 Answers0