Last active
July 17, 2026 18:08
-
-
Save PhrozenByte/da0e61d2ab326ed917722ab353b4c4b5 to your computer and use it in GitHub Desktop.
NetworkManager dispatcher script to start/stop a Systemd unit when a connection's status changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # nm-systemd-dispatcher.sh | |
| # Starts/stops a Systemd unit when a connection's status changes. | |
| # | |
| # Install this script to `/etc/NetworkManager/dispatcher.d/60-systemd.sh` and | |
| # add the following section to your NetworkManager connection config in | |
| # `/etc/NetworkManager/system-connections/*.nmconnection`: | |
| # | |
| # [user] | |
| # systemd.unit=myservice.service | |
| # systemd.start=up | |
| # systemd.stop=down | |
| # | |
| # The `systemd.start` and `systemd.stop` options are optional and can be used | |
| # to change what connection status yields what `systemctl` command. For | |
| # example, by setting `systemd.start=pre-up` you can start the Systemd unit | |
| # with the `pre-up` status instead. Next to `start` and `stop` the dispatcher | |
| # script also supports `reload`, `restart`, `try-restart`, `kill`, `freeze`, | |
| # and `thaw`. Remember that you need to symlink the dispatcher script to | |
| # `/etc/NetworkManager/dispatcher.d/pre-{up,down}.d/` to use `pre` actions. | |
| # | |
| # Copyright (C) 2025-2026 Daniel Rudolf (<https://www.daniel-rudolf.de>) | |
| # License: The MIT License <http://opensource.org/licenses/MIT> | |
| # | |
| # SPDX-License-Identifier: MIT | |
| print_usage() { | |
| echo "Usage:" | |
| echo " ${BASH_SOURCE[0]} <interface> <action>" | |
| } | |
| [ $# -ge 2 ] || { print_usage >&2; exit 1; } | |
| INTERFACE="$1" | |
| ACTION="$2" | |
| [ -n "$INTERFACE" ] && [ -n "$ACTION" ] || exit 0 | |
| [ -n "${CONNECTION_USER_SYSTEMD__UNIT:-}" ] || exit 0 | |
| case "$ACTION" in | |
| "${CONNECTION_USER_SYSTEMD__START:-up}") | |
| systemctl start "$CONNECTION_USER_SYSTEMD__UNIT" | |
| ;; | |
| "${CONNECTION_USER_SYSTEMD__STOP:-down}") | |
| systemctl stop "$CONNECTION_USER_SYSTEMD__UNIT" | |
| ;; | |
| "${CONNECTION_USER_SYSTEMD__RELOAD:-}") | |
| systemctl reload "$CONNECTION_USER_SYSTEMD__UNIT" | |
| ;; | |
| "${CONNECTION_USER_SYSTEMD__RESTART:-}") | |
| systemctl restart "$CONNECTION_USER_SYSTEMD__UNIT" | |
| ;; | |
| "${CONNECTION_USER_SYSTEMD__TRY_055RESTART:-}") | |
| systemctl try-restart "$CONNECTION_USER_SYSTEMD__UNIT" | |
| ;; | |
| "${CONNECTION_USER_SYSTEMD__KILL:-}") | |
| systemctl kill "$CONNECTION_USER_SYSTEMD__UNIT" | |
| ;; | |
| "${CONNECTION_USER_SYSTEMD__FREEZE:-}") | |
| systemctl freeze "$CONNECTION_USER_SYSTEMD__UNIT" | |
| ;; | |
| "${CONNECTION_USER_SYSTEMD__THAW:-}") | |
| systemctl thaw "$CONNECTION_USER_SYSTEMD__UNIT" | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment