linux 远程杀掉进程
Posted 悠然现南山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux 远程杀掉进程相关的知识,希望对你有一定的参考价值。
转自http://blog.csdn.net/tengdazhang770960436/article/details/53906263
第一步:获取进程号
pid=$(ssh [email protected]$remote_host "ps -ef | grep ${tomcat_name} | grep -v grep | awk ‘{print $2}‘"| awk ‘{print $2}‘)
第二步:杀掉进程
ssh [email protected]$remote_host "kill -9 ${pid}"
注意:
杀掉远程进程跟杀掉本地进程不一样,杀掉本地进程只需要一句话就可以搞定了:
ps -ef | grep ${tomcat_name} | grep -v grep | awk ‘{print $2}‘|xargs kill -9
但是杀掉远程的进程不一样,因为过滤的结果会被整体返回,如果在远程执行这个杀掉进程的命令:
ssh [email protected]$remote_host "ps -ef | grep ${tomcat_name} | grep -v grep | awk ‘{print $2}‘ | xargs kill -9"
你会得到错误的提示:
kill process java -jar tomcat-8081 kill: can‘t find process "root" kill: can‘t find process "09:42" kill: can‘t find process "?" kill: can‘t find process "00:00:44" kill: can‘t find process "/opt/tools/jdk1.7.0_67/bin/java" kill: can‘t find process "-Djava.util.logging.config.file=/opt/tools/tomcat-8081/conf/logging.properties" kill: can‘t find process "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" kill: can‘t find process "-Ddisconf.env=rd" kill: can‘t find process "-Ddisconf.conf_server_host=192.168.100.15:8015" kill: can‘t find process "-Ddisconf.app=ync365" kill: can‘t find process "-Ddisconf.version=1.0.0" kill: can‘t find process "-Ddisconf.user_define_download_dir=./classes" kill: can‘t find process "-Ddisconf.enable.remote.conf=true" kill: can‘t find process "-Djdk.tls.ephemeralDHKeySize=2048" kill: can‘t find process "-Djava.endorsed.dirs=/opt/tools/tomcat-8081/endorsed" kill: can‘t find process "-classpath" kill: can‘t find process "/opt/tools/tomcat-8081/bin/bootstrap.jar:/opt/tools/tomcat-8081/bin/tomcat-juli.jar" kill: can‘t find process "-Dcatalina.base=/opt/tools/tomcat-8081" kill: can‘t find process "-Dcatalina.home=/opt/tools/tomcat-8081" kill: can‘t find process "-Djava.io.tmpdir=/opt/tools/tomcat-8081/temp" kill: can‘t find process "org.apache.catalina.startup.Bootstrap" kill: can‘t find process "start"
这是因为你执行的远程命令会返回如下结果:
root 7314 1 7 09:51 pts/0 00:00:17 /opt/tools/jdk1.7.0_67/bin/java -Djava.util.logging.config.file=/opt/tools/apache-tomcat-7.0.70/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/opt/tools/apache-tomcat-7.0.70/endorsed -classpath /opt/tools/apache-tomcat-7.0.70/bin/bootstrap.jar:/opt/tools/apache-tomcat-7.0.70/bin/tomcat-juli.jar -Dcatalina.base=/opt/tools/apache-tomcat-7.0.70 -Dcatalina.home=/opt/tools/apache-tomcat-7.0.70 -Djava.io.tmpdir=/opt/tools/apache-tomcat-7.0.70/temp org.apache.catalina.startup.Bootstrap start
然后 xargs kill -9 会把上面的所有的空格隔开的进程给删除一遍,实际上他们根本不是进程,只是一个进程下面返回的描述而已,我们真正需要的进程号是第二个 7314 这个进程,因此我们需要把返回的字符串再次过滤一遍,这样就能够得到真正的进程号了。
以上是关于linux 远程杀掉进程的主要内容,如果未能解决你的问题,请参考以下文章