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.

Tim R
  • 143
  • 4
  • 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`? – Wieland Oct 04 '21 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. – Tim R Oct 04 '21 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? – Wieland Oct 04 '21 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. – Tim R Oct 04 '21 at 15:06
  • Interesting; have you made changes to the file, and done a daemon-reload (or used systemctl edit)? Seems like there's a syntax error that's not apparent (to me). – Jeff Schaller Oct 05 '21 at 12:01
  • @JeffSchaller I have made several changes or different try's including using `'ExecStart=bash -c 'test.startup'`, I reset it back to the way it is here, to make sure the error was correct. Each time I make a change I do a daemon-reload and then try to start. I agree, there's like a syntax error somewhere, I have yet to find it though. It is driving me up the wall, because I find something new on google, think it looks promising then fail, lol. – Tim R Oct 05 '21 at 12:41
  • Have you tried `systemd-analyze verify init-file-name-here`? Htt output could be helpful. – Jeff Schaller Oct 05 '21 at 12:46

2 Answers2

0

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
Mark Lakata
  • 854
  • 1
  • 10
  • 18
-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.