0

I've to replicate behaviour of CTRL+C using shell script to turned down tomcat application server. Hence wrote following code in Jenkins pipeline script -

sh '''pgrep -fl catalina | awk '{print $1}' | xargs kill -SIGINT'''

But it does not turn application down. However if I executed same command from terminal, it turns down the application -

pgrep -fl catalina | awk '{print $1}' | xargs kill -SIGINT

Can you please help to understand what is wrong with shell script code?

3
  • Why are you escaping the $? You can see that it works when you don't. Run it that way. May 18, 2020 at 5:35
  • Yes, you're correct. $ need not to be escaped. And run it with that but still server did not go down.
    – Alpha
    May 18, 2020 at 5:47
  • The $ would need to be escaped as its otherwise unquoted. I think Nasir didn't see that the whole pipeline was quoted.
    – Kusalananda
    May 18, 2020 at 6:29

2 Answers 2

1

The two commands are not the same. The first command tries to run a script called, literally, pgrep -fl catalina | awk {print with the argument } | xargs kill -SIGINT (assuming $1 is empty in the calling shell). It should give you a "No such file or directory" error. It does this due to the quoting.

Triple single quotes in the shell is just an awkward way of starting an ordinary single quote (the first two single quotes would be an empty string). And a single quoted string can not contain a single quote. This means that {print $1} is unquoted in your command, so the shell expands the $1 (probably to nothing), and then splits the command on that space after print.

To run the pipeline with sh, use sh -c:

sh -c 'sh pgrep -fl catalina | awk "{ print \$1 }" | xargs kill -SIGINT'

Or, just use pkill:

pkill -f -SIGINT catalina

Or, you could use the shutdown.sh script for Tomcat, or catalina.sh stop, or service tomcat stop.

4
  • pkill -f -SIGINT catalina through shell script does not turn server down and sh -c 'sh pgrep -fl catalina | awk "{ print \$1 }" | xargs kill -SIGINT' giver compilation error - org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 2619: expecting '}', found 'sh pgrep -fl catalina | awk "{ print $1 }" | xargs kill -SIGINT' @ line 2619, column 47. sh -c 'sh pgrep
    – Alpha
    May 18, 2020 at 6:34
  • pkill -9 -f tomcat, sh ./shutdown.sh and sh ./catalina.sh stop' are not helping to achieve what CTRL+C` does. hence opted for kill -SIGINT
    – Alpha
    May 18, 2020 at 6:43
  • @TDHM Why do you put sh in front of shutdown.sh etc.? Also, have you tried service tomcat stop or similar commands (systemctl?) to stop the service?
    – Kusalananda
    May 18, 2020 at 6:47
  • Yes sh is not needed actually. systemctl asks for login details which makes things complicated in shell script(based on my knoweldge)
    – Alpha
    May 19, 2020 at 7:18
0

well, to begin with, you're making things unnecessarily complex.

It looks like you want to match the full command name 'catalina'. If that's the case, then I think you want to use the '-x' parameter. and, if you don't use the '-l', then you won't get a command name that then needs to be cut out with 'awk'.

pgrep -x catalina 

You can then interpolate the 'pgrep' command into the 'kill' command line.

sh 'kill -SIGINT `pgrep -x catalina` '

even better yet:

killall  -SIGINT --exact catalina                                  

As to why this works in one context and not in the other, it's not clear. Perhaps you're running as a different (non-root) user in the Jenkins Pipeline context and the correct (or root) user when you are in the terminal?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .