Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

using conflict and before (systemd)

For a little background, I have 2 systemd service units, UnitA and UnitB. UnitA is pulling from a code repository and building an application. UnitB is running the built application. UnitA is started by a timer, and UnitB runs after UnitA finishes. This happens at regular intervals. Sometimes UnitB runs for a long time, and UnitA will be started triggering a build and causing UnitB to throw a segfault and crash.

If I use Conflict= in UnitA by itself UnitB will be stopped before UnitA is started. That works.

If I put After= into UnitB by itself it will be ran after UnitA.

When I use both of these together, UnitB will be stopped when UnitA starts, but UnitB will never be started after UnitA completes.

Unit A.service:

[Unit]
Conflicts=UnitB.service

[Service]
User=myuser
Group=myuser
Type=oneshot
EnvironmentFile=/home/myuser/.env/.MyEnv
RemainAfterExit=no
ExecStart=/bin/sh -c "cd $GIT_DIR && git pull origin my_branch"
ExecStart=/bin/sh -c "dotnet publish $CSPROJ_ABS_PATH -r ubuntu.16.04-x64 -c Release -o $MY_BUILD_DIR"
ExecStart=/bin/sh -c "chmod -R 700 $OUTPUT_DIR"

[Install]
WantedBy=multi-user.target

UnitB.service:

[Unit]
After=UnitA.service

[Service]
User=myuser
Group=myuser
Type=oneshot
EnvironmentFile=/home/myuser/.env/.MyEnv
RemainAfterExit=no
ExecStart=/home/myuser/$MY_BUILD_DIR/myscript.sh

[Install]
WantedBy=UnitA.service

The documentation says that Conflict= is independent of Before and After: https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Conflicts=

I've considered adding an intermediate service that would launch the others, maybe after a pause. --UPDATE: Adding an intermediate step does not change the behavior.

Update: Implementation details aside, here's what I want to achieve:

UNIT A: Git pull & build dotnetcore application

=======> UnitB1: Run Application Instance 1 and exit when complete

=======> UnitB2: Run Application Instance 2 and exit when complete

Where UnitA runs first, and UnitB1 & UnitB2 run second concurrently.

Once all processes exit, the whole process should be restarted at some interval.

Comments