Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variable in command path for ExecStart in systemd service [closed]

Im trying to make a systemd service like below :

[Unit]
Description=Syslog

[Service]
Type=simple
Environment="TESTEXTSERVICESFILES=/opt/test/extservices"
Environment="TESTCONFDATA=/storage/test/conf"

ExecStartPre=/bin/echo ${TESTEXTSERVICESFILES}/syslog/bin/nxlog $TESTCONFDATA
ExecStart=/opt/test/extservices/syslog/bin/nxlog -c ${TESTCONFDATA}/syslog/nxlog.conf
#ExecStart=/${TESTEXTSERVICESFILES}/syslog/bin/nxlog -c ${TESTCONFDATA}/syslog/nxlog.conf


[Install]
WantedBy=multi-user.target

After running 'sudo systemctl daemon-reload ; sudo systemctl start test-syslog ; sudo systemctl status test-syslog', I get the following success output:

● test-syslog.service - TestSyslog
   Loaded: loaded (/usr/lib/systemd/system/test-syslog.service; enabled; vendor preset: disabled)
   Active: deactivating (stop-sigterm) since Fri 2018-02-23 10:15:09 UTC; 11ms ago
  Process: 9474 ExecStart=/./opt/test/extservices/test-syslog/bin/nxlog -c ${TESTCONFDATA}/test-syslog/nxlog.conf (code=exited, status=0/SUCCESS)
  Process: 9471 ExecStartPre=/bin/echo /.${TESTEXTSERVICESFILES}/test-syslog/bin/nxlog $TESTCONFDATA (code=exited, status=0/SUCCESS)
 Main PID: 9474 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/test-syslog.service
           └─9478 /./opt/test/extservices/test-syslog/bin/nxlog -c /storage/test/conf/test-syslog/nxlog.conf

Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service: control process exited, code=exited status=0
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service got final SIGCHLD for state start-pre
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: About to execute: /./opt/test/extservices/test-syslog/bin/nxlog -c ${TESTCONFDATA}/test-syslog/nxlog.conf
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: Forked /./opt/test/extservices/test-syslog/bin/nxlog as 9474
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service changed start-pre -> running
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: Job test-syslog.service/start finished, result=done
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: Started Test Syslog.
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: Child 9474 belongs to test-syslog.service
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service: main process exited, code=exited, status=0/SUCCESS
Feb 23 10:15:09 lt-x260-1606.test.local systemd[1]: test-syslog.service changed running -> stop-sigterm

Here the service has started successfully. But when I comment the first ExecStart directive and uncomment the second one I get as failure :

● test-syslog.service - Test Syslog
   Loaded: loaded (/usr/lib/systemd/system/test-syslog.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2018-02-23 10:11:44 UTC; 11ms ago
  Process: 9243 ExecStart=/$TESTEXTSERVICESFILES/test-syslog/bin/nxlog -c $TESTCONFDATA/test-syslog/nxlog.conf (code=exited, status=203/EXEC)
  Process: 9239 ExecStartPre=/bin/echo /.${TESTEXTSERVICESFILES}/test-syslog/bin/nxlog $TESTCONFDATA (code=exited, status=0/SUCCESS)
 Main PID: 9243 (code=exited, status=203/EXEC)

Feb 23 10:11:44 lt-x260-1606.test.local echo[9239]: /./opt/test/extservices/test-syslog/bin/nxlog /storage/test/conf

This time the service cannot start, like it doesnt want to start the process starting by ${TESTEXTSERVICESFILES} variable. Does someone have any idea why it is not working even if command lines are the same in both cases ?

like image 650
Mistrale Avatar asked Feb 22 '18 17:02

Mistrale


People also ask

What is ExecStart in systemd?

ExecStart specifies the full path of a command that will be executed to start a service. Normally, the service output is directed to the journald component of systemd. We can check the output using the journalctl command. But, as there may be many services running and logging, the output of journalctl may be messy.

Where do I put systemd environment files?

Systemd Files and Paths Unit files are stored in the /usr/lib/systemd directory and its subdirectories, while the /etc/systemd/ directory and its subdirectories contain symbolic links to the unit files necessary to the local configuration of the host. We recommend putting your scripts in /etc/systemd/system .


1 Answers

You can't use variables in the actual command. systemd.service:

The command to execute must be an absolute path name. It may contain spaces, but control characters are not allowed.

You might wan't to wrap it in a shell command (which does parameter expansion):

ExecStart=/bin/bash -c '/${TESTEXTSERVICESFILES}/syslog/bin/nxlog -c ${TESTCONFDATA}/syslog/nxlog.conf'
like image 61
Jürgen Hötzel Avatar answered Sep 29 '22 01:09

Jürgen Hötzel