Alternative Way to Daemonize Java Applications on Systemd (CentOS7/RHEL7)
30th October 2014 by Ali Erdinç KöroğluI already explained how to daemonize java applications on SysV-style system in here. Since CentOS7/RHEL7 comes with Systemd which is a system and service manager for Linux we migrate old init scripts to the new system.
Again we’ll use our best budy Daemonize but this time we gonna compile it from source because although it’s signed as approved on fedora package db, Daemonize is not in EPEL7 repository for now. I’ll not going to details how to compile install etc. but I assume that you installed daemonize into /usr/local/sbin
So we need to create two files /etc/sysconfig/fixtures and /lib/systemd/system/fixtures.service
This 1st file is where we define java releated variables such as user, java path, arguments, log files etc..
1 2 3 4 5 6 7 8 9 10 | # Configz for fixtures service JAVA_USER="pronet" JAVA_STDOUT="/var/log/pronet/fixtures.log" JAVA_STDERR="/var/log/pronet/fixtures-error.log" JAVA_BIN="/usr/java/jdk1.7.0_71/bin/java" JAVA_APPDIR="/opt/pronet/fixtures" ARG1="-Dfile.encoding=UTF-8 -Dproject.properties=/opt/pronet/fixtures/fixtures.properties" ARG2="-Dlog4j.configuration=file:/opt/pronet/fixtures/fixtures-log.properties" ARG3="-jar /opt/pronet/fixtures/fixtures.jar" |
2nd file is service file for fixtures where we define systemd releted variables. There are plenty of documents in Freedesktop Systemd wiki, if you want to know more about I advice you to read them. But roughly unit: consist information about a service, a socket, a device etc, service: information about a process controlled and supervised by systemd and install: installation information for the unit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [Unit] Description=Fixtures Service After=syslog.target After=network.target [Service] Type=forking EnvironmentFile=-/etc/sysconfig/fixtures ExecStart=/usr/local/sbin/daemonize -u $JAVA_USER -o $JAVA_STDOUT -e $JAVA_STDERR -c $JAVA_APPDIR $JAVA_BIN $ARG1 $ARG2 $ARG3 ExecStop=/bin/kill -TERM $MAINPID TimeoutSec=300 [Install] WantedBy=multi-user.target |
Let’s start and stop the service
[root@Srv25 pronet]# systemctl start fixtures [root@Srv25 pronet]# systemctl stop fixtures |
if there is something wrong all service files and docker containers insert data into the systemd journal and we can read the journal :)
[root@Srv25 pronet]# journalctl -u fixtures.service |
Checking the service status
[root@Srv25 pronet]# systemctl status fixtures fixtures.service - Fixtures Service Loaded: loaded (/usr/lib/systemd/system/fixtures.service; disabled) Active: active (running) since Wed 2014-10-29 21:21:49 EET; 13min ago Main PID: 28859 (java) CGroup: /system.slice/fixtures.service └─28859 /usr/java/jdk1.7.0_71/bin/java -Dfile.encoding=UTF-8 -Dproject.properties=/opt/pronet/fixtures/fixtures.properties -Dlog4j.configuration=file:... Oct 29 21:21:49 Srv25 systemd[1]: Started Fixtures Service. |
Enable the service to be started on bootup
[root@Srv25 pronet]# systemctl enable fixtures.service ln -s '/usr/lib/systemd/system/fixtures.service' '/etc/systemd/system/multi-user.target.wants/fixtures.service' [root@Srv25 pronet]# |
So that’s how it works..

This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

