systemd script: Assignment outside of section; Missing '='
Asked Answered
T

3

4

I have the following (anonymised) systemd script called tomcat-autostart.service, stored in "/usr/lib/systemd/system/tomcat-autostart.service"

[Unit]
Description=Tomcat Autostart Script
Requires=var-www-projects.mount
After=var-www-projects.mount

[Service]
Type=forking
ExecStart=/usr/bin/tomcat-autostart start
ExecStop=/usr/bin/tomcat-autostart stop

[Install]
WantedBy=multi-user.target

After running

sudo systemctl daemon-reload
sudo systemctl enable tomcat-autostart.service

I get the error message:

Failed to execute operation: Bad message

And I also get the following output in /var/log/messages

Feb 17 11:29:53 cheese systemd: [/usr/lib/systemd/system/tomcat-autostart.service:1] Assignment outside of section. Ignoring.
Feb 17 11:29:53 cheese systemd: tomcat-autostart.service lacks both ExecStart= and ExecStop= setting. Refusing.
Feb 17 11:29:53 cheese systemd: [/usr/lib/systemd/system/tomcat-autostart.service:1] Missing '='.
Feb 17 11:29:53 cheese systemd: [/usr/lib/systemd/system/tomcat-autostart.service:1] Missing '='.

I had it working for a while, then I added some comments above the '[Unit]' line, and it stopped working. I then removed the comments, and probably unintentionally modified something else in the script, and I haven't been able to get it working again after that.

It seems to delight it in it's own contradiction. The first error message says that there is an assignment on the first line ('[Unit]'), and the third says that the assignment is missing an '=' sign. So basically, '[Unit]' is an assignment, except that it isn't because it is missing an '=' sign.

Searching for this problem has determined that the second error message is a direct cause of the others. Because it is ignoring the section headers, it never reads the ExecStart and ExecStop declarations. If I can solve the first and third error messages, I should be able to

What is the actual problem with my script? Running 'sudo /usr/bin/tomcat-autostart start' succeeds as expected, and the most common cause on the internet for this error message is having the script not use absolute paths (which I do in this case)

Tchad answered 16/2, 2017 at 22:43 Comment(0)
T
6

I have solved this issue. It turns out that the encoding of the file was changed when I added the comments (turning the file from a utf-8 encoding to a utf-16 encoding). I presume that systemd doesn't support utf-16, and was interpreting it as ASCII (or something else). Changing the encoding back to UTF-8 allowed the autostart scripts to start working again.

Tchad answered 17/2, 2017 at 1:53 Comment(1)
Just as an aside, I got around this by installing dos2unix with apt-get install dos2unix and then running dos2unix on the file in question.Jentoft
C
4

Welcome to StackOverflow.

Your systemd script looks fine. Also when I copy/paste it to my own system and run it through systemd-analyze verify, it is does not generate this error-- I only get errors about file paths that don't exist on my system but should exist on yours:

systemd-analyze verify ~/tmp/t.service
t.service: Failed to create t.service/start: Unit var-www-projects.mount not found.
t.service: Command /usr/bin/tomcat-autostart is not executable: No such file or directory
t.service: Command /usr/bin/tomcat-autostart is not executable: No such file or directory

I think the error was lost in translation when you anonymized it. Run systemd-analyze verify /usr/lib/systemd/system/tomcat-autostart.service and confirm the errors you are getting from your actual file.

Also, the files you modify should be in /etc/systemd/system. The files in /usr/lib are intended only be managed by packages you install.

If your file in /etc/systemd/system has the name as a parallel file in /usr/lib/systemd/system directory, it will override it.

Catholicity answered 16/2, 2017 at 23:34 Comment(2)
I have double-checked that the anonymization didn't change anything. The lines that were changed were the Description, Requires, After, ExecStart, and ExecStop lines, as well as the filename of the *.service file. There were no single quotes anywhere in the file. Moving the file to /etc/systemd/system didn't change anything except for the output in /var/log/messages (which now referenced /etc/systemd/system/tomcat-autostart.service, instead of /usr/lib/systemd/system/tomcat-autostart.service) However, I will use that directory in the future.Tchad
See updated answer mentioning systemd-analyze verify tool.Catholicity
B
2

In my case the file had a Byte Order Mark - BOM (hexadecimal bytes EF BB BF in beginning of file to identify an UTF-8 file).

You can verify this using cat -A tomcat-autostart.service (print All characters, including special/invisible ones) or using hexdump -C tomcat-autostart.service (print in Canonical hex+ASCII display).

Remove these characters with your favorite text editor (like sed) or an converter like dos2unix as Digit Cruncher suggested.

Brockway answered 8/11, 2020 at 19:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.