Forcing work directory when executing jar file in systemd - java

I am attempting to start a jar from a systemd service, in linux. The jar that I am executing assumes that within the current directory, there is an xml file. I am passing the name of that file, with the -config argument. The sample service I have created is below:
[Unit]
Description=my service
After=network.target
[Service]
Type=simple
Environment="xml_file_name=sample.xml"
ExecStart=/usr/bin/java -jar /path/to/jar/myapp.jar -config ${xml_file_name}
The service file above is placed in the /usr/lib/systemd/system directory, and is called myservice.service. I am doing the following commands to start it:
systemctl daemon-reload
systemctl stop myservice.service
systemctl start myservice.service
systemctl status myservice.service
The systemctl status myservice.service command shows that the jar file ran, but my application says that it cannot find ${xml_file_name}.
In addition, my jar states that it was executed from the / directory. I believe that this is part of the problem, because the ${xml_file_name} is only applicable in the /path/to/jar/ directory.
Things tried:
-Xbootclasspath/p:"/path/to/jar/": prepend the path of the jars
location, so that maybe the ${xml_file_name} can be seen.
changed the /path/to/jar/ to make sure it has all possible permissions enabled
I tried to add User=root under the [Service] section of my systemd service, but it made no change. Either way, only root user is on the machine, and the permissions all seem to check out.
What makes this even more strange is that if I cd to / , and then manually execute:
/usr/bin/java -jar /path/to/jar/myapp.jar -config sample.xml
everything words just fine.
Is there something evident that I am missing here? Is it possible to tell systemd service, execute this java jar, but make sure that the working path is /path/to/jar/ as opposed to / ?

To solve this, I ended up using the following attribute under the [Service] section:
[Service]
...
WorkingDirectory=/path/to/jar
ExecStart=/usr/bin/java -jar my.jar -config sample.xml
Solved the problem!

Try following:
cd /path/to/jar && /usr/bin/java -jar /path/to/jar/myapp.jar -config sample.xml
If you use && in Linux like systems, it combines the two commands in the way the second will be executed only after the first will be executed successfully. So basically it would first change the working directory and then run the java jar command from the changed directory, which could have solved the problem.

Related

How to properly and safely run tomcat at IntelliJ and Linux

The problem
I installed and configured Tomcat on my Linux Mint for use in IntelliJ.
In the configuration process, I gave permissions, for security reasons, to a new user group (named tomcat) and to another new user (also named tomcat).
I did it because of some tutorials like the one from Digital Ocean that says:
For security reasons, you should NOT run tomcat using root user. We shall create a non-login user called tomcat (in group tomcat) to run the server.
So, i executed:
$ sudo chgrp -R tomcat /opt/tomcat
$ sudo chmod -R g+r conf
$ sudo chmod g+x conf
$ sudo chown -R tomcat webapps/ work/ temp/ logs/
So my folder looks like this:
However, when I run Tomcat, I get the following error:
org.apache.catalina.startup.ContextConfig.beforeStart Exception fixing docBase for context [/edi]
java.io.IOException: Unable to create the directory [/opt/tomcat/apache-tomcat-9.0.33/webapps/edi]
What i've tried
As the error implies that it is something related to user permissions, I gave permission to everyone in the folder /opt/tomcat/apache-tomcat-9.0.33 using:
$ sudo chmod -R 777
The doubt
I would like to know what I can do to keep the permission settings for security and at the same time run Tomcat via IntelliJ.
Can I force IntelliJ to run with the user tomcat? If so, how can I do it?

How to run jar files sequentially from a shell script

I am trying to run two java application one after other in my docker container.
In my dockerfile i have specified invoker.sh as the entry point.
ENTRYPOINT ["sh", "/opt/invoker.sh"]
Then i use this script to run two jar files.
#!/bin/sh
java -jar loader.jar
java -jar service.jar
but this does not work. It gives
Error: Unable to access jarfile javaimpl-loader.jar
and only the service.jar is executed. When i tried echo $(ls) it shows that both the jar files are there.
but if i change the script to
#!/bin/sh
echo $(java -jar loader.jar)
java -jar service.jar
then both the jars work. Why cant i use the 1st script. any help regarding this highly apreciated.
It appears the first example is being treated as a single line, you could work with that. Also, I would prefer bash to /bin/sh. Like,
#!/usr/bin/env bash
java -jar loader.jar && java -jar service.jar

"Activemq not found" error after running custom Docker image

We have a legacy application that I am trying to dockerize. The jar of the application has both the application and an activemq bundled together. (We cannot change the way it is built). And has certain installation steps. I created the following initial Dockerfile for this however I am facing an issue (mentioned after the Dockerfile) when I run the image.
The Dockerfile looks like this :
FROM registry:4000/openjdk:8-jre-alpine
RUN addgroup -S appuser && adduser -S -G appuser appuser
ADD ./fe.jar /home/appuser
RUN chmod +x /home/appuser/fe.jar \
&& chown appuser:appuser /home/appuser/fe.jar
USER appuser
RUN ["java", "-jar", "/home/appuser/fe.jar", "-i"]
WORKDIR /home/appuser/fe/activemq/bin
CMD ["/bin/sh", "-c", "activemq"]
The RUN command extracts the application and the activemq at the location into folder called fe.
The WORKDIR seems to be setting the working directly to activemq/bin. I confirmed this by using sh script which triggers when the image is run. In the sh script I trigger an ls and pwd command to see the contents and the location.
However when I run the image which triggers the CMD command I get the error that :
/bin/sh: activemq: not found
What can be the possible issue here?
If activemq is an executable in your bin directory (and not in PATH) then you need to edit your CMD:
CMD ["/bin/sh", "-c", "./activemq"]
Also make sure that your script is executable.
Found the problem. The activemq script starts with #!/bin/bash and I am trying to run it using sh. I need to first install bash in the image and then run the activemq script using one.
I got the hint from this answer : docker alpine /bin/sh script.sh not found
Now it moved ahead however the container dies after running immediately. Not sure what the issue is. Doesn't even give any error.

Can run jar from commandline but not in shell script - why?

I want t start a jar which should provide a Rest-WebService.
When I run following command from terminal the jar and the webservice starts successfully:
java -jar SchnittprofilService-1.0-fat.jar BH121 8888
If I run the same command in a shell script start.sh, the jar starts but the not the webservice.
The permission for start.sh is set to 777.
Any suggestions?
Oops:
You have to put your arguments in quotation marks:
java -jar SchnittprofilService-1.0-fat.jar "BH121" "8888"

using rc.local to start a jar file at startup is not working

I have a .jar file I want to run whenever the system reboots/starts, so I put the line
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
in my /etc/rc.local file. The program is validated as working, and if I run the above command at the command line the program works as expected.
Other versions I have tried without success:
nohup /usr/bin/java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
and:
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar 2> /dev/null \ .. &
I am running centos 6.4.
Check that your jar file is accesible roots, NFS mounted volumes may impose special restrictions for root.
Instead of discarding your error messages, you might want to route them to syslog, something like 2> /sbin/logger -t FOO 1> /sbin/logger -t BAR
Maybe the path isn't set yet at startup time and you need the full path to the java executable or, possibly, nohup.

Categories

Resources