20

I have the following systemd unit file in /etc/systemd/system/emacs.service:

[Unit]
Description=Emacs: the extensible, self-documenting text editor
Documentatin=man:emacs(1) info:Emacs


[Service]
Type=forking
ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --eval "(progn (setq kill-emacs-hook nil) (kill-emacs))"
Restart=always
Environment=DISPLAY=:%i
TimeoutStartSec=0

[Install]
WantedBy=default.target

I want this to start on boot, so I have entered systemctl enable emacs

However, each time my service reboots, systemctl status emacs shows:

● emacs.service - Emacs: the extensible, self-documenting text editor
   Loaded: loaded (/etc/systemd/system/emacs.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

But then entering systemctl start emacs and checking the status returns:

● emacs.service - Emacs: the extensible, self-documenting text editor
   Loaded: loaded (/etc/systemd/system/emacs.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2016-11-11 23:03:59 UTC; 4s ago
  Process: 3151 ExecStart=/usr/bin/emacs --daemon (code=exited, status=0/SUCCESS)
 Main PID: 3154 (emacs)
    Tasks: 2
   Memory: 7.6M
      CPU: 53ms
   CGroup: /system.slice/emacs.service
           └─3154 /usr/bin/emacs --daemon

How can I get this process to successfully start on boot?

9

I have no idea why but to get this to work I:

deleted Environment=DISPLAY=:%i

added a User= variable

Made sure the correct file was in /etc/systemd/system/emacs.service (earlier it had been a hard link)

and re-ran systemctl enable emacs

This made it work.

EDIT The real problem here is that I had a typo at line 3: Documentatin

I found this by checking journalctl. I suggest anyone who has issues with a systemd script do the same as there was no error sent to stderr.

  • Does that also work on a reboot? I guess if the daemon mode does not require an X11 connection, then there is no need for the After=... I mentioned. – Alexis Wilke Nov 12 '16 at 1:20
  • 1
    Yes it works after reboot. This is not a graphical Emacs so no X11 is needed. That was just a line I copied from an example and didn't pay attention. – Startec Nov 12 '16 at 1:39
3

ooh this is interesting.

Picking a random service unit and staring at it, it depends on a specific target instead of default.target. The latter is symbolic... a configured link to a specific target, semantically it doesn't make sense. (See systemctl set-default)

That might explain why your service shows as disabled after you enable it. Try replacing default.target in your service file with multi-user.target, for example.

(Not reporting an error when failing to enable seems like a defect in systemd. I almost wonder if you now have a directory /etc/systemd/system/default.target.wants).

  • The journalctl command tells you what broke (why the enable failed, for example.) As for the link, it is not required if you create your own personal service. It does matter if you create a package that you want others to install/remove, etc. – Alexis Wilke Nov 12 '16 at 1:22
  • @AlexisWilke That would be even worse! Why would it be written to report some errors to stderr and others to the journal? – sourcejedi Nov 12 '16 at 9:26
  • It writes everything to the journal. From what I've seen, you only see a very basic, systemd specific, error if it could not start the daemon. – Alexis Wilke Nov 12 '16 at 20:46
  • 2
    user did not report even a basic error, they believed the operation had succeeded and therefore proceeded to reboot. at face value, there is a defect in systemd. Users also have failure modes, but it didn't sound to me like overlooking an error message completely was a very likely one in this question. – sourcejedi Nov 12 '16 at 21:45
  • 1
    @sourcejedi I have no idea how you knew this but yes, I now have a listing at /etc/systemd/system/default.target.wants Inside that is my service files. And yes, I had no idea there was any error. – Startec Dec 2 '16 at 6:24
1

You have a DISPLAY environment variable, that means you want X11 to be started. So you need to have a way to block your service until then.

This is done using the After=... option.

I haven't done it myself, so I cannot say that it would work, but it is likely something to do with graphical.target.

[Unit]
After=graphical.target

Another possibility, if the X server does not start immediately (i.e. you have a login screen with lightdm or such), then you may have to use WantedBy=... instead:

[Unit]
WantedBy=graphical.target

If you get tired of getting it to work with systemd, you may want to look into the usual way X-Windows managers make it work.

There is the ~/.xprofile file, which works like the ~/.bashrc file.

There is also the ~/.config/autostart/*.desktop files. It will automatically start whatever applications are defined in there.

These solutions are not system wide, though, in case you have multiple users, each one would have to have its own entry. Also, it does not start the application as root, but you, instead.


As a side note, the "loaded + inactive (dead)" message means that systemd had a hard time to start the process and as a result decided to abandon it. You can manually test that the name.service works once you rebooted using:

systemctl stop <service-name>
systemctl start <service-name>

This will refresh the status and start the service properly, assuming the info is correct. You can then check the status again to see additional details:

 systemctl status <service-name>
  • 2
    Hm, I deleted that entire line and nothing changed. Also, (as I said in my question) it starts fine by running systemctl start emacs). – Startec Nov 11 '16 at 23:25
  • see my updated question. I had a typo in Documentatin . Your hint about journalctl helped me here. – Startec Dec 2 '16 at 6:33
0

It's a bug in several service files of Debian:

# systemctl enable watchdog
Synchronizing state of watchdog.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable watchdog
# find /etc/systemd/ | grep watch
# tail -n2 /lib/systemd/system/watchdog.service

[Install]

https://www.raspberrypi.org/forums/viewtopic.php?f=82&t=218609&p=1406567#p1406567 https://forum.armbian.com/topic/9115-still-dont-know-where-to-report-bugs-watchdogservice-refuses-to-start-due-to-broken-service-file/

The distribution level fix is to

echo "WantedBy=default.target" >> /lib/systemd/system/watchdog.service
systemctl daemon-reexec
systemctl enable watchdog
systemctl stop watchdog.service
systemctl start watchdog.service

There are many manual alternatives to this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.