3
votes

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)

3

3 Answers

4
votes

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.

2
votes

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.

1
votes

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.