# restart server + check if service is started

**URL:** <https://forum.ansible.com/t/restart-server-check-if-service-is-started/15405>\
**Category:** Ansible Project\
**Created:** [June 5, 2014, 8:05am UTC](https://forum.ansible.com/t/restart-server-check-if-service-is-started/15405 "2014-06-05T08:05:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark3](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@Mark3](https://forum.ansible.com/u/Mark3)\
**Post date:** [June 5, 2014, 8:05am UTC](https://forum.ansible.com/t/restart-server-check-if-service-is-started/15405/1 "2014-06-05T08:05:59Z")

</div>

Hi,  
Kinda new to Ansible and I’m struggling with the following:

I want to update a MYSQL server → httpd → varnish server. in that order.  
I want to check, after the server rebooted, if the service mysqld or httpd is started before I continue with the httpd or varnish server.

Right now I have this but the post task runs way to fast before the server is restarted so it returns that the service is started:

Following is a code snippit, for the app server i instructed it to wait for port 80 and service httpd. but its the same.

Question is: How do I check if the server and service is up and running before I continue with the other servers in the playbook.  
Thanks!

- hosts: dbservers  
user: root  
serial: 1

tasks:

- name: upgrade all packages + when done restart server  
yum: name=\* state=latest  
notify:
- restart server db  
handlers:
- name: restart server db  
command: reboot

post\_tasks:

- name: wait for db server to come up  
wait\_for: host={{ inventory\_hostname }} port=3306 state=started timeout=80
- name: make sure mysql is started  
service: name=mysqld state=started

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [June 5, 2014, 4:18pm UTC](https://forum.ansible.com/t/restart-server-check-if-service-is-started/15405/2 "2014-06-05T16:18:56Z")

</div>

I’m guessing that what’s going on here is that you’re rebooting, but during the shutdown the mysql service is still running and port 3306 is up, so that when it gets to the wait\_for task it succeeds and moves on. You may either want to add in a pause there before the wait\_for, or you can add in a delay to the wait\_for to cause it to delay its first polling attempt.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [June 5, 2014, 6:41pm UTC](https://forum.ansible.com/t/restart-server-check-if-service-is-started/15405/3 "2014-06-05T18:41:30Z")

</div>

​Or do a wait\_for state=stopped and then the same but with state=started.

If you want to check on a particular name in the process list to go away,  
then to re-appear, you might want to have a look at this module:

[https://github.com/ginsys/ansible-plugins/blob/devel/library/check\_process](https://github.com/ginsys/ansible-plugins/blob/devel/library/check_process)

Serge

&nbsp;&nbsp;&nbsp;&nbsp;​

---

<div class="post-metadata">

**Author:** ![Mark3](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@Mark3](https://forum.ansible.com/u/Mark3)\
**Post date:** [June 5, 2014, 9:00pm UTC](https://forum.ansible.com/t/restart-server-check-if-service-is-started/15405/4 "2014-06-05T21:00:13Z")

</div>

I see now that I missed the delay argument in the wait for port.  
I already figured out the wait for stopped service.

Thanks a lot for the quick and clean responses from you both!
