3

I created my own init scripts that use variables:

#! /bin/sh

case "$1" in
    start)
        echo "Starting Public API"
        sudo -u techops sh ${JBOSS_HOME_PUBLIC_API}/bin/standalone.sh > ${PUBLICAPI_LOGGING_PATH} &
    ;;
    stop)
        echo "Stopping Public API"
        sudo -u techops sh ${JBOSS_HOME_PUBLIC_API}/bin/jboss-cli.sh --connect --controller=localhost:$((9990 + $PUBLICAPI_PORT_OFFSET)) command=:shutdown > ${PUBLICAPI_LOGGING_PATH} &
    ;;
    *)
        echo "Usage: /etc/init.d/publicapi {start|stop}"
        exit 1
    ;;
esac

exit 0

The variables are defined in /etc/environment and looks so:

PUBLICAPI_PORT_OFFSET=0
PUBLICAPI_LOGGING_PATH=/var/log/publicapi/publicapi.log
JBOSS_HOME_PUBLIC_API=/opt/publicapi

...and work after login, when I start and stop the init script manually, but they didn't work for the startup init scripts (which are symbolic links to /etc/init.d/publicapi in /etc/rc2.d/, /etc/rc5.d/, /etc/rc6.d/). The startup crashes then, because the variables are unknown.

I was able to fix this by executing systemctl edit publicapi which created a file /etc/systemd/system/publicapi.service.d/local.conf which looks like this after I edited it:

[Unit]
Description=Public API startup script
Documentation=no documentation

[Service]
Environment="JBOSS_HOME_PUBLIC_API=/opt/publicapi"
Environment="PUBLICAPI_PORT_OFFSET=0"
Environment="PUBLICAPI_LOGGING_PATH=/var/log/publicapi/publicapi.log"

when I reboot, the startup of the init script works. But now I have a strange situation: I always need to define the variable both in /etc/systemd/system/publicapi.service.d/local.conf and in /etc/environment. If I omit the ones from /etc/environment, the init boot scripts are executed but crashes, because the variables seem to be not set (at least after login). if I comment the variables in /etc/systemd/system/publicapi.service.d/local.conf and set them in /etc/environment only, the variables are set after login, but the init boot scripts are not executed. What is going on here? What is the scope of /etc/systemd/system/publicapi.service.d/local.conf (since the values are obviously gone after login or maybe not set)? How can I define the variables only once globally?

3
+25

What's going on here is that you are writing a system V init script on a system that runs systemd.

Your systemd is somewhat backwards compatible and will run these old-style init scripts in a sort of emulation, but treat them as systemd.

I think you'll have a better time abandoning your old init script and writing a new systemd service. This way you don't need /etc/environment.

Here is some further reading on how to write systemd services.

  • I need /etc/environment because I need it with Wildfly and other scripts as well. At least I already find a solution (see my answer) – Bevor Jan 19 at 11:47
1

I can answer the question myself now. After wasting half a day with this tinker crap I've got a solution that work. There are no init scripts anymore in /etc/init.d/ or /etc/rcX.d) or wherever:

*) /etc/environment

JBOSS_HOME_CONSENT_SERVER=/opt/consent-server
CONSENT_SERVER_LOGGING_PATH=/var/log/consentserver/consent-server.log
CONSENT_SERVER_PORT_OFFSET=3000

*) in /home/techops create a script consent-server with this content:

#!/bin/sh

case "$1" in
    start)
        echo "Starting Consent Server"
        sudo -u techops sh ${JBOSS_HOME_CONSENT_SERVER}/bin/standalone.sh > ${CONSENT_SERVER_LOGGING_PATH} &
    ;;
    stop)
        echo "Stopping Consent Server"
        sudo -u techops sh ${JBOSS_HOME_CONSENT_SERVER}/bin/jboss-cli.sh --connect --controller=localhost:$((9990 + $CONSENT_SERVER_PORT_OFFSET)) command=:shutdown > ${CONSENT_SERVER_LOGGING_PATH} &
    ;;
    *)
        echo "Usage: systemctl {start|stop} consent-server or pass {start|stop} as parameter"
        exit 1
    ;;
esac

exit 0

*) Create a file /etc/systemd/system/consent-server.service with this content:

[Unit]
Description=consent server startup script

[Service]
Type=oneshot
RemainAfterExit=yes
#if type=oneshot and RemainAfterExit=yes is not set, then the script stops immediately!
EnvironmentFile=-/etc/environment
WorkingDirectory=/home/techops
ExecStart=/home/techops/consent-server start
ExecStop=/home/techops/consent-server stop

[Install]
WantedBy=multi-user.target

*) Activate service (will create a symbolic link):

systemctl enable consent-server.service

After reboot, the service is starting automatically and all the variables are recognized. You can also start and stop the service with systemctl start consent-server systemctl stop consent-server

  • 1
    Congrats ! You wrote your first systemd service :-) – Robert Riedl Jan 19 at 12:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.