# Tomcat startup.sh script works from shell but not with ansible

**URL:** <https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530>\
**Category:** Ansible Project\
**Created:** [February 9, 2014, 8:25pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530 "2014-02-09T20:25:06Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alain\_Sahli](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Alain\_Sahli](https://forum.ansible.com/u/Alain_Sahli)\
**Post date:** [February 9, 2014, 8:25pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/1 "2014-02-09T20:25:06Z")

</div>

Hi!

I wrote a simple playbook that downloads, extracts and starts Tomcat. Here are the relevant parts of it:

- name: Download Tomcat 7.0.42  
get\_url: url=[http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.42/bin/apache-tomcat-7.0.42.tar.gz](http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.42/bin/apache-tomcat-7.0.42.tar.gz) dest=/home/tomcat/apache-tomcat-7.0.42.tar.gz

- name: Extract Tomcat  
command: chdir=/home/tomcat tar -xvzf apache-tomcat-7.0.42.tar.gz creates=/home/tomcat/apache-tomcat-7.0.42

- name: Change ownership of Tomcat installation  
file: path=/home/tomcat/apache-tomcat-7.0.42 owner=tomcat group=tomcat state=directory recurse=yes

- name: Configure Tomcat server  
template: src=tomcat-users.xml dest=/home/tomcat/apache-tomcat-7.0.42/conf/  
notify: restart tomcat

- name: Set JAVA\_HOME for tomcat  
lineinfile: dest=/home/tomcat/apache-tomcat-7.0.42/bin/setenv.sh line=‘export JAVA\_HOME=/opt/java/jdk1.7.0\_51’ create=yes state=present mode=0555

- name: Start Tomcat  
command: /home/tomcat/apache-tomcat-7.0.42/bin/startup.sh  
sudo: true  
sudo\_user: tomcat

- name: wait for tomcat to start  
wait\_for: port=8080

The wait step always times out because Tomcat doesn’t start. The strange thing is that when I execute the startup.sh script on the vm’s shell it works. And Ansible doesn’t complain it says that the step was executed successfully. Here’s the output with ‘vvvv’:

TASK: [tomcat | Start Tomcat] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\<127.0.0.1\> ESTABLISH CONNECTION FOR USER: vagrant

\<127.0.0.1\> EXEC [‘ssh’, ‘-tt’, ‘-vvv’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=60s’, ‘-o’, ‘ControlPath=/Users/asahli/.ansible/cp/ansible-ssh-%h-%p-%r’, ‘-o’, ‘Port=2222’, ‘-o’, ‘IdentityFile=/Users/asahli/.vagrant.d/insecure\_private\_key’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘User=vagrant’, ‘-o’, ‘ConnectTimeout=10’, ‘127.0.0.1’, “/bin/sh -c ‘mkdir -p /tmp/ansible-1391975068.22-50068957235035 && chmod a+rx /tmp/ansible-1391975068.22-50068957235035 && echo /tmp/ansible-1391975068.22-50068957235035’”]

\<127.0.0.1\> REMOTE\_MODULE command /home/tomcat/apache-tomcat-7.0.42/bin/startup.sh

\<127.0.0.1\> PUT /var/folders/vj/fw7zgzj93j371115rb1mtvv00000gn/T/tmpcOMgD0 TO /tmp/ansible-1391975068.22-50068957235035/command

\<127.0.0.1\> EXEC [‘ssh’, ‘-tt’, ‘-vvv’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=60s’, ‘-o’, ‘ControlPath=/Users/asahli/.ansible/cp/ansible-ssh-%h-%p-%r’, ‘-o’, ‘Port=2222’, ‘-o’, ‘IdentityFile=/Users/asahli/.vagrant.d/insecure\_private\_key’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘User=vagrant’, ‘-o’, ‘ConnectTimeout=10’, ‘127.0.0.1’, “/bin/sh -c ‘chmod a+r /tmp/ansible-1391975068.22-50068957235035/command’”]

\<127.0.0.1\> EXEC [‘ssh’, ‘-tt’, ‘-vvv’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=60s’, ‘-o’, ‘ControlPath=/Users/asahli/.ansible/cp/ansible-ssh-%h-%p-%r’, ‘-o’, ‘Port=2222’, ‘-o’, ‘IdentityFile=/Users/asahli/.vagrant.d/insecure\_private\_key’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘User=vagrant’, ‘-o’, ‘ConnectTimeout=10’, ‘127.0.0.1’, ‘/bin/sh -c 'sudo -k && sudo -H -S -p “[sudo via ansible, key=pqvlljvxxezlskhjkospaalznznwlzdy] password: " -u tomcat /bin/sh -c '”'“'echo SUDO-SUCCESS-pqvlljvxxezlskhjkospaalznznwlzdy; /usr/bin/python /tmp/ansible-1391975068.22-50068957235035/command'”'"''’]

\<127.0.0.1\> EXEC [‘ssh’, ‘-tt’, ‘-vvv’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=60s’, ‘-o’, ‘ControlPath=/Users/asahli/.ansible/cp/ansible-ssh-%h-%p-%r’, ‘-o’, ‘Port=2222’, ‘-o’, ‘IdentityFile=/Users/asahli/.vagrant.d/insecure\_private\_key’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘User=vagrant’, ‘-o’, ‘ConnectTimeout=10’, ‘127.0.0.1’, “/bin/sh -c ‘rm -rf /tmp/ansible-1391975068.22-50068957235035/ \>/dev/null 2\>&1’”]

changed: [tp-web-proxy] =\> {“changed”: true, “cmd”: [“/home/tomcat/apache-tomcat-7.0.42/bin/startup.sh”], “delta”: “0:00:00.006908”, “end”: “2014-02-09 19:44:28.235406”, “item”: “”, “rc”: 0, “start”: “2014-02-09 19:44:28.228498”, “stderr”: “”, “stdout”: “Using CATALINA\_BASE: /home/tomcat/apache-tomcat-7.0.42\nUsing CATALINA\_HOME: /home/tomcat/apache-tomcat-7.0.42\nUsing CATALINA\_TMPDIR: /home/tomcat/apache-tomcat-7.0.42/temp\nUsing JRE\_HOME: /opt/java/jdk1.7.0\_51\nUsing CLASSPATH: /home/tomcat/apache-tomcat-7.0.42/bin/bootstrap.jar:/home/tomcat/apache-tomcat-7.0.42/bin/tomcat-juli.jar”}

On IRC somebody gave me that hint: “something that tomcat wants (from your shell env) seems like it’s missing from running the command without a terminal started”. But I can’t find out what’s missing.

Does somebody know this problem?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![David\_Adams](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@David\_Adams](https://forum.ansible.com/u/David_Adams)\
**Post date:** [February 10, 2014, 12:36pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/2 "2014-02-10T12:36:56Z")

</div>

I can’t say off the top of my head what the problem is but here’s a way to troubleshoot it:

Add to setenv.sh: “env \> /home/tomcat/env.out.$(date +%H%M%S)”

Then run the ansible playbook, and start tomcat from the shell, and you’ll have two env.out.\* files in /home/tomcat. Compare them with diff (you might need to sort them first? I don’t know how predictable the order of env’s output is) or by sight. That should point you to what’s missing (most likely something in PATH or LD\_LIBRARY\_PATH).

-dave

---

<div class="post-metadata">

**Author:** ![Alain\_Sahli](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Alain\_Sahli](https://forum.ansible.com/u/Alain_Sahli)\
**Post date:** [February 10, 2014, 9:28pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/3 "2014-02-10T21:28:43Z")

</div>

I did it but it didn’t help me much. The following environment variables are different:

ansible: USERNAME=tomcat  
shell: USERNAME=root

ansible: LANG=C  
shell: LANG=en\_US

ansible:  
shell: LS\_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:_.tar=01;31:_.tgz=01;31:_.arj=01;31:_.taz=01;31:_.lzh=01;31:_.lzma=01;31:_.tlz=01;31:_.txz=01;31:_.zip=01;31:_.z=01;31:_.Z=01;31:_.dz=01;31:_.gz=01;31:_.lz=01;31:_.xz=01;31:_.bz2=01;31:_.bz=01;31:_.tbz=01;31:_.tbz2=01;31:_.tz=01;31:_.deb=01;31:_.rpm=01;31:_.jar=01;31:_.war=01;31:_.ear=01;31:_.sar=01;31:_.rar=01;31:_.ace=01;31:_.zoo=01;31:_.cpio=01;31:_.7z=01;31:_.rz=01;31:_.jpg=01;35:_.jpeg=01;35:_.gif=01;35:_.bmp=01;35:_.pbm=01;35:_.pgm=01;35:_.ppm=01;35:_.tga=01;35:_.xbm=01;35:_.xpm=01;35:_.tif=01;35:_.tiff=01;35:_.png=01;35:_.svg=01;35:_.svgz=01;35:_.mng=01;35:_.pcx=01;35:_.mov=01;35:_.mpg=01;35:_.mpeg=01;35:_.m2v=01;35:_.mkv=01;35:_.webm=01;35:_.ogm=01;35:_.mp4=01;35:_.m4v=01;35:_.mp4v=01;35:_.vob=01;35:_.qt=01;35:_.nuv=01;35:_.wmv=01;35:_.asf=01;35:_.rm=01;35:_.rmvb=01;35:_.flc=01;35:_.avi=01;35:_.fli=01;35:_.flv=01;35:_.gl=01;35:_.dl=01;35:_.xcf=01;35:_.xwd=01;35:_.yuv=01;35:_.cgm=01;35:_.emf=01;35:_.axv=01;35:_.anx=01;35:_.ogv=01;35:_.ogx=01;35:_.aac=00;36:_.au=00;36:_.flac=00;36:_.mid=00;36:_.midi=00;36:_.mka=00;36:_.mp3=00;36:_.mpc=00;36:_.ogg=00;36:_.ra=00;36:_.wav=00;36:_.axa=00;36:_.oga=00;36:_.spx=00;36:_.xspf=00;36:

ansible: SUDO\_COMMAND=/bin/sh -c echo SUDO-SUCCESS-ztvhjxfxdqkjmmjigsawnrwhamjttxvl; /usr/bin/python /tmp/ansible-1392065810.04-265771362994182/command

shell: SUDO\_COMMAND=/bin/su -c /home/tomcat/apache-tomcat-7.0.42/bin/startup.sh tomcat

ansible: SHELL=/bin/sh  
shell: SHELL=/bin/bash

ansible:  
shell: LC\_CTYPE=UTF-8

After that I tried to change the SHELL to /bin/bash for ansible but it didn’t help.

Any other hint? I’m really blocked by this problem ☹

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [February 10, 2014, 9:48pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/4 "2014-02-10T21:48:03Z")

</div>

I just remembered that I had so many problems with tomcat 7 start/stop that I ended up writing my own init script (will need to look for it). This was sans ansible, I was using puppet at the time.

---

<div class="post-metadata">

**Author:** ![C\_S](https://avatars.discourse-cdn.com/v4/letter/c/ea5d25/32.png) [@C\_S](https://forum.ansible.com/u/C_S)\
**Post date:** [February 11, 2014, 1:38am UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/5 "2014-02-11T01:38:51Z")

</div>

We modified the tomcat script so it killed the process group, that resolved some of the issues as I recall.

The other thing is that you may want to have unpackWAR set to false on the context, otherwise when you update the war file tomcat will continue to use the previously unpacked war vs. the new one by default.

---

<div class="post-metadata">

**Author:** ![Karl\_E\_Jorgensen](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@Karl\_E\_Jorgensen](https://forum.ansible.com/u/Karl_E_Jorgensen)\
**Post date:** [February 11, 2014, 12:50pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/6 "2014-02-11T12:50:01Z")

</div>

Hi

---

<div class="post-metadata">

**Author:** ![David\_Adams](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@David\_Adams](https://forum.ansible.com/u/David_Adams)\
**Post date:** [February 11, 2014, 4:27pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/7 "2014-02-11T16:27:50Z")

</div>

This is a good point. Tomcat does NOT detach, so running startup.sh directly from an Ansible ssh session may well immediately shut down. You’ve got to wrap it in something that will detach correctly. The easiest would be nohup. startup.sh is also kind of pointless. You can just call catalina.sh directly. From my Tomcat app’s init script:

nohup $CATALINA\_HOME/bin/catalina.sh start

-dave

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [February 11, 2014, 4:31pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/8 "2014-02-11T16:31:54Z")

</div>

I have created a couple of tomcat init scripts that actually ‘work’ to avoid these issues.

Let me look and see if i can publish one for general usage.

---

<div class="post-metadata">

**Author:** ![Alain\_Sahli](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Alain\_Sahli](https://forum.ansible.com/u/Alain_Sahli)\
**Post date:** [February 11, 2014, 6:58pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/9 "2014-02-11T18:58:42Z")

</div>

Thank you very much! Your solution works.

---

<div class="post-metadata">

**Author:** ![willthames](https://avatars.discourse-cdn.com/v4/letter/w/8dc957/32.png) [@willthames](https://forum.ansible.com/u/willthames)\
**Post date:** [February 12, 2014, 11:48pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/10 "2014-02-12T23:48:22Z")

</div>

This came up for me in the past. I found setsid pretty helpful too

[https://github.com/ansible/ansible/pull/4800](https://github.com/ansible/ansible/pull/4800)

(note that #4800 never amounted to much as it caused too many other problems, but the workaround still applies!)

Will

---

<div class="post-metadata">

**Author:** ![Alain\_Sahli](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@Alain\_Sahli](https://forum.ansible.com/u/Alain_Sahli)\
**Post date:** [February 13, 2014, 7:27am UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/11 "2014-02-13T07:27:44Z")

</div>

I finally solved it by using the script from the ansible examples: [https://github.com/ansible/ansible-examples/blob/master/tomcat-standalone/roles/tomcat/files/tomcat-initscript.sh](https://github.com/ansible/ansible-examples/blob/master/tomcat-standalone/roles/tomcat/files/tomcat-initscript.sh)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 13, 2014, 1:41pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/12 "2014-02-13T13:41:47Z")

</div>

Hi Will,

I’m confused a bit about what you are referring to in the ticket.

As I recall we got the async math bugfix completed in the end. Is there a chance you linked a different ticket by mistake possibly?

---

<div class="post-metadata">

**Author:** ![willthames](https://avatars.discourse-cdn.com/v4/letter/w/8dc957/32.png) [@willthames](https://forum.ansible.com/u/willthames)\
**Post date:** [February 13, 2014, 1:48pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/13 "2014-02-13T13:48:12Z")

</div>

I meant the workaround details at the bottom of the issue description

You’re right though, it was #4807 that never amounted to much. Mainly because I couldn’t get the fix right and wasn’t motivated enough to need to get the fix right!

Will

---

<div class="post-metadata">

**Author:** ![shivaji.devopsenable](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@shivaji.devopsenable](https://forum.ansible.com/u/shivaji.devopsenable)\
**Post date:** [July 22, 2016, 1:49pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/14 "2016-07-22T13:49:40Z")

</div>

iam also struck in tomcat8 to run .sh script can you share your playbook.

In which file i have to add this line.(nohup $CATALINA\_HOME/bin/catalina.sh start)

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [July 22, 2016, 5:23pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/15 "2016-07-22T17:23:17Z")

</div>

Folks, please don't use nohup.

Any linux will have systemd or upstart which makes it easy to run  
foreground tasks as services if you write a small file to tell it what  
to do.

---

<div class="post-metadata">

**Author:** ![dinesh.kumar2](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@dinesh.kumar2](https://forum.ansible.com/u/dinesh.kumar2)\
**Post date:** [September 14, 2017, 9:22am UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/16 "2017-09-14T09:22:19Z")

</div>

Hi,

Below rule working fine for me .

- name: start tomcat7 service  
command: “nohup /usr/local/apache-tomcat-7.0.54/bin/startup.sh”  
tags: starttom
- name: stop tomcat7 service  
command: “nohup /usr/local/apache-tomcat-7.0.54/bin/shutdown.sh”  
tags: stoptom

#ansible-playbook main.yml -i hosts --tags starttom

Thanks:  
Dinesh Kumar

---

<div class="post-metadata">

**Author:** ![Deepak\_Rohila](https://avatars.discourse-cdn.com/v4/letter/d/dec6dc/32.png) [@Deepak\_Rohila](https://forum.ansible.com/u/Deepak_Rohila)\
**Post date:** [May 31, 2018, 1:30pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/17 "2018-05-31T13:30:25Z")

</div>

thanks its working

---

<div class="post-metadata">

**Author:** ![venkat\_r](https://avatars.discourse-cdn.com/v4/letter/v/7feea3/32.png) [@venkat\_r](https://forum.ansible.com/u/venkat_r)\
**Post date:** [September 22, 2018, 2:45pm UTC](https://forum.ansible.com/t/tomcat-startup-sh-script-works-from-shell-but-not-with-ansible/14530/18 "2018-09-22T14:45:28Z")

</div>

Below worked for me to . Thanks a ton .
