Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu run script as user when usb inserted

I want to automatically attach any inserted usb drive to a virtual machine that is run inside ubuntu under particular user login.

To do that, I've set up the following udev rule(from [1], [2]):

KERNEL=="sd?", ENV{ID_BUS}=="usb", SUBSYSTEM=="block", RUN+="/bin/su me -c '/home/me/automout.sh'"

To test udev working, the automount.sh script was done as following:

#!/bin/bash 

echo "$devpath">/home/me/2

When run manually, the empty file is created inside home, but when a usb is inserted, nothing happens. What may be the cause of that?

like image 642
Dimitry Avatar asked Mar 03 '26 15:03

Dimitry


1 Answers

Indeed, as MiiinimalLogic suggested, the problem was owner of the script. If you want to run a script as another user via su, the script should belong to root.

The file may still reside in user's home.

like image 179
Dimitry Avatar answered Mar 06 '26 04:03

Dimitry