Start bash script when connecting external drive?

Hello,

I’m not certain if this KDE forum is the right place, but since my question involves some KDE functions and dialogs, I decided to start here.

The situation: I’m using a simple bash script based on rsync on an MX Linux 23 KDE machine for backing up several important directories to an external drive. This script is still somewhat hands-on: it automatically mounts upon connecting, but I still run the back-up script from a terminal window. Afterwards, I unmount it and remove the drive.

Now for me this CLI-based approach is no problem at all, but I’d like to streamline this procedure for my less CLI-oriented users running the same MX LInux distro. So basically, what I’m looking for is a way to autostart the back-up script as soon as the back-up drive is mounted by KDE.

The user only needs to confirm making a back-up, and should receive a message to disconnect the drive after it completes - which I can do with kdialog, no problem there.

But is there a simple way to autostart a bash script (or any other command, for that matter) when automounting an external drive?

Not exactly KDE-specific, but what you need is a UDEV rule that triggers a systemd service:

Thanks for your suggestion. However, I don’t want to get deep into systemd and udev here, since it’s just one simple user space script that I want to start. I was hoping to hitch a ride on KDE’s automatic (and configurable) handling of certain events, such as mounting a known drive upon connecting.

I think I’ll leave it to the user to click the backup script after plugging in the backup drive. I’ll just add a simple check whether it is indeed the backup drive that is mounted, exiting with an error message if it is not. It can handle all other user interaction such as the finishing notification with kdialog.