3

I've added a service (Seafile, in this case) that I want to have running at all times to systemd with a service file. It works great, but every time the unattended updates run, the service gets shutdown properly - but never restarted.

Here's what the service file looks like:

[Unit]
Description=Seafile
Requires=mysql.service
After=mysql.service

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
User=root
Group=root
PermissionsStartOnly=true
ExecStart=/srv/start-seafile
TimeoutSec=600
Restart=on-failure

/srv/start-seafile looks like this:

#!/bin/bash

cd /srv/seafile/XXXX/seafile-server-latest && nohup ./seafile.sh start

Like I said - this works perfectly - systemd can enable / disable / start / stop the service, realize if it's started / running - so I must be doing something right.

# systemctl start seafile
# systemctl status seafile
* seafile.service - Seafile
   Loaded: loaded (/etc/systemd/system/seafile.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-01-28 17:53:51 CET; 8s ago
  Process: 6569 ExecStart=/srv/start-seafile (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/seafile.service
       |-6598 /srv/seafile/XXXX/seafile-server-5.1.4/seafile/bin/seafile-controller -c /srv/seafile/XXXX/ccnet -d /volume1/Seafile -F /srv/seafile/XXXX/conf
       |-6600 ccnet-server -F /srv/seafile/XXXX/conf -c /srv/seafile/XXXX/ccnet -f /srv/seafile/XXXX/logs/ccnet.log -d -P /srv/seafile/XXXX/pids/ccnet.pid
       `-6602 seaf-server -F /srv/seafile/XXXX/conf -c /srv/seafile/XXXX/ccnet -d /volume1/Seafile -l /srv/seafile/XXXX/logs/seafile.log -P /srv/seafile/XXXX/pids/seaf-server.pid

Jan 28 17:53:48 XXXX systemd[1]: Starting Seafile...
Jan 28 17:53:48 XXXX start-seafile[6569]: [01/28/18 17:53:48] ../common/session.c(132): using config file /srv/seafile/XXXX/conf/ccnet.conf
Jan 28 17:53:48 XXXX start-seafile[6569]: Starting seafile server, please wait ...
Jan 28 17:53:51 XXXX start-seafile[6569]: Seafile server started
Jan 28 17:53:51 XXXX start-seafile[6569]: Done.
Jan 28 17:53:51 XXXX systemd[1]: Started Seafile.

However, every time the unattended updates come, this happens:

Jan 23 06:38:08 XXXX systemd[1]: Starting Daily apt activities...
Jan 23 06:40:13 XXXX systemd[1]: Reloading.
Jan 23 06:40:13 XXXX systemd[1]: Stopping Seahub...
Jan 23 06:40:14 XXXX systemd[1]: Stopped Seahub.
Jan 23 06:40:14 XXXX systemd[1]: Stopping Seafile...
Jan 23 06:40:15 XXXX systemd[1]: Stopped Seafile.
Jan 23 06:40:15 XXXX systemd[1]: Stopping MySQL Community Server...
Jan 23 06:40:16 XXXX systemd[1]: Stopped MySQL Community Server.
Jan 23 06:40:17 XXXX systemd[1]: Reloading.
Jan 23 06:40:17 XXXX systemd[1]: Stopped MySQL Community Server.
...
Jan 23 06:40:43 XXXX systemd[1]: Reloading.
Jan 23 06:40:44 XXXX systemd[1]: Stopped MySQL Community Server.
Jan 23 06:40:44 XXXX systemd[1]: Reloading.
Jan 23 06:40:44 XXXX systemd[1]: Reloading.
Jan 23 06:40:44 XXXX systemd[1]: Starting MySQL Community Server...
Jan 23 06:40:45 XXXX systemd[1]: Started MySQL Community Server.
Jan 23 06:40:47 XXXX systemd[1]: Reloading.
Jan 23 06:40:48 XXXX systemd[1]: Stopping MySQL Community Server...
Jan 23 06:40:49 XXXX systemd[1]: Stopped MySQL Community Server.
Jan 23 06:40:49 XXXX systemd[1]: Starting MySQL Community Server...
Jan 23 06:40:50 XXXX systemd[1]: Started MySQL Community Server.
(... continues with unrelated services )

So it realizes that Seafile needs to be stopped before MySQL, and does so, but no longer starts it after it restarts MySQL.

Does anyone have any experience as to what could be causing this? ie. under which circumstances systemd services will be stopped during an update, but not restarted?

Aaa
  • 614
  • 5
  • 14
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Jan 28 '18 at 17:42
  • 1
    @jww Please don't downvote questions because they are off-topic. A close flag is enough. – iamauser Jan 28 '18 at 17:47
  • 1
    @jww I disagree. I'm writing my own service-files. But gj going through year old questions like https://stackoverflow.com/questions/21830670/systemd-start-service-after-specific-service - also on SO - like half a dozen others in the related questions - and voting them down too. You make me want to participate in SO so much. Not. – Aaa Jan 30 '18 at 00:30

1 Answers1

3

Use Restart=always if you want it to run at all times. When mysql service update happens, this service does a clean stop and therefore systemd doesn't restart it. You have Restart=on-failure set, which only restarts if the stop has a return code other than 0.

Restart = always
RestartSec = 10

RestartSec
Configures the time to sleep before restarting a service (as configured with Restart=). Takes a unit-less value in seconds, or a time span value such as "5min 20s". Defaults to 100ms.

iamauser
  • 11,119
  • 5
  • 34
  • 52
  • I do want it to run always - but is this really the only way? If I stop the service manually for some reason, wouldn't Restart=always restart it as well? If not, what's different between me stopping a service and apt's unattended update stopping the service? To clarify - it seems like this cures the symptoms (service down -> restart) and not the cause (why isn't the service restarted after the update, like MySQL is? The mysql.service file has "Restart=on-failure" in it as well) – Aaa Jan 28 '18 at 17:16
  • If you want to do this via `systemd`. Yes, if you don't use `systemd` to stop the service and `kill` the process or use its binary to stop the service, then `systemd` will restart it. The cause is `mysql` is a requirement for this service, if that's not running, this service stops, but you haven't told the service to restart unless it's a failure. – iamauser Jan 28 '18 at 17:20
  • I still don't understand what's different between me (cleanly) shutting down a service using systemd, and the unattended updates (cleanly) shutting it down. In the former case I wouldn't want it to restart, in the latter case I would. The problem is more that during an update, Seafile and MySQL are properly shut down, but only MySQL is restarted, even though both have "Restart=on-failure" in their service-file. – Aaa Jan 28 '18 at 17:26
  • 1
    Restart of `MySQL` after an update is a packaging time trigger, like a `rpm` `%post` script which runs when the package gets updated. Whereas, if you stop `MySQL` outside of `systemd`, it will not restart itself. – iamauser Jan 28 '18 at 17:30
  • Be careful answering questions like these. Some in the community feel answers should be downvoted, too. Also see [Should one downvote answers to off-topic questions?](http://meta.stackexchange.com/q/194963/173448) – jww Jan 28 '18 at 22:21
  • 1
    @jww Well! Evidently... especially to a question that clearly off-topic, while this one is not... it seems you are only a “few”, not “some”... – iamauser Jan 28 '18 at 22:56
  • 1
    And how many downvotes you got for that. Many of the questions you have answered about openssl should belong to unix&linux, don’t call any of that fair, you are being abusive. – iamauser Jan 28 '18 at 23:09
  • 3
    well I certainly didn't downvote you. Writing my own service files - which I clearly stated I did - isn't quite "administrative" "super user" stuff either. If people just want to swing around their e-p I wish they did it outside of honest, carefully written questions. Yes, I did consider other sites. Yes, I do consider writing my own service files part of my programming activities. Hence, after consideration, I posted on this site. What an absolute hostile environment here. Have an accept vote for +10 rep, that's all I can do. – Aaa Jan 30 '18 at 00:27