Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

systemd script - environment file updated by ExecStartPre

I am trying out systemd script along in Docker environment.

Consider this:

command mentioned in ExecStartPre updating environment file and ExecStart actually making use of environment variable mentioned in env. file.? (all in the same systemd file).

like this:

[Unit]
Description=test service
Before=memcached.service

[Service]
Type=oneshot
EnvironmentFile=-/etc/sysconfig/testfile
ExecStartPre=/usr/local/bin/update_sysconfig_testfile.sh
ExecStart=/usr/bin/testmebinary $VOLUMES

[Install]
WantedBy=multi-user.target

Here, $VOLUMES is defined inside testfile and it is updated by update_sysconfig_testfile.sh script.

Will systemd aware about the change made by ExecStartPre (or) it just loads whatever value in testfile?

If there is any better approach, please share.

like image 349
kumar Avatar asked Mar 16 '17 13:03

kumar


People also ask

What is ExecStartPre?

ExecStartPre= , ExecStartPost= Additional commands that are executed before or after the command in ExecStart= , respectively. Syntax is the same as for ExecStart= , except that multiple command lines are allowed and the commands are executed one after the other, serially.

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 ExecStop Systemd?

The ExecStop setting is optional and is used to communicate with the service for a clean termination. The process specified by ExecStop will run in case the service crashes.


1 Answers

As an alternate approach, consider a script like with_testfile_vars doing the following:

#!/bin/sh
export foo=bar   # export the same calculated values you would otherwise write to the file
export baz=qux
exec "$@"        # then invoke your "real" program

...with a service file using that wrapper:

[Unit]
Description=test service
Before=memcached.service

[Service]
Type=oneshot
ExecStart=/usr/local/with_testfile_vars /usr/bin/testmebinary $VOLUMES

[Install]
WantedBy=multi-user.target

No EnvironmentFile needed at all, no ordering dependencies around how exactly it's interpreted, no concerns about how systemd's parsing differs from a shell's, etc.

like image 71
Charles Duffy Avatar answered Sep 22 '22 13:09

Charles Duffy