1

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 ```
  • Command kill returns -1 if something goes wrong and I guess that jenkins fails due to this error. Does user used to run jenkins has privilege to kill service name? Does pgrep found any process to stop? You should try to save pid in variable and check if pgrep returns 1 that means that it found a process. – Lety Aug 14 '22 at 10:35
  • thanks for reply, there is no error appear only ([SSH] exit-status: -1) is appear and job fail, also jenkins has privilege as I've access linux server with jenkins user and enter kill command after pgrep to found process id and these commands worked successfully from shell without jenkins. so what else would cause this issue PLZ? – asmaa mahmoud Aug 14 '22 at 15:23
  • it depends on how you are using jenkins. If it is a freestyle job and you have write you build process using shell script, any command that return non 0 value is considered as error. here is a useful link: https://stackoverflow.com/questions/22814559/how-when-does-execute-shell-mark-a-build-as-failure-in-jenkins?rq=1 – Lety Aug 14 '22 at 16:15
  • yes It's freestyle job and it return -1 value when I use the command for kill process as mentioned at question, so why this command fail ? and how to use jenkins to kill process in the right way ? – asmaa mahmoud Aug 15 '22 at 07:56
  • In order to debug kill you should add -x to your bash script in order to view command execution in details, or use: `pid=$(pgrep -f servicename); echo "killing $pid"; kill -9 $pid;` that is equivalent to: `kill -9 $(pgrep -f servicename)` It could be the single quote the error. – Lety Aug 15 '22 at 08:49
  • I've try the command you mention and still the same error, the process killed but jenkins job get ` [SSH] completed [SSH] exit-status: -1` and failed, what would be the reason of job failure ? – asmaa mahmoud Aug 15 '22 at 09:28
  • I've edit the question with the logs appears after adding #/bin/bash -x – asmaa mahmoud Aug 15 '22 at 11:24
  • pgrep has found 3 process to kill, did you want to kill all of them? – Lety Aug 15 '22 at 11:34
  • yes I want, but when I do the same command at linux machine terminal it gives me one process ID do you ow is that possible ? – asmaa mahmoud Aug 15 '22 at 11:46

1 Answers1

0

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
Lety
  • 2,511
  • 21
  • 25
  • I've try your script but still get the same issue as below ```[SSH] script: #/bin/bash -x pgrep -f service-name | while read pid; do kill -9 $pid done [SSH] executing... channel stopped [SSH] completed [SSH] exit-status: -1 Build step 'Execute shell script on remote host using ssh' marked build as failure``` – asmaa mahmoud Aug 15 '22 at 12:09
  • did you see if the 3 process are what you want to kill? add in the script `pidwithname=$(pgrep -a -f service name); echo $pidwithname;` it could be possible that you kill the job if the service-name pattern is present in the command that jenkins run – Lety Aug 15 '22 at 12:18
  • when I've add that to my script it give me the 3 ids of the processes , but what you mean with that (it could be possible that you kill the job if the service-name pattern is present in the command that jenkins run) . would you explain more please – asmaa mahmoud Aug 15 '22 at 15:15
  • jenkins run your script, so it could be possible that the parent process contains service-name so you could kill the job process. Try your script with the ps command (I added in my answer) and add the output in your question just to view the 3 process and the relation between them – Lety Aug 15 '22 at 17:18
  • I've edit the question with the output of the command but still unable to know what is the reason of issue which make the job fail , would you help please – asmaa mahmoud Aug 16 '22 at 13:49
  • I add a test in my answer, try with this and post the log please. – Lety Aug 16 '22 at 15:40
  • I have add the output of test script – asmaa mahmoud Aug 17 '22 at 10:36
  • Update answer, as you can see, pgrep match job script and kill himself – Lety Aug 17 '22 at 11:30