问题
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.
来源:https://stackoverflow.com/questions/21530126/how-to-kill-a-process-on-a-remote-server-using-python-paramiko