Recipe for systemd: force restart of a service using a file flag

    Task:
    • there is a kind of self-written service launched and monitored from systemd;
    • when the flag file appears, restart this service;
    • You cannot do this from inside the service for religious reasons.

    Solution:
    restart-myservice.service
    [Unit]
    Description=Restart my cool service
    [Service]
    Type=oneshot
    ExecStartPre=/bin/rm -f /path/to/restart.flag
    ExecStart=/bin/systemctl restart myservice.service
    


    restart-myservice.path
    [Unit]
    Description=Path marker to restart my cool service
    [Path]
    PathExists=/path/to/restart.flag
    Unit=restart-myservice.service
    


    Do not forget to say systemctl start restart-myservice.pathand register restart-myservice.path in the Wantsmanaged service parameter .

    Limitations: The
    path unit works through the notify system call, and if the flag is on the path with the symlink, then when the symlink is rearranged (it was on the directory without the flag, it became with the flag) it most likely will not fire, I did not check.

    Additional materials:
    man systemd.path
    man systemd.unit

    Also popular now: