28

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 (one line, but unfolded here for readability):

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.

1

2 Answers 2

46

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)

2
2

I had the same problem with PHP starting faster or before MySQL and then failing to connect to the MySQL server showing HTTP 503 errors to the user and filling my logs with "can't connect errors" which I couldn't do anything about. This is how I've solved it.

You can use systemctl, a systemd service manager, to edit the configuration file and add the After=... configuration option. In your case, you can run systemctl edit emailserver.service (where emailserver.service is your service name) and then add

[Unit]
After=mysql.service

and save the file.

Using systemctl will not edit the system configuration files but instead, it will create a new "override" file in /etc/systemd/system/emailserver.service.d/override.conf and add the configuration you've entered in the previous step into that file. You can also edit the configuration file, or the configuration override file directly, but using systemctl will figure out which file to create or edit for you.

You can also use a one-liner, but you need to tell systemctl to use a different "editor" which supports input on stdin. "Editor" in quotes because tee is not really an editor as such, but does what's needed.

echo -e "[Unit]\nAfter=mysql.service" | sudo SYSTEMD_EDITOR=tee systemctl edit emailserver.service

This will create an override file which you can then verify with:

cat /etc/systemd/system/emailserver.service.d/override.conf

You should see the following as the output:

[Unit]
After=mysql.service

You can use multiple services in the After= line separated with space, or use multiple After= lines, both is fine as documented.

Finally, you can check the changed configuration is valid with

systemctl show emailserver.service | grep After=

and you should see the After= line contains mysql.service, probably at the end of the line.

One error I've made which resulted in the mysql.service not present in the systemctl show configuration check is that I've used [Service] instead of [Unit] in the one-liner used to create the override configuration file. That's why I recommend the systemctl show check. Trust but verify, including yourself.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .