LibreOffice Headless with systemd

I've fought a very annoying bug recently and think it's worth sharing it here. At work we manage different type of user provided documents. For various reason we convert it to pdf. This step is achieved using a headless libreoffice installation. Our server run Debian GNU/Linux Wheezy and everything go smoothly.

Until... one of our client need our software to be hosted on his own servers, running CentOS. Nothing bad here, but it leads us to develop various systemd service files for our systems as recent versions of CentOS switched to systemd. Here come the bug. We were used to run libreoffice as follow, embed in an init.d script:

1: soffice -headless -accept="socket,host=127.0.0.1,port=8101;urp;" -display :5.0 -nofirststartwizard & > /var/log/soffice.log 2>&1

Moving on systemd and more recent version of libreoffice, I learn that single-dotted options are no more. Thus, my first attempt to write a systemd service file, directly inspired by the line above, was:

1:  [Unit]
2:  Description=Control headless soffice instance
3:  After=network.target xvfb.service
4:  Requires=xvfb.service
5:  
6:  [Service]
7:  Type=simple
8:  ExecStart=/opt/libreoffice4.4/program/soffice --headless \
9:      --accept="socket,host=127.0.0.1,port=8101;urp;" --display :5.0 \
10:     --pidfile=/var/run/soffice.pid --nologo --nodefault --nofirststartwizard
11: RestartSec=5
12: ExecStop=/usr/bin/pkill -F /var/run/soffice.pid
13: PIDFile=/var/run/soffice.pid
14: 
15: [Install]
16: WantedBy=multi-user.target

And nothing works as expected. In fact, I'm lying a bit: the daemon was here, systemctl telling me everything is ok and libreoffice showing off in ps axf. But no conversion succeeded. The problem was obvious: looking at netstat -laputn show me that nothing was listening at port 8101.

After weeks of test, we abandoned the problem (I just start the exactly same command by hand and forget it). Yesterday it comes to my face, due to a libreoffice crash, which prevents conversion to occurs. We use monit to monitor our process, but in this case soffice wasn't monitored, as I didn't reached to have a functional service. As I was crying blood, because of not understanding the problem source, I try a last thing before fucking all of it and writing a quick'n'dirty shell script. And it works.

1:  [Unit]
2:  Description=Control headless soffice instance
3:  After=network.target xvfb.service
4:  Requires=xvfb.service
5:  
6:  [Service]
7:  Type=simple
8:  ExecStart=/opt/libreoffice4.4/program/soffice --headless \
9:      --accept=socket,host=127.0.0.1,port=8101;urp; --display :5.0 \
10:     --pidfile=/var/run/soffice.pid --nologo --nodefault --nofirststartwizard
11: RestartSec=5
12: ExecStop=/usr/bin/pkill -F /var/run/soffice.pid
13: PIDFile=/var/run/soffice.pid
14: 
15: [Install]
16: WantedBy=multi-user.target

Yes: nothing changes, but the quotation marks on line 9. The solution was simply to remove them from the ExecStart line. I absolutely don't know why putting here double quotes prevent systemd-started soffice to open a socket, but it does. If anyone has an explanation, I'm very curious. The documentation itself is not very clear about that. Anyway, now we have a working service file for headless execution of libreoffice.

Commentaires

N'hésitez pas à réagir en m'envoyant un email, qui sera publié ci-dessous sous la forme d'un commentaire.

Comment from Dani

De Dani - 19/01/2017, 16:49

Thanks for the info, you saved my life :)