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?

I have solved creating two systemd services, the first checks the network and the second depends on the success of the first. It works.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.