我使用Jenkins构建工件并将其部署到服务器。在部署文件之后,我使用kill -9 'pgrep -f service name'命令停止了服务
请注意,该服务已终止,但jenkins作业在状态代码-1中失败,尽管当我在没有jenkins的linux服务器的shell上使用该命令时,此命令运行良好。
请帮我解释一下为什么我有-1的出境身份?以及如何通过jenkins作业在linux服务器上不出故障地杀死进程?
编辑:在我的脚本中添加/bin/bash -x之后出现的以下日志:
#/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的输出是:
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-审判脚本的输出:
#/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 ```发布于 2022-08-15 11:49:47
如果要终止命令行与service-name匹配的任何进程,则应更改脚本:
#/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命令一次:
#/bin/bash
kill -9 $(pgrep -f service-name -d " ")为了查看pgrep所选择的进程,请使用:
pgrep -a -f sevice-name或
ps -ef | grep service-name使用man pgrep查看所有选项
在您的示例中,由于pgrep与作业脚本匹配,所以作业被终止,因此您应该在-x参数中使用更具体的模式:
#/bin/bash
pgrep -xf "java -jar /root/service-name --spring.config.location=/root/service-name/application.properties" | while read pid; do
kill -9 $pid
donehttps://stackoverflow.com/questions/73350781
复制相似问题