Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kill remote process by ssh [closed]

Tags:

linux

shell

ssh

I can't directly access target host, need ssh as a proxy.

How can I kill a process from local use ssh ? I try this:

ssh root@$center "ssh root@$ip \"ps -ef|grep -v grep|grep $target_dir/$main|awk '{print \$2}'|xargs kill\""

it got error:

kill: can't find process "root"

And how to avoid error where process not exist ?

like image 577
sharewind Avatar asked Feb 22 '14 05:02

sharewind


People also ask

What is kill PID command?

Description. The kill command sends a signal (by default, the SIGTERM signal) to a running process. This default action normally stops processes. If you want to stop a process, specify the process ID (PID) in the ProcessID variable.


2 Answers

Suppose your process' name is name, then you can try something like this:

ssh hostname "pid=\$(ps aux | grep 'name' | awk '{print \$2}' | head -1); echo \$pid |xargs kill"
like image 51
Jayesh Bhoi Avatar answered Oct 26 '22 23:10

Jayesh Bhoi


Use pkill -f to easily kill a process by matching its command-line.

ssh root@$center ssh root@$ip pkill -f "$target_dir/$main"
like image 45
John Kugelman Avatar answered Oct 26 '22 22:10

John Kugelman