Systemd service file for e.g. Centos 7


#1

Hi @BuvinJ,

since we stumbled upon this - https://serverfault.com/questions/920178/centos-7-custom-service-failure-interactive-authentication-required/922635 - post, we are going to add systemd support with one of the upcoming releases.

For now, you may use a service file like the following example:

[Unit]
Description=Scipio CE ERP Service
After=syslog.target network.target network-online.target
Requires=network-online.target

[Service]
User=scipio
Group=scipio
WorkingDirectory=/home/scipio/scipioce
ExecStart=/bin/sh -c "exec /usr/java/latest/bin/java -Dsolr.solr.home=applications/solr/ -Dsolr.log.dir=runtime/logs/solr/ -jar /home/scipio/scipioce/ofbiz.jar"
ExecStop=/bin/kill $MAINPID
SuccessExitStatus=143
KillMode=process
Type=simple

[Install]
WantedBy=multi-user.target

Please adjust the user and paths to your needs.

Thanks, @rentonize


#2

You guys are the best! I was going to submit a similar script to your repo, but you beat me to the punch. Mine looks very similar to yours, but basically just wrapped the start / stop bash scripts.


#3

Questions:

What is assigning a value to $MAINPID? Is that a systemd built-in feature? Does it know the pid it launched via start?

Why use exec to spawn another shell? And (related I think) why set type to simple rather than fork?


#4

Hi @BuvinJ

What is assigning a value to $MAINPID? Is that a systemd built-in feature? Does it know the pid it launched via start?

Exactly, systemd is aware of the pid and sends a SIGTERM in order to kill it if desired.

Why use exec to spawn another shell? And (related I think) why set type to simple rather than fork?

Well, if you don’t wrap it in a shell systemd doesn’t know what the JVM started exactly and therefore is unaware of what to stop (see above). It’s more difficult to control a daemon that doesn’t have a pid file, therefore forked doesn’t make much sense in this case.


#5

Excellent explanation. Thank you. I understand now. I will put this in place in the immediate future on my CentOS 7 machine.


#6

After tweaking the paths, this worked perfectly. Thanks!

One suggestion I have is inserting the ${JAVA_HOME} variable. That’s what I did to define that part. I had already created it for the Scipio setup, so I carried it into this.