Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write systemd environment variables value which contains =

Tags:

linux

systemd

In systemd units file, I have a Environment which content is key=IamValue=abc, as you can see the value is IamValue=abc which contains =.

For this situation, how can I write the unit files?

I have tried as following, but it seems invalid:

[Unit]
Description=...

[Service]
WorkingDirectory=...
ExecStart=...
Restart=always
RestartSec=10                                          
SyslogIdentifier=...
User=root
Environment=key="IamValue=abc"
like image 648
Jerry Bian Avatar asked Apr 07 '17 13:04

Jerry Bian


People also ask

How do I find systemd environment variables?

If your service is running, you can use systemctl status <name>. service to identify the PID(s) of the service process(es), and then use sudo strings /proc/<PID>/environ to look at the actual environment of the process.

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 .

What is Environmentfile?

Description. The /etc/environment file contains variables specifying the basic environment for all processes. When a new process begins, the exec subroutine makes an array of strings available that have the form Name=Value. This array of strings is called the environment.


2 Answers

I tested that this works in a test.service file:

[Unit]
Description=Hi

[Service]
Type=oneshot
Environment=key="IamValue=abc"
ExecStart=/bin/bash -c "/bin/echo key:$key"

If you run that and then do journalctl -u test, you can see the key containing the equal sign works.

I've proposed an update to the official systemd docs to better clarify this case.

like image 84
Mark Stosberg Avatar answered Sep 27 '22 03:09

Mark Stosberg


Alternatively use EnvironmentFile, config line in unit file:

EnvironmentFile=-/etc/sysconfig/test

With content in /etc/sysconfig/test:

key="IamValue=abc"
like image 22
shizhz Avatar answered Sep 26 '22 03:09

shizhz