首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >退出-状态:-1当通过jenkins杀死进程时

退出-状态:-1当通过jenkins杀死进程时
EN

Stack Overflow用户
提问于 2022-08-14 10:10:37
回答 1查看 274关注 0票数 1

我使用Jenkins构建工件并将其部署到服务器。在部署文件之后,我使用kill -9 'pgrep -f service name'命令停止了服务

请注意,该服务已终止,但jenkins作业在状态代码-1中失败,尽管当我在没有jenkins的linux服务器的shell上使用该命令时,此命令运行良好。

请帮我解释一下为什么我有-1的出境身份?以及如何通过jenkins作业在linux服务器上不出故障地杀死进程?

编辑:在我的脚本中添加/bin/bash -x之后出现的以下日志:

代码语言:javascript
复制
 #/bin/bash -x
pid=$(pgrep -f service-name); echo "killing $pid"; kill -9 $pid;

[SSH] executing...
killing 13664
16924
16932

[SSH] completed
[SSH] exit-status: -1

Build step 'Execute shell script on remote host using ssh' marked build as failure
Email was triggered for: Failure - Any

编辑:命令ps -ef | grep service-name的输出是:

代码语言:javascript
复制
ps -ef | grep service-name

[SSH] executing...
channel stopped
user     11786 11782  0 15:28 ?        00:00:00 bash -c  ps -ef | grep service-name
user     11799 11786  0 15:28 ?        00:00:00 grep service-name
root     19981 11991  0 Aug15 pts/1    00:02:53 java -jar /root/service-name /spring.config.location=/root/service-name/application.properties

[SSH] completed

-审判脚本的输出:

代码语言:javascript
复制
#/bin/bash -x
ps -ef | grep service-name

pgrep -f "java -jar /root/service-name --spring.config.location=/root/service-name/application.properties" | while read pid; do
   ps -ef | grep $pid  
   kill -9 $pid
   echo "kill command returns $?"
done

[SSH] executing...
channel stopped
root     56980 11991 37 11:03 pts/1    00:00:33 java -jar /root/service-name --spring.config.location=/root/service-name/application.properties
root     57070 57062  0 11:05 ?        00:00:00 bash -c  #/bin/bash -x ps -ef | grep service-name  pgrep -f "java -jar /root/service-name --spring.config.location=/root/service-name/application.properties" | while read pid; do    ps -ef | grep $pid      kill -9 $pid    echo "kill command returns $?" done
root     57079 57070  0 11:05 ?        00:00:00 grep service-name
root     56980 11991 37 11:03 pts/1    00:00:33 java -jar /root/service-name --spring.config.location=/root/service-name/application.properties
root     57083 57081  0 11:05 ?        00:00:00 grep 56980
kill command returns 0
root     57070 57062  0 11:05 ?        00:00:00 bash -c  #/bin/bash -x ps -ef | grep service-name  pgrep -f "java -jar /root/service-name --spring.config.location=/root/service-name/application.properties" | while read pid; do    ps -ef | grep $pid      kill -9 $pid    echo "kill command returns $?" done
root     57081 57070  0 11:05 ?        00:00:00 bash -c  #/bin/bash -x ps -ef | grep service-name  pgrep -f "java -jar /root/service-name --spring.config.location=/root/service-name/application.properties" | while read pid; do    ps -ef | grep $pid      kill -9 $pid    echo "kill command returns $?" done
root     57085 57081  0 11:05 ?        00:00:00 grep 57070
kill command returns 0
root     57081     1  0 11:05 ?        00:00:00 bash -c  #/bin/bash -x ps -ef | grep service-name  pgrep -f "java -jar /root/service-name --spring.config.location=/root/service-name/application.properties" | while read pid; do    ps -ef | grep $pid      kill -9 $pid    echo "kill command returns $?" done
root     57086 57081  0 11:05 ?        00:00:00 ps -ef
root     57087 57081  0 11:05 ?        00:00:00 grep 57081

[SSH] completed
[SSH] exit-status: -1 ```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-15 11:49:47

如果要终止命令行与service-name匹配的任何进程,则应更改脚本:

代码语言:javascript
复制
#/bin/bash
pgrep -f service-name | while read pid; do
   ps -ef | grep $pid   # so you can see what you are going to kill
   kill -9 $pid
done

命令pgrep每行返回一个进程列表。

为了获得由空格分隔的pid列表并调用kill命令一次:

代码语言:javascript
复制
#/bin/bash
kill -9 $(pgrep -f service-name -d " ")

为了查看pgrep所选择的进程,请使用:

代码语言:javascript
复制
pgrep -a -f sevice-name

代码语言:javascript
复制
ps -ef | grep service-name

使用man pgrep查看所有选项

在您的示例中,由于pgrep与作业脚本匹配,所以作业被终止,因此您应该在-x参数中使用更具体的模式:

代码语言:javascript
复制
#/bin/bash 
pgrep -xf "java -jar /root/service-name --spring.config.location=/root/service-name/application.properties" | while read pid; do
   kill -9 $pid
done
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73350781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档