# How to retain the remote session till the script is fully executed |

**URL:** <https://forum.ansible.com/t/how-to-retain-the-remote-session-till-the-script-is-fully-executed/24677>\
**Category:** Ansible Project\
**Created:** [June 7, 2017, 10:44pm UTC](https://forum.ansible.com/t/how-to-retain-the-remote-session-till-the-script-is-fully-executed/24677 "2017-06-07T22:44:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surya](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@Surya](https://forum.ansible.com/u/Surya)\
**Post date:** [June 7, 2017, 10:44pm UTC](https://forum.ansible.com/t/how-to-retain-the-remote-session-till-the-script-is-fully-executed/24677/1 "2017-06-07T22:44:38Z")

</div>

Dear Ansible Team,

I have my customized Jboss start script “jboss-start-all.sh” to start my Domain/host controllers and the server groups. When i execute the the script locally its starting the instances successfully. but when i trigger it from ansible, the command is successfully executed but the instances are not starting .

- name: Start Jboss EAP Server  
shell: “nohup /opt/jboss-eap-6.4/bin/jboss-start-all.sh &”  
tags: start

is it possible to keep the ansible session active till the script is starting all the instances. What i suspect is, Ansible fire the command in remote server and closing the session, As soon as session is closed my script also stopped, so its failing to start all my instances.

Please help fix this issue.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [June 8, 2017, 4:31am UTC](https://forum.ansible.com/t/how-to-retain-the-remote-session-till-the-script-is-fully-executed/24677/2 "2017-06-08T04:31:44Z")

</div>

I think you might just need to use async - [http://docs.ansible.com/ansible/playbooks\_async.html](http://docs.ansible.com/ansible/playbooks_async.html) to get this working.

However, depending on what you are trying to achieve, it might be worth re-organising things so that ansible starts the cluster nodes individually. This would let you do useful things like do rolling restarts of your cluster members (this is very easy to do using the ‘serial’ directive - see [http://docs.ansible.com/ansible/playbooks\_delegation.html#rolling-update-batch-size](http://docs.ansible.com/ansible/playbooks_delegation.html#rolling-update-batch-size)).

Also might be worth considering wrapping the scripts that you use to start your nodes as services. That way, once a host is booted, it could bring the service (jboss) up straight away.

We use tomcat, which we have wrapped as a service and use ansible to do rolling upgrades of our tomcat apps without downtime. Originally we used to just wait between nodes but once we had figured out how to poll our apps to check that they had completed deploying and were serving requests by polling them using the uri module we are now able to do relatively smooth rolling updates. Here’s the url task we use to poll.

- name: check every 3 seconds for 40 attempts if tomcat is up and ready to serve the healthcheck json  
uri:  
url: ‘http://{{ inventory\_hostname }}/application/healthcheck.json’  
return\_content: yes  
timeout: 2  
delegate\_to: localhost  
register: tomcat\_result  
until: tomcat\_result[‘status’]|default(0) == 200  
retries: 40  
delay: 3

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Paul\_Markham](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Paul\_Markham](https://forum.ansible.com/u/Paul_Markham)\
**Post date:** [June 8, 2017, 5:44am UTC](https://forum.ansible.com/t/how-to-retain-the-remote-session-till-the-script-is-fully-executed/24677/3 "2017-06-08T05:44:11Z")

</div>

Since you’re backgrounding the script, the shell will finish immediately. Not sure why this would be stopping you’re script from running.

If your script finishes when all of the instances are started, just remove the & at the end and run it in the foreground. Ansible will wait for it to return.

---

<div class="post-metadata">

**Author:** ![Surya](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@Surya](https://forum.ansible.com/u/Surya)\
**Post date:** [June 8, 2017, 6:29pm UTC](https://forum.ansible.com/t/how-to-retain-the-remote-session-till-the-script-is-fully-executed/24677/4 "2017-06-08T18:29:52Z")

</div>

Thanks team,!

Have resolved the issue, just added “sleep 200” in the end of my startup script. So Ansible will wait 200 sec to terminate the ssh session. I could see my jboss instances are started and running fine.

Thanks for your time and suggestions …! Appreciate it
