How to start a systemd service only after thesql has started

opensusesystemd

I am working on a project which is a mono EmailServer. I have tried adding a service to the systemd so that it starts up automatically during boot but I am having a problem.

I have created the script and added to /lib/systemd/system/emailserver.service and then run ln -s /lib/systemd/system/emailserver.service /etc/systemd/system/emailserver.service.

I have then executed systemd reload-daemon followed by systemctl start emailserver.service and then when I run ps -ef |grep -i emailserver I can then see that mono EmailServer.exe is running, all looking good so far.

However, when I reboot the server in the /var/log/messages file it then contains the following error message

Jul 16 19:41:02 dev-server systemd[1]: emailserver.service holdoff
time over, scheduling restart. Jul 16 19:41:02 dev-server systemd[1]:
emailserver.service holdoff time over, scheduling restart. Jul 16
19:41:03 dev-server systemd[1]: emailserver.service holdoff time over,
scheduling restart. Jul 16 19:41:03 dev-server systemd[1]:
emailserver.service holdoff time over, scheduling restart. Jul 16
19:41:03 dev-server systemd[1]: emailserver.service holdoff time over,
scheduling restart. Jul 16 19:41:04 dev-server systemd[1]:
emailserver.service holdoff time over, scheduling restart. Jul 16
19:41:04 dev-server systemd[1]: emailserver.service start request
repeated too quickly, refusing to start.

In my emailserver.service script I have the following

[Unit]
Description=Boardies Email Server Startup Script

[Service]
ExecStart=/home/bits/EmailServer/start.email
Restart=always

[Install]
WantedBy=multi-user.target

I'm using OpenSuse 12.1

What am I doing wrong, thanks for any help you can provide.

UPDATE
I have found out what is causing the program to stop working at bootup but I am not sure how I can fix this problem. My program relies on a MySQL database and I think my program is starting up before MySQL has started, how can I make my service not load until after MySQL has started up.

Best Answer

Add After=mysql.service to your service file (or change it to the correct service name), e.g:

[Unit]
Description=Boardies Email Server Startup Script
After=mysql.service

[Service]
ExecStart=/home/bits/EmailServer/start.email
Restart=always

[Install]
WantedBy=multi-user.target

Please note that you don't have to put your service file into /lib/systemd/system, it is a user provided file and you should only copy it to /etc/systemd/system.

To get a list of all service files you can use systemctl list-unit-files and determine the correct name for your database service (it is probably either mysql.service or mysqld.service)