Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

systemd not waiting for my service unit to finish before starting the next one

Tags:

docker

systemd

Here are my two units:

 - name: percona_db.service
   command: start
   enable: true
   content: |
     [Unit]
     Description=Percona db
     After=docker.service
     Requires=docker.service

     [Service]
     ExecStartPre=/bin/bash -c '/usr/bin/docker start -a mysql_datastore || /usr/bin/docker run -d -v /var/lib/mysql --name mysql_datastore -p 23:23 busybox'
     ExecStart=/bin/bash -c '/usr/bin/docker start -a mypercona || /usr/bin/docker run -i -t --volumes-from mysql_datastore --name="mypercona" -p 3306:3306 --rm percona'
     ExecStop=/usr/bin/docker stop mypercona

     [Install]
     WantedBy=multi-user.target
 - name: php_fpm.service
   command: start
   enable: true
   content: |
     [Unit]
     Description=php fpm
     After=percona_db.service
     Requires=percona_db.service

     [Service]
     ExecStart=/bin/bash -c '/usr/bin/docker start -a myphpfpm_53 || /usr/bin/docker run --name myphpfpm_53 -dit -p 9000:9000  --link mypercona:db phpfpm_53'
     ExecStop=/usr/bin/docker stop myphpfpm_53

     [Install]
     WantedBy=multi-user.target

My problem is the following: The docker container launched by percona_db.service can sometimes takes a long time to be loaded (if it's the fist load, it will create db and add data, it can take some time). The php_fpm service needs percona service to start as I link them together. And, even though I specified:

 After=percona_db.service
 Requires=percona_db.service

systemd tries to start the phpfpm service before percona_db service is finsihed and throws an error saying percona container doesnt exists :/.

What am I doing wrong? Or what can I do to make it work? (maybe make phpfpm service wait artificially? Is it possible with systemd?)

thank you !

like image 518
VsM Avatar asked Sep 26 '14 04:09

VsM


Video Answer


1 Answers

your percona_db is a long running process, so systemd would fork a subprocess and let it run, and assume all is well, and go ahead start php_fpm.

This is also common for a lot of services, i.e., start-up script returned before the application is actually ready. The best way is to write a program to check the status of percona_db and it would be waiting until percona_db is ready, and then, add a line ExecStartPre=<your_check_program> .. in your php_fpm service file.

like image 159
Sproffer Avatar answered Nov 15 '22 10:11

Sproffer