0

How to correctly write a service that will allow start aplication with start.sh where is a bash command and monitor the (.jar) application?

[Unit]
Description=Some service 
After=syslog.target
[Service]
User=userXService
Restart=always
RestartSec=300s
WorkingDirectory=/opt/test/target
ExecStart=/opt/test/start.sh 
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target

start.sh contains bash script with some environment variables:

#!/bin/bash
MEMORY="-Xms640m -Xmx640m"
HEAP_DUMP="-XX:+HeapDumpOnOutOfMemoryError"
SECURITY_RANDOM="-Djava.security.egd=file:/dev/./urandom"
REMOTE_JMX="-Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.rmi.port=16078 -Dcom.sun.management.jmxremote.port=16078 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

cd $DIRECTORY/target
java $HEAP_DUMP $MEMORY $SECURITY_RANDOM $REMOTE_JMX -jar test-application.jar --spring.profiles.active=database >> ../test-application.log 2>&1 &`
vidarlo
  • 6,654
  • 2
  • 18
  • 31
Dafik
  • 1
  • 1
  • What do you mean by *monitor*? WorkingDirectory should be a directory, not a jar file. – vidarlo Apr 17 '23 at 08:31
  • @vidarlo You're right! Thanks I had directory, but after a error I've changed it to jar location. I want to monitor a service (.jar) not a .sh file. But I'd like to start a java service from start.sh and everything is packed in linux service (systemd) – Dafik Apr 17 '23 at 08:45
  • What error did you get? – vidarlo Apr 17 '23 at 08:47
  • @vidarlo " ExecStart=/start.sh (code=exited, status=200/CHDIR)" That's werid bcs it says: "No such file or directory" but when i run the command from linux cmd then it works perfect! – Dafik Apr 17 '23 at 08:49
  • @vidarlo If I understand, if I leave the ExecStart alone where I run start.sh then linux-service will monitor the PID of start.sh instead of the application itself that start.sh is running – Dafik Apr 17 '23 at 08:51
  • Whats the content of your `start.sh`? – vidarlo Apr 17 '23 at 09:09
  • `start.sh` contains command to start java application with few parametres for java and application - it's generated when maven build application `cd $DIRECTORY/target java $HEAP_DUMP $MEMORY $SECURITY_RANDOM $REMOTE_JMX -jar test-application.jar --spring.profiles.active=database >> ../test-application.log 2>&1 &` of course $param is defined in filed but I don't put it here – Dafik Apr 17 '23 at 09:20
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/145394/discussion-between-vidarlo-and-dafik). – vidarlo Apr 17 '23 at 09:33

0 Answers0