第一步:获取进程号

pid=$(ssh root@$remote_host "ps -ef | grep ${tomcat_name} | grep -v grep | awk  '{print $2}'"| awk '{print $2}')

第二步:杀掉进程

ssh root@$remote_host "kill -9 ${pid}"

注意:

杀掉远程进程跟杀掉本地进程不一样,杀掉本地进程只需要一句话就可以搞定了:

ps -ef | grep ${tomcat_name} | grep -v grep | awk  '{print $2}'|xargs kill -9

但是杀掉远程的进程不一样,因为过滤的结果会被整体返回,如果在远程执行这个杀掉进程的命令:

ssh root@$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 这个进程,因此我们需要把返回的字符串再次过滤一遍,这样就能够得到真正的进程号了。

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐