Script to keep kdeconnect connected

Sometimes when the network my laptop is connected to change, KDEconnect doesn’t connect itself again. A quick kdeconnect-cli --refresh fixes the issue. However doing this over time gets annoying. What I tried to do is, use networkmanager dispatcher scripts, so it would run the mentioned command whenever network state changes. But it tries to do it as root, so I get an error:

error activating kdeconnectd:
 QDBusError("org.freedesktop.DBus.Error.Disconnected", "Not connected to D-Bus server")

It is possible to run a systemd user service every few minutes to keep kdeconnect connected, but I would keep it as last resort.

Another solution that came to my mind was that the notification settings used to have (I cannot find it anymore) options to run scripts for different events, such as network connected or disconnected, now it seems to have gotten rid of the script options.
Any suggestions on what I can do? Thank you for your patience.

1 Like

If you want to create stuff in systemd for user, remove sudo and add the --user option.

systemctl --user status kdeconnect_refresh.timer (I just made up a timer to use in the example)

The configs for the user units goes best into ~/.config/systemd/user (just place them there like you do in /etc/systemd/system but interact with using --user).

You can make a systemd user timer that runs kdeconnect-cli --refresh

Something like:


Description=Run kdeconnect --refresh
# wait for network

ExecStartPre=/bin/sleep 5
ExecStart=/usr/bin/kdeconnect-cli --refresh


Description=Schedule kdeconnect-cli --refresh every 2 minutes


systemctl --user daemon-reload
systemctl --user enable --now kdeconnect_refresh.timer
systemctl --user status kdeconnect_refresh.timer

Thank you for you reply. Although I don’t really prefer a persistent service for this, however this does work.


…is pretty contradictive don’t you think?

Apologies if it was confusing, but the systemd service with a timer wasn’t a question in the original post, as I said myself I’m aware that it is possible, but I would keep it as last resort since I wanted to get something to work primarily based off network change.

So what is your question if not:

Because it’s hard to help if not understanding the question. :slight_smile:
And I’d like to help if I can.

If your question is “My connection to kde connect on my phone dies, how do I stop it from doing that?”
Then the answer is: In 99 out of 100 times when that happens, it’s a phone setting, not something on your computer. On android for example you have to disable the app going to sleep if inactive for some time. And that is a setting on your phone, not in the kde connect app.

I think I might have figured the misunderstanding, and it’s me and my “#¤”“Q%#Q”#% dyslexia… I switched places of “if” and “is”… TWO TIMES… Sorry about that…
But the question still stands, what was the question? xD

It’s all good. To summarise, I stated three probable ways of automating the kdeconnect-cli --refresh command usage that I know of:

  1. NetworkManager dispatcher script, which can run a script when network state changes, but it runs the command as root, so it is giving errors.
  2. The persistent systemd service with timer, which I don’t really prefer.
  3. The option to invoke a script within kde system settings when some notifications appear, such as network connected/disconnected, but I think the feature got removed since plasma 6.

So I was looking for other suggestions. For the phone part, if the kdeconnect app went into some sort of battery optimisation, a simple kdeconnect-cli --refresh wouldn’t be able to restore connection, so that is not the issue most certainly.

I have quickly looked at the networkmanager dispatcher for usage in combination to NFS once, but ended up doing something else.

But IIRC it is using a service unit?

If that is the case, try adding User=your_username to the [Service] section of the service that starts the script (that runs the --refresh command). Should run as your user.


etc etc etc


Another brilliant idea I just had, in case you want to run something as another user when starting as root, should work to privelege de-escalate so…

ExecStart=/usr/bin/bash -c "/usr/bin/su yourusername && /path/to/"

Or even:

ExecStart=/usr/bin/su -s /usr/bin/bash -c "/path/to/" yourusername

But I argue, remove the script completely and run kdeconnect-cli --refresh directly instead, unless you have other things happening in the script that can not be used directly in the service file.

Networkmanager dispatcher uses scripts, instead of a service unit. But I tried using sudo to run the command as my user instead of root with the script, still having the same error though, it just cannot find the dbus server.

error activating kdeconnectd: QDBusError("org.freedesktop.DBus.Error.Disconnected", "Not connected to D-Bus server")