stop Tomcat instances

Hello.

im unable to stop Tomcat instances with Ansible.
in the shell, i just need to type /etc/init.d/tomcat7_stop instance01

i found this .yml file and it’s written

  • name: stop tomcat
    service: name=tomcat state=stopped pattern={{instance}} args={{instance}}

but i don’t know what i supposed to fill in the “pattern” and in “args”

thanks

​"pattern" is a literal pattern on which you would grep on a full ps output.
That could be 'java', but you probably want to note something more
specific, depending on the full command line​ which starts your tomcat.

Given that tomcat sometimes doesn't stop, as in hangs or as in takes a long
time to stop, I combine this with a custom module i wrote, that has
different options to check on a process, whether it stopped, and to kill it
nicely, or later on not so nicely:

  https://github.com/ginsys/ansible-plugins/blob/devel/library/check_process

HTH,

Serge

not sure i quite understand…

when i did vi /etc/init.d/tomcat7_stop instance01 … this is how it stops the service:

instance01)
su tomcat7_01 -c “/var/tomcat7_instances/instance01/bin/shutdown.sh”
pkill -9 -u tomcat7_01
;;

and when i type ps -aux | grep java … the output is:

Warning: bad syntax, perhaps a bogus ‘-’? See /usr/share/doc/procps-3.2.8/FAQ
1013 1604 0.0 4.7 2902916 283460 ? Sl Sep23 109:22 /usr/java/jdk/bin/java -Djava.util.logging.config.file=/var/tomcat7_instances/instance04/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512M -Xmx512M -XX:MaxPermSize=256M -Dcom.sun.management.jmxremote -Duser.language=en -Duser.country=US -Duser.region= -Djava.endorsed.dirs=/var/tomcat7_instances/instance04/endorsed -classpath /var/tomcat7_instances/instance04/bin/bootstrap.jar:/var/tomcat7_instances/instance04/bin/tomcat-juli.jar -Dcatalina.base=/var/tomcat7_instances/instance04 -Dcatalina.home=/var/tomcat7_instances/instance04 -Djava.io.tmpdir=/var/tomcat7_instances/instance04/temp org.apache.catalina.startup.Bootstrap start
1010 5243 0.1 10.4 3735296 624772 ? Sl Dec10 1:41 /usr/java/jdk/bin/java -Djava.util.logging.config.file=/var/tomcat7_instances/instance01/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512M -Xmx1024M -XX:MaxPermSize=256M -Dcom.sun.management.jmxremote -Duser.language=en -Duser.country=US -Duser.region= -Djava.endorsed.dirs=/var/tomcat7_instances/instance01/endorsed -classpath /var/tomcat7_instances/instance01/bin/bootstrap.jar:/var/tomcat7_instances/instance01/bin/tomcat-juli.jar -Dcatalina.base=/var/tomcat7_instances/instance01 -Dcatalina.home=/var/tomcat7_instances/instance01 -Djava.io.tmpdir=/var/tomcat7_instances/instance01/temp org.apache.catalina.startup.Bootstrap start
rad 5570 58.1 1.3 2767368 81252 ? Sl 19:50 0:04 java -Dlog_file_name=reporter_packager_stats -Dblade_name=MGMT -Dcomp_name=Feedback -Dext.prop.dir=/usr/share/common/Properties -jar reports_packager.jar 5555 MGMT stats
1016 5618 78.7 1.1 2692084 69384 ? Sl 19:50 0:03 java -Dlog_file_name=reporter_packager_vthsBlackMd5info_amw -Dblade_name=AMW -Dcomp_name=Feedback -Dext.prop.dir=/usr/share/common/Properties -jar reports_packager.jar 8888 AMW vthsBlackMd5info
root 5636 0.0 0.0 103244 820 pts/0 R+ 19:50 0:00 grep java
1010 12770 2.0 12.8 3800328 770416 ? Sl Dec10 36:19 /usr/java/jdk/bin/java -Djava.util.logging.config.file=/var/tomcat7_instances/instance01/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512M -Xmx1024M -XX:MaxPermSize=256M -Dcom.sun.management.jmxremote -Duser.language=en -Duser.country=US -Duser.region= -Djava.endorsed.dirs=/var/tomcat7_instances/instance01/endorsed -classpath /var/tomcat7_instances/instance01/bin/bootstrap.jar:/var/tomcat7_instances/instance01/bin/tomcat-juli.jar -Dcatalina.base=/var/tomcat7_instances/instance01 -Dcatalina.home=/var/tomcat7_instances/instance01 -Djava.io.tmpdir=/var/tomcat7_instances/instance01/temp org.apache.catalina.startup.Bootstrap start
1011 16122 8.9 14.5 4490312 871212 ? Sl Dec10 133:32 /usr/java/jdk/bin/java -Djava.util.logging.config.file=/var/tomcat7_instances/instance02/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512M -Xmx1536M -XX:MaxPermSize=256M -Dcom.sun.management.jmxremote -Duser.language=en -Duser.country=US -Duser.region= -Djava.endorsed.dirs=/var/tomcat7_instances/instance02/endorsed -classpath /var/tomcat7_instances/instance02/bin/bootstrap.jar:/var/tomcat7_instances/instance02/bin/tomcat-juli.jar -Dcatalina.base=/var/tomcat7_instances/instance02 -Dcatalina.home=/var/tomcat7_instances/instance02 -Djava.io.tmpdir=/var/tomcat7_instances/instance02/temp org.apache.catalina.startup.Bootstrap start
1012 28137 0.4 15.3 4315724 919084 ? Sl Sep28 527:26 /usr/java/jdk/bin/java -Djava.util.logging.config.file=/var/tomcat7_instances/instance03/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512M -Xmx1536M -XX:MaxPermSize=256M -Dcom.sun.management.jmxremote -Duser.language=en -Duser.country=US -Duser.region= -Djava.endorsed.dirs=/var/tomcat7_instances/instance03/endorsed -classpath /var/tomcat7_instances/instance03/bin/bootstrap.jar:/var/tomcat7_instances/instance03/bin/tomcat-juli.jar -Dcatalina.base=/var/tomcat7_instances/instance03 -Dcatalina.home=/var/tomcat7_instances/instance03 -Djava.io.tmpdir=/var/tomcat7_instances/instance03/temp org.apache.catalina.startup.Bootstrap start

am i supposed to fill in pattern like this:

  • name: stop tomcat
    service: name=tomcat state=stopped pattern={{java}} args={{java}}

thanks

I would also point out that "/etc/init.d/tomcat7_stop instance01"
does not look like a standard init script, this will not work with the
service module.

I've used multi instance tomcat scripts before but they were either:
/etc/init.d/tomcat7_instance01 stop
works using tomcat7_instance01 as service name

or
/etc/init.d/tomcat7 stop instance01
which is supported by the arguments parameter arguments=instance01

i was able to run it like this:

  • name: stop tomcat
    sudo: yes
    command: /etc/init.d/tomcat7_stop instance01

but failed to run the second command: pkill -9 -u tomcat7_01
received this output:

TASK: [stop tomcat] ***********************************************************
failed: [194...*] => {“changed”: true, “cmd”: [“/etc/init.d/tomcat7_stop”, “instance01”], “delta”: “0:00:01.090791”, “end”: “2014-12-11 20:30:35.019231”, “rc”: 1, “start”: “2014-12-11 20:30:33.928440”}
stderr: Dec 11, 2014 8:30:34 PM org.apache.catalina.startup.Catalina stopServer
SEVERE: Catalina.stop:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.(Unknown Source)
at java.net.Socket.(Unknown Source)
at org.apache.catalina.startup.Catalina.stopServer(Catalina.java:499)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.stopServer(Bootstrap.java:371)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:458)
stdout: Using CATALINA_BASE: /var/tomcat7_instances/instance01
Using CATALINA_HOME: /var/tomcat7_instances/instance01
Using CATALINA_TMPDIR: /var/tomcat7_instances/instance01/temp
Using JRE_HOME: /usr/java/jdk
Using CLASSPATH: /var/tomcat7_instances/instance01/bin/bootstrap.jar:/var/tomcat7_instances/instance01/bin/tomcat-juli.jar

FATAL: all hosts have already failed – aborting

how could i write it the write way?
or is it better to fill the previous command? tomcat state=stopped pattern={{instance}} args={{instance}}

thanks!

Hi Sharon,

As this is a user-level question, can you please continue this over on ansible-project list?

Thanks!