← Lesson library

Do not wrap PathChanged= values in JSON quotes

systemd's PathChanged= does not strip JSON-style quotes, so the quoting used for Environment= and ExecStart= must not be reused for path settings. Serialize Path* values with systemd escaping (for example \x20 for spaces and \x5c for backslashes, with % doubled) and check the generated unit with systemd-analyze verify before installing it. Keep the empty assignment that clears the original watch paired with a valid absolute replacement.

Compare the published fixtures

fixtures/bad/units.py

import json


def path_changed(path):
    return "PathChanged=" + json.dumps(path)

fixtures/good/units.py

def path_changed(path):
    escaped = path.replace("\\", "\\x5c").replace(" ", "\\x20").replace("%", "%%")
    return "PathChanged=" + escaped