Why is my udev rule called twice?

0

I am working on CentOS 7. I wrote a udev rule in order to start a service each time a cd is inserted. The udev rule is the following :

KERNEL=="sr1", SUBSYSTEM=="block", ACTION=="change", ENV{ID_CROM_CD}=="1", ENV{DEVTYPE}=="1", RUN+="/bin/systemctl restart cd-mount@%k.service"

The service is configured as follow :

[Unit]
Description=Mount CD drive on %i

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/cd-mount.sh %i
ExecStop=/usr/local/bin/cd-mount.sh %i

For some reason my udev rule is called twice. This is pretty annoying since I am mounting the CD-ROM, I got errors because of that. How can I write my rule so it is called only once ?

EDIT : I have changed my udev rule and now the service is restarted only once. The udev rule looks like this :

KERNEL=="sr[0-1]", SUBSYSTEM=="block", ACTION=="change", ENV{ID_CROM_CD}=="1", ENV{DEVTYPE}=="disk", ENV{ID_FS_TYPE}=="udf" RUN+="/bin/systemctl restart cd-mount@%k.service"

There is one remaining problem. This rule detects that another CD is inserted only when the "eject" command is used. When I press the button eject, nothing happens. Does somebody know why ?

M.Brbr

Posted 2019-05-23T08:43:49.033

Reputation: 129

Are you sure it's the udev rule that is called twice? Or is it cd-mount.sh? systemctl restart means stop+start, so ExecStop+ExecStart, so (in your case) cd-mount.sh twice. If the rule was called twice, the script would run four times. Is this what happens? – Kamil Maciorowski – 2019-05-28T15:07:45.800

@KamilMaciorowski yes the script is run four times. – M.Brbr – 2019-05-29T08:28:32.053

No answers