You are not logged in.

#1 2020-06-03 18:23:39

DAMO238
Member
Registered: 2020-02-25
Posts: 51

[SOLVED] Run script after systemd automounts

I have a usb stick that I regularly plug in and out to write a file to. I have therefore tried to automate this process as much as possible using systemd. I have managed to get it to auto mount when I access it with cura (software doing the writing) and after 10 seconds of idle time, it auto unmounts. However, I would like it to make a sound when it unmounts, so that I know it is safe to remove. Here is the relevant fstab line:

UUID=422F-11B0                                  /home/damien/.mnt       auto    rw,user,noexec,nofail,x-systemd.automount,x-systemd.device-timeout=1sec,x-systemd.idle-timeout=10sec,noauto,gid=1000,uid=1000   0   2

After doing some digging, it appears that this causes systemd to create an 'automount' service, of which the following is the ouput of 'systemctl show home-damien-.mnt.automount':

Where=/home/damien/.mnt
DirectoryMode=0755
Result=success
TimeoutIdleUSec=10s
Id=home-damien-.mnt.automount
Names=home-damien-.mnt.automount
Requires=home.mount -.mount
WantedBy=local-fs.target
Conflicts=umount.target
Before=home-damien-.mnt.mount local-fs.target umount.target
After=home.mount local-fs-pre.target -.mount
Triggers=home-damien-.mnt.mount
RequiresMountsFor=/home/damien
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
Description=home-damien-.mnt.automount
LoadState=loaded
ActiveState=active
SubState=waiting
FragmentPath=/run/systemd/generator/home-damien-.mnt.automount
SourcePath=/etc/fstab
UnitFileState=generated
UnitFilePreset=disabled
StateChangeTimestamp=Wed 2020-06-03 18:49:52 BST
StateChangeTimestampMonotonic=174274092
InactiveExitTimestamp=Wed 2020-06-03 18:47:07 BST
InactiveExitTimestampMonotonic=8845059
ActiveEnterTimestamp=Wed 2020-06-03 18:47:07 BST
ActiveEnterTimestampMonotonic=8845059
ActiveExitTimestampMonotonic=0
InactiveEnterTimestampMonotonic=0
CanStart=yes
CanStop=yes
CanReload=no
CanIsolate=no
StopWhenUnneeded=no
RefuseManualStart=no
RefuseManualStop=no
AllowIsolate=no
DefaultDependencies=yes
OnFailureJobMode=replace
IgnoreOnIsolate=yes
NeedDaemonReload=no
JobTimeoutUSec=infinity
JobRunningTimeoutUSec=infinity
JobTimeoutAction=none
ConditionResult=yes
AssertResult=yes
ConditionTimestamp=Wed 2020-06-03 18:47:07 BST
ConditionTimestampMonotonic=8839543
AssertTimestamp=Wed 2020-06-03 18:47:07 BST
AssertTimestampMonotonic=8839544
Transient=no
Perpetual=no
StartLimitIntervalUSec=10s
StartLimitBurst=5
StartLimitAction=none
FailureAction=none
SuccessAction=none
InvocationID=9774a43ca1474c229b9c3dcb2347c725
CollectMode=inactive

What I have not been able to find out, is that, if I edit this service, the changes will persist between boots, nor is it obvious how I can edit this to make it run a script. The ideal solution would be an fstab parameter like 'x-systemd.unmount-script=<location_of_script>', but I have not found anything of the sort.

Last edited by DAMO238 (2020-06-04 17:52:17)

Offline

#2 2020-06-03 20:54:04

seth
Member
Registered: 2012-09-03
Posts: 51,046

Re: [SOLVED] Run script after systemd automounts

/run is tmpfs and won't survive a reboot (nor is this designed to be)

Would it be an option to run a script (mount, run cura, unmount, play ping) whenever you plug in the USB key?
(udev rule using the UUID or label of the partition)

Offline

#3 2020-06-04 14:17:09

DAMO238
Member
Registered: 2020-02-25
Posts: 51

Re: [SOLVED] Run script after systemd automounts

I had a look at the udev wiki page, and it appears that the rules only work for physically plugging in and out the device, not for mounting and unmounting. Since I need to unmount before unplugging (it corrupted one of my files when I didn't), this solution doesn't quite work for my needs. On the plus side, I know more about udev rules now, so thanks!

Offline

#4 2020-06-04 14:28:17

seth
Member
Registered: 2012-09-03
Posts: 51,046

Re: [SOLVED] Run script after systemd automounts

What I meant you could do is have a rule that
- recognoizes the device
- mounts it
- copies stuff
- unmounts the device
- plays a nifty "pinggg"

So you'd simply plug the key, wait for the pinggggggg sound and unplug it. No further action needed.

Offline

#5 2020-06-04 17:51:53

DAMO238
Member
Registered: 2020-02-25
Posts: 51

Re: [SOLVED] Run script after systemd automounts

That would have almost work, but I wanted to save the file directly to the usb stick. I came up with an alternate solution. I made a (janky) i3blocks block that shows if something is mounted or not:

#!/bin/sh

UUID="${UUID:~$BLOCK_INSTANCE}"
TEXT=$(lsblk -o UUID,NAME,MOUNTPOINT | grep -i $UUID | awk '{ print $2 " => " $3 } END { print NR }' | sed 's/└─//g ; s/├─//g ; s/^0$/Not Mounted/g ; s/^1$//g ; /^$/d ; s/^sd/\/dev\/sd/g')

echo $TEXT

Offline

Board footer

Powered by FluxBB