You are not logged in.

#1 2022-10-11 07:05:24

mgazzin
Member
Registered: 2022-10-11
Posts: 4

Using variable with ConditionEnvironment in systemd service

I am using the following systemd service file:

[Unit]
After=multi-user.target
Description=cifscloak
ConditionEnvironment=homeNetwork=1

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/cifscloak mount -a -r 6
ExecStop=/usr/bin/cifscloak mount -u -a

[Install]
WantedBy=multi-user.target

ConditionEnvironment should evaluate the homeNetwork variable and start the service if the value is 1.
The problem is, how can I create an instance of this variable in the way the systemd service can read it?
I would like to use something like the following:

gw=$(ip r | grep default | head -1 | awk ' {print $3;}')
if [ "$gw" == "10.1.1.10" ]; then export officeNetwork=1; else export officeNetwork=0; fi
if [ "$gw" == "192.168.1.1" ]; then export homeNetwork=1; else export homeNetwork=0; fi

The problem is: how can I run this script before the systemd service runs?

Last edited by mgazzin (2022-10-11 07:06:59)

Offline

#2 2022-10-11 07:10:27

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Using variable with ConditionEnvironment in systemd service

No idea, but awk is smarter than that:

awk '/default/ {print $3;}' <(ip r)

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2022-10-11 07:21:37

mgazzin
Member
Registered: 2022-10-11
Posts: 4

Re: Using variable with ConditionEnvironment in systemd service

I am getting two lines and it does not solve my problem :-)

[dellg5 ~]# awk '/default/ {print $3;}' <(ip r)
10.1.1.10
10.1.1.10

Offline

#4 2022-10-11 07:22:01

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,015
Website

Re: Using variable with ConditionEnvironment in systemd service

This sounds like an X/Y issue.
Why do you want to depend on environment variables in the first place, if you don't even know how to use them?
Why do you think that they solve a problem you might encounter?
What is the reason why you want to depend on a set environment variable?

Probably a systemd unit dependency or a PID file are more adequate solutions to the problem you're facing.
In order to be able to evaluate this, we must know what problem you are really trying to fix.

That being said, global environment variables can be set via /etc/systemd/system.conf.
But again: This is probably not what you really want.

Last edited by schard (2022-10-11 07:23:23)


macro_rules! yolo { { $($tokens:tt)* } => { unsafe { $($tokens)* } }; }

Offline

#5 2022-10-11 07:23:02

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,965

Re: Using variable with ConditionEnvironment in systemd service

The literal answer, systemctl has a set-environment command which imports a certain environment into the manager instance. What I'd do if you wanted to do this per script is add an ExecStartPre= directive running your script and using systemctl set-environment to import the relevant environment variable to the system manager.

What you are also going to lack is a way of waiting for the network to be available, which is handled by waiting for network-online.target and activating the relevant invoking service of your networkmanager: https://wiki.archlinux.org/title/System … work_is_up

Offline

#6 2022-10-11 07:47:28

mgazzin
Member
Registered: 2022-10-11
Posts: 4

Re: Using variable with ConditionEnvironment in systemd service

Thanks for your answers, few points:

- I have already tried to use ExecStartPre= and it seems that this is only evaluated in the [Service] section and not in the [Unit] section
- I can use systemctl set-environment only from command line. I need these variables are set at boot time
- system.conf only accepts static variable and not variables defined with a script

What I need is a way to set dynamic calculated variables at boot so the my .service configuration file can evaluate it.

# systemctl set-environment officeNetwork=1
# systemctl start cifscloak.office

definitely works but I need to set this variable automatically at boot based on the default gateway of the network my laptop is connected to.

Offline

#7 2022-10-11 08:05:40

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,965

Re: Using variable with ConditionEnvironment in systemd service

I'd forget about environment variables, what you actually want is to run this service based on a specific network being available.

There are multiple approaches to this, another would be to create a "sibling" service that does nothing but determine whether you are in the correct network, fail when you are not which thus does not start your main service or do an ExecStartPre=/ExecCondition= directive but instead of caring about an environment variable, return a non-zero exit code if you are not in the network you want to be which will make ExecStart not get executed.

And the main condition you need to worry about still is invoking this only when you actually have a connection which means you need to depend on network-online.target regardless as otherwise your script and your service will be ran before the network is up and will fail guaranteed.

Last edited by V1del (2022-10-11 08:14:41)

Offline

#8 2022-10-11 09:30:21

mgazzin
Member
Registered: 2022-10-11
Posts: 4

Re: Using variable with ConditionEnvironment in systemd service

Ok, I think I have done with 2 services for checking network and other 2 services for starting the cifscloak service.
Thanks.

Offline

Board footer

Powered by FluxBB