I have used Jenkins for build and deploy my artifacts to server. After deploying files I stopped service by using kill command
kill -9 'pgrep -f service name'
note that the service killed but jenkins job fail with status code -1 although this command works fine when I use it at shell of linux server without jenkins
Please help me why I get -1 exit status? and how I can kill process at linux server through jenkins job without failure ?
Edit : the below logs which appears after adding /bin/bash -x to my script:
#/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
Edit : the output of command ps -ef | grep service-name
is :
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
--- the output of trial script :
#/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 ```
CodePudding user response:
If you want to kill any process whose full command line match service-name
you should change your script:
#/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
Command pgrep
returns a list of process one per line.
In order to get pid list separated by a space and call kill
command once:
#/bin/bash
kill -9 $(pgrep -f service-name -d " ")
In order to view which process are selected by pgrep
use:
pgrep -a -f sevice-name
or
ps -ef | grep service-name
use man pgrep
to see all options
In your case, job is killed because pgrep
match the job script, so you should use a more specific pattern with the -x
parameters:
#/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