Eugen
Eugen

Reputation: 8783

Jenkins job to kill process (Tomcat) over ssh

I am using a Jenkins job to run a few simple shell commands (over ssh, via the Jenkins SSH Plugin); the commands are supposed to shut down a running Tomcat server:

sudo /opt/tomcat/bin/catalina.sh stop
ps xu | awk '/[t]omcat/{print $2}' | xargs -r kill -9

The job executes fine and does terminate the Tomcat, but unfortunately it also fails; the full output is:

[SSH] executing pre build script:

sudo /opt/tomcat/bin/catalina.sh stop
ps xu | awk '/[t]omcat/{print $2}' | xargs kill -9
[SSH] exit-status: -1
Finished: FAILURE

Any idea why the exit code of the command if -1? I have tried several variations without any luck.

Thanks.

Upvotes: 1

Views: 3709

Answers (4)

redOctober13
redOctober13

Reputation: 3974

Can you just do pkill tomcat?

Upvotes: 0

ub_marco
ub_marco

Reputation: 158

The questions is a bit old but as I stumbled upon that, here's another suggestion.

ps xu | awk '/[t]omcat/{print $2}'

returns the running tomcat AND the awk process itself, see here

<user>     2370  0.0  0.0  26144  1440 pts/7    R+   10:51   0:00 awk /[t]omcat/{print $2}

The awk process instantly ends itself before running xargs on it, so one of the xargs has an exit code unequal 0.

Try running killall tomcat

Upvotes: 1

Mike Yockey
Mike Yockey

Reputation: 4593

I suspect that Jenkins doesn't like the no process killed that the kill command prints of it doesn't run. Try redirecting stdout to /dev/null.

Upvotes: 1

Roy
Roy

Reputation: 3624

You should examine the output of ps xu. Since kill will kill the processes sequentially, it may be the case that if there are multiple tomcat processes yielded by ps xu, the other ones will automatically terminate after the first one is terminated. Then kill attempts to terminate processes that no longer exist.

Upvotes: 1

Related Questions