Running a java application via a systemd service doesn't see additional directories (Rocky Linux) - Stack Overflow

I am trying to make a local machine restart to the new version control build of my java application. Le

I am trying to make a local machine restart to the new version control build of my java application. Let me run you through the flow:

  • .jar file is uploaded via ssh to a directory
  • a service is running a script that checks if the file has been changed/uploaded/etc
  • after the script checks that the file has been fully downloaded, it restarts the troublesome service
  • the troublesome service runs a shell script that only checks if the file is there and runs java -jar <build-file>.jar.

What I am stuck upon is that I have a media directory in the working folder (where the jar file is as well). The application runs normal, compiled videos but from time to time it also runs videos from the media directory (I need that because I have an external video source as well).

When I start the file with the command by myself, it works. When I start the script by myself, it works. But when the service starts it, the directory isn't seen.

This is the service:

[Unit]
Description=DMR Service

[Service]
WorkingDirectory=/home/ops/DMR
ExecStart=sh run.sh
Restart=on-failure
RestartSec=15s
StartLimitInterval=500
StartLimitBurst=15
TimeoutSec=300
StandardOutput=journal
StandardError=journal
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/ops/.Xauthority
User=ops
Group=ops

[Install]
WantedBy=multi-user.target

I checked the path, it's the same because the application runs. I checked the rights to the directory and I have access with my user. What am I missing?

Edit: Added the script /home/ops/DMR/run.sh to leave out the possibility of subdirectories without rights

#!/bin/sh
set -e

cd /home/ops/DMR
ls -l

file=$(find . -maxdepth 1 -type f -name "*.jar" | head -n 1)

if [ -z "$file" ]; then
    echo "No .jar file found in the directory."
    exit 1
fi

echo "$file is a .jar file"
java -jar $file

I will put an emphasis again on the fact that if I run the script as ops user, the whole thing works.

ls -l output on working directory:

drwxr-xr-x. 4 ops ops        29 Mar 20 15:25 data
-rw-r--r--. 1 ops ops 263641531 Mar 21 10:31 <application-name>.jar
drwxr-xr-x. 2 ops ops      4096 Mar 21 10:59 logs
-rwxr-xr-x. 1 ops ops       235 Mar 21 10:34 run.sh

Edit 2 - what I think could be the problem:

I am suspecting that the problem is some environment variable about ffmpeg. I need it only for the videos in the external directory to be played - this was the problem before installing it. The pre-compiled videos in the jar file are still played and running - as expected - but the ones that require ffmpeg are not.

I am trying to make a local machine restart to the new version control build of my java application. Let me run you through the flow:

  • .jar file is uploaded via ssh to a directory
  • a service is running a script that checks if the file has been changed/uploaded/etc
  • after the script checks that the file has been fully downloaded, it restarts the troublesome service
  • the troublesome service runs a shell script that only checks if the file is there and runs java -jar <build-file>.jar.

What I am stuck upon is that I have a media directory in the working folder (where the jar file is as well). The application runs normal, compiled videos but from time to time it also runs videos from the media directory (I need that because I have an external video source as well).

When I start the file with the command by myself, it works. When I start the script by myself, it works. But when the service starts it, the directory isn't seen.

This is the service:

[Unit]
Description=DMR Service

[Service]
WorkingDirectory=/home/ops/DMR
ExecStart=sh run.sh
Restart=on-failure
RestartSec=15s
StartLimitInterval=500
StartLimitBurst=15
TimeoutSec=300
StandardOutput=journal
StandardError=journal
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/ops/.Xauthority
User=ops
Group=ops

[Install]
WantedBy=multi-user.target

I checked the path, it's the same because the application runs. I checked the rights to the directory and I have access with my user. What am I missing?

Edit: Added the script /home/ops/DMR/run.sh to leave out the possibility of subdirectories without rights

#!/bin/sh
set -e

cd /home/ops/DMR
ls -l

file=$(find . -maxdepth 1 -type f -name "*.jar" | head -n 1)

if [ -z "$file" ]; then
    echo "No .jar file found in the directory."
    exit 1
fi

echo "$file is a .jar file"
java -jar $file

I will put an emphasis again on the fact that if I run the script as ops user, the whole thing works.

ls -l output on working directory:

drwxr-xr-x. 4 ops ops        29 Mar 20 15:25 data
-rw-r--r--. 1 ops ops 263641531 Mar 21 10:31 <application-name>.jar
drwxr-xr-x. 2 ops ops      4096 Mar 21 10:59 logs
-rwxr-xr-x. 1 ops ops       235 Mar 21 10:34 run.sh

Edit 2 - what I think could be the problem:

I am suspecting that the problem is some environment variable about ffmpeg. I need it only for the videos in the external directory to be played - this was the problem before installing it. The pre-compiled videos in the jar file are still played and running - as expected - but the ones that require ffmpeg are not.

Share Improve this question edited Mar 21 at 15:25 VicVerevita asked Mar 21 at 11:13 VicVerevitaVicVerevita 451 gold badge2 silver badges7 bronze badges 8
  • You did not write, how you run your shell script. In the way it is written, it suggests that you execute it as /bin/sh, which means (among others) that your bash startup files won't be processed. – user1934428 Commented Mar 21 at 14:11
  • When using ExecStart=/usr/bin/sh run.sh, there is no behavior change – VicVerevita Commented Mar 21 at 14:20
  • Of course not. In both cases it is run by sh. Is there a reason why you don't run it as bash script? – user1934428 Commented Mar 21 at 14:22
  • No intentional reason, to be honest. Ok, I changed the shebang to bash and i am running it with ExecStart=/usr/bin/bash run.sh. Same behavior though. I also tried adding the directory to the path in order to run it as ExecStart=run.sh, but systemd does not let me -> "bad unit file setting" – VicVerevita Commented Mar 21 at 14:39
  • Just a wild guess: How can systemd know, where run.sh is located? How about starting it by providing the full path to run.sh ? – user1934428 Commented Mar 21 at 14:51
 |  Show 3 more comments

2 Answers 2

Reset to default 0

If your user is not ops I would make sure the ops user has access too possibly by adding it to the group that does have access or if the ops group is the correct group ensure group permissions are appropriate for that directory, just in case inheritance has been overwritten or disabled, this should be checked on the sub directory containing the files to be processed.

Found the solution. Indeed if you have a look at the question and why I needed ffmpeg for the "external" videos to play, you can figure out that there might be an audio problem somewhere. Well, even though I don't play audio on the device I run my application, the video still has audio.

Now going further with the investigation, I found that Rocky Linux uses PipeWire as its audio driver (together with Alsa, but that didn't matter) and found the environment variables that PipeWire has for my user. Turns out it was XDG_RUNTIME_DIR - from here. Got the variable that my user had with the set command and now it's all up and running.

Thank you for the suggestions, @Alex Crichton and @user1934428. You kinda pointed me in the right direction, because at first I didn't think it could be an env var missing.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744358092a4570331.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信