1

I'm trying to follow the ArchWiki guide for writing a custom service file to launch DavMail as a system-wide daemon. Previously I had a line in my ~/.xinitrc that read

nohup /usr/bin/davmail /etc/conf.d/davmail.properties

as per the DavMail instructions (where /usr/bin/davmail is a symlink to /usr/share/java/davmail/davmail.sh). Now that I've made the computer into a headless server, I'd like the auto-launcher to be independent of an X session.

All the guides I've seen online for a DavMail initscript assume that the distro is using sysvinit, not systemd. My attempt at a custom systemd service file is

[Unit]
Description=DavMail Exchange Gateway
Requires=network.target
After=network.target

[Service]
Type=forking
RemainAfterExit=yes
ExecStart=/usr/bin/davmail /etc/conf.d/davmail.properties
ExecStop=killall davmail

[Install]
WantedBy=multi-user.target

which fails to fork properly. Prepending the ExecStart command with /usr/bin/nohup is no help, because systemd complains about an invalid argument (preferring to take control of the daemonization itself, if I'm correctly understanding previous forum answers). Changing the Service Type to oneshot or simple also fails.

I think an issue here is that DavMail requires a Java interpreter to run, and systemd service files aren't as flexible as shell scripts in handing off the job to a Java interpreter. But maybe there's a much simpler explanation. Has anyone successfully written a systemd service file for DavMail that they'd be willing to share?

biqu
  • 11

3 Answers3

4

You must define it as a simple service (the forking is for process that finishes when the daemon is already started). For it's working like a charm in Arch

[Unit]
Description=DavMail Exchange Gateway
Requires=network.target
After=network.target

[Service]
Type=simple
RemainAfterExit=no
ExecStart=/usr/bin/davmail /etc/conf.d/davmail.properties
ExecStop=killall davmail

[Install]
WantedBy=multi-user.target
0

For those that don't have a debian or a distro based on it and downloaded the generic Linux (other) package (that includes the davmail.sh), the following worked for me:

[Unit]
Description=DavMail Exchange Gateway
Requires=network.target
After=network.target

[Service]
Type=forking
PIDFile=/opt/davmail/davmail.pid
ExecStart=/opt/davmail/davmail.sh /opt/davmail/davmail.properties

[Install]
WantedBy=multi-user.target
0

After a little bit of experimentation, I found a somewhat-more-optimal solution for using Davmail with the davmail.server=true directive, assuming you have performed a manual install.

[Unit]  
Description=DavMail Exchange Gateway  
Requires=network.target  
After=network.target  

[Service]  
Type=simple  
RemainAfterExit=no  
GuessMainPid=yes  
Environment=BASE=<< YOUR_MANUAL_INSTALL_PATH_HERE >>  
Environment=PROPERTIES=<< PATH_TO_davmail.properties >>
ExecStartPre=/usr/bin/bash -c "for i in ${BASE}/lib/*; do /usr/bin/systemctl set-environment CLASSPATH=${CLASSPATH}:$i ; done"  
ExecStart=/usr/bin/java -Xms1G -Xmx1G -XX:ErrorFile=/var/log/davmail_errors.log -XX:-HeapDumpOnOutOfMemoryError -Djava.awt.headless=true -Dsun.net.inetaddr.ttl=60 -cp ${BASE}/davmail.jar:${CLASSPATH} davmail.DavGateway ${PROPERTIES}  
ExecReload=/bin/kill -HUP ${MAINPID}  
ExecStop=/bin/kill -QUIT ${MAINPID}  
Restart=always  
RestartSec=3  

[Install]  
WantedBy=multi-user.target

For those who have done packaged installs, they can likely comment out the "ExecStartPre" line, and then set BASE=/usr/share/java and PROPERTIES=/etc/davmail.properties .

Reasons this is "nicer" include showing the java process console output in your default syslog location ( /var/log/syslog || /var/log/messages || wherever ), making it easier to debug when it goes bad.