4

I have been trying to get a service file setup for our software so that it can be started and stopped. When I manually run the commands, the server starts normal. I next took this an implemented this into a bash script and it also starts normal. The bash script:

#!/bin/bash
/bin/cd /opt/software/test/; source software_env.sh
export TEST_APP="/opt/software/testapp"
/bin/cd /opt/sotware/test/bin/; startserver

I tried to just implement those commands into a system file, but cannot seem to get it write. So the next thing I tried was to leave the bash script and call it from a system file that looked like:

[Unit]
Description=Test Service

[Service]
Type=forking
ExecStart=/opt/software/test/test.startup

[Install]
WantedBy=multi-user.target

I have also tried setting the servive type as Type=simple but that also does not work. I can successfully start the server by running the bash script via cron at reboot, but if there is a way to get that bash script into a service file that is ultimately what I am looking to do.

The difficulty appears to be getting the source and export to work properly in the unit file. I also noticed some strange behavior when I was getting the bash script to work, that running: source /opt/software/test/software_env.sh instead of /bin/cd /opt/software/test/; source software_env.sh does not work.

The error from systemd reguardless of the different techniques to try to get it to run have all given this error.

test.service - Test service
Loaded: bad-setting (Reason: Unit test.service has a bad unit file setting.)
Active: failed (Result: exit-code)
Main PID: 2778 (code=exited, status)

Thank you for any and all suggestions.

8
  • Is test.startup the same as the bash script you posted, or is it something different? Are you trying to run source and export in the service file directly or in test.startup? Commented Oct 4, 2021 at 13:42
  • test.startup is the bash script posted, source specifically only works if I have changed into the /opt/software/test directory. The export command does not care where it is run from. And I did also double check that there is not a separate source file inside the directory where it works. Commented Oct 4, 2021 at 14:09
  • I see. Then you have to be more precise what "does not work" mean, i.e. an error message or similar. Also, export TEST_APP-... should be export TEST_APP=..., right? Commented Oct 4, 2021 at 14:16
  • Yes, it should be export TEST_APP=..., in terms of the specifics, the error message when I try to start it as a service just says failed with no other useful information. But if I instead just run the bash script it all works as expected. I wasn't sure if there might be a way to run source and export inside the unit file instead, I could not find anything online indicating how to properly do that. Commented Oct 4, 2021 at 15:06
  • 1
    Loaded: bad-setting (Reason: Unit test.service has a bad unit file setting.) Could you add the output of:systemd-analyze verify test.service? It should give you more info about what is wrong... Most likely a problem with access permissions or no execute bits set on the script used in ExecStart=. Commented Sep 4, 2023 at 16:03

2 Answers 2

1

systemd cannot take shell command lines directly. So you can pass a shell implementation of some sort to source a file. In your case, you can try to modify your unit like this:

ExecStart=/bin/sh -c '. /opt/software/test/software_env.sh'

Freedesktop has a manual on systemd, you can check it out.

1
  • I think this is the way to do it. I wish there was a more specialized way but this works. Commented Sep 16 at 8:30
1

I don't know what your source file looks like, but if it is a simple VAR=VALUE format, then you can use the EnvironmentFile feature.

[Service]
EnvironmentFile=/opt/software/test/software_env.sh
WorkingDirectory=/opt/software/test
Environment=TEST_APP=/opt/software/testapp
Type=forking
ExecStart=/opt/software/test/test.startup

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.