I'm using this definition for a systemd
job:
[Unit]
Description=Some job
[Service]
ExecStart=/usr/local/sbin/somejob
User=dlt
Type=forking
[Install]
WantedBy=multi-user.target
The script is called is as follows (calling a simple routine that listens on a tcpip socket and appends the input to a file):
#!/bin/sh
cd /home/user/tmp/testout
nohup java -jar /home/user/programming/tests/java/core/SocketTest/SocketTest.jar </dev/null >/dev/null &
After systemctl start somejob
process shows as running, with init
as its parent:
user@CANTANDO ~$ ps -u dlt eo pid,ppid,command
PID PPID COMMAND
8718 1 java -jar /home/user/programming/tests/java/core/SocketTest/SocketTest.jar
After performing systemctl stop somejob
the process doesn't show anymore (and the port is closed).
So everything seems fine and dandy
My question is: Is this an acceptable solution for running a java daemon with systemd
, or are there caveats, and thus other more stable or secure ways to go about achieving this?
Best Answer
Here's some minor modifications:
network.target
.nohup
is not needed sincesystemd
will daemonize the executable for you.< /dev/null
and so forth) isn't needed since systemd sets up an appropriate standard I/O context. Indeed, if you take the redirection out systemd will log anything sent to standard output by the Java program in its journal, with no special logging mechanism required.&
) isn't needed or appropriate.Type=forking
, and if it isn't followed by the dæmon things go wrong. So try forType=simple
(orType=notify
).So the service file looks like this:
Notes:
java
as the name of the program to run. systemd doesn't searchPATH
for executables, and the name of the executable given toExecStart
must be absolute. So if you want path searching you have to invoke via a shell or/usr/bin/env
. We choose/bin/sh
here.Type=simple
the shell mustexec
Java, not run it as a child process. systemd controls the service through the main process, and that needs to be Java, not a parent shell process.sh
in its journal as the service name. See How to avoid /usr/bin/env being marked in systemd logs as the executable for more on this.As far as I know, there is no special caveat of running Java application with Systemd.