Welcome! If this is your first visit, be sure to check out the FAQ. You will have to register before you can post in the forums. (Be aware the forums do not accept user names with a dash "-") Also, logging in lets you avoid the CAPTCHA verification when searching . Select Articles, Forum, or Blog. Posting in the Forums implies acceptance of the Terms and Conditions.

Script run from udev rule gets killed shortly after start

Hi everyone.
I've written a udev rule to backup data in a usb stick when plugged in (that is backup data FROM usb to HDD).
Before updating to 12.3 the script worked fine. I can't recall if something else came in the way, but after updating the script gets killed shortly after starting. The device mounts, rsync starts and the detached script gets killed killing rsync and leaving the device mounted.

Re: Script run from udev rule gets killed shortly after start

That won't work. RUN is for short program invocations only and udev enforces this. Also, it would have been killed by systemd anyway when parent udev exits.

The clean way to do it using modern systemd/udev infrastructure.

1. Create systemd service that starts you backup script on request. Do not put it in background! I.e. remove { ... } & wrapper. Otherwise system will believe your script finished as soon as it is started:

This is template unit (it has "@" in its name). Later you will dynamically instantiate template by calling it with device name. You refer to device name using %i and %I placeholders (latter is capital "i" not small "L"). BindsTo ensure service is stopped when device in unplugged.

Re: Script run from udev rule gets killed shortly after start

That won't work. RUN is for short program invocations only and udev enforces this. Also, it would have been killed by systemd anyway when parent udev exits.

The clean way to do it using modern systemd/udev infrastructure.

1. Create systemd service that starts you backup script on request. Do not put it in background! I.e. remove { ... } & wrapper. Otherwise system will believe your script finished as soon as it is started:

This is template unit (it has "@" in its name). Later you will dynamically instantiate template by calling it with device name. You refer to device name using %i and %I placeholders (latter is capital "i" not small "L"). BindsTo ensure service is stopped when device in unplugged.

Re: Script run from udev rule gets killed shortly after start

First of all thank you all for your prompt replies.

@deano_ferrari

Thanks for the interest. No errors or info in the log whatsoever.

@arvidjaar
SOOO elegant solution. I didn't have any idea I could make it work with systemd.
Works as promised!
Two questions though:
1) When plugging in the device, it mounted instantly in KDE and unmounted before I the service starts. No major issue here. In the other hand the device still is available for mount in KDE. Haven't tried it but I'm assuming that trying to mount while the backup service runs will fail, right?

2) I want to have a visual indication as to when the backup starts (so as not to try to mount it) and ends (so that I can mount and use it). I found someone used notify-send setting the missing DBUS_SESSION_BUS_ADDRESS env here: Ubuntu: backup to USB drive on mount | Ninetynine.be

Would you say this is a lean way to do it or should I find some other way?

Re: Script run from udev rule gets killed shortly after start

1) When plugging in the device, it mounted instantly in KDE and unmounted before I the service starts.

I understand that it is mounted, but I'm surprised it is unmounted, I do not know who does it.

No major issue here. In the other hand the device still is available for mount in KDE. Haven't tried it but I'm assuming that trying to mount while the backup service runs will fail, right?

As your script mounts device, I expectudisks (and user session disk managers using it) will notice that fact and won't offer to mount partition again. You can also prevent automount by setting ENV{UDISKS_AUTO}="0" or set it to be completely ignored by setting ENV{UDISKS_IGNORE}="1" device property in udev rule. I assume KDE in 12.3 finally switched to using udisks2.

2) I want to have a visual indication as to when the backup starts (so as not to try to mount it) and ends (so that I can mount and use it). I found someone used notify-send setting the missing DBUS_SESSION_BUS_ADDRESS env here: Ubuntu: backup to USB drive on mount | Ninetynine.be

Would you say this is a lean way to do it or should I find some other way?

I guess it will work. Unfortunately this is the same problem as with running X11 program outside of user context - you need to know DBUS session address (X11 DISPLAY number) before you can try to communicate. The clean way is to have some small program started in user session and listening on system bus for signals and forwarding them to local session notification manager. Signals are broadcast to all users so sending program does not really need to know how many listeners there are nor their address. But solution described in the link will do as well as long as you need to notify only one and the same user always.