Autostarting an application just by inserting USB Stick

New features which should be implemented in Porteus; suggestions are welcome. All questions or problems with testing releases (alpha, beta, or rc) should go in their relevant thread here, rather than the Bug Reports section.
Post Reply
User avatar
ralcocer
Samurai
Samurai
Posts: 146
Joined: 02 Jan 2011, 12:53
Distribution: 3.2rc5 Xfce
Location: Puerto Rico
Contact:

Autostarting an application just by inserting USB Stick

Post#1 by ralcocer » 02 May 2015, 22:36

I found this http://www.unix.com/shell-programming-a ... evice.html however I have been unable to compile autorun http://linux.softpedia.com/get/Utilitie ... 5250.shtml has anyone ever tried this?. I want to autostart office when I plugin the USB stick.

Thanks
Rafael Alcocer

User avatar
brokenman
Site Admin
Site Admin
Posts: 5439
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

Re: Autostarting an application just by inserting USB Stick

Post#2 by brokenman » 02 May 2015, 23:05

Here is how I do it. I will assume your USB is plugged in at lies at: /dev/sdb1

Get the model of your USB.

Code: Select all

udevadm info -a -n sdb1 | grep model
ATTRS{model}=="silicon-power "

Create a udev rule that will run a script on plugin.

Code: Select all

cat /etc/udev/rules.d/98-usb-run.rules
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z]1", ATTRS{model}=="silicon-power ", RUN+="/usr/local/bin/run.sh"

Create the script to run:

Code: Select all

cat /usr/local/bin/run.sh
#!/bin/bash
touch /tmp/touched.txt

Make it executable:

Code: Select all

chmod +x /usr/loca/bin/run.sh
Reload your udev rules.

Code: Select all

udevadm control --reload-rules
Unmount your device. Remove it. Plug it back in and see that your script has run.

Be responsible. Don't anything stupid for example like put a porn video on your friends copy of porteus and run a script that launches it at full volume. It doesn't end well. :O:
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
brokenman
Site Admin
Site Admin
Posts: 5439
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

Re: Autostarting an application just by inserting USB Stick

Post#3 by brokenman » 03 May 2015, 00:06

If you are running anything other than a console job you may run into trouble. Probably due to the dbus communication and no display. I just saw that you are wanting to run office so follow this instead.

I found this method a while back for popping up a notification when plugging in a USB. It requires creating three files.

1. A udev rule
2. A dbus workaround script
3. Your main script to run

File 1: /etc/udev/rules.d/98-usb-run.rules

Code: Select all

ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z]1", ATTRS{model}=="silicon-power   ", RUN+="/home/guest/runguest.sh 'Important community announcement' 'Somebody just stuck something in your port!'"
File 2: /usr/local/bin/dbus-workaround.sh

Code: Select all

#!/bin/sh
user=guest
pids=`pgrep nm-applet`
title=$1
text=$2
timeout=$3
icon=$4

xhost local:guest
export DISPLAY=:0.0
 
if [ -z "$title" ]; then
 echo You need to give me a title >&2
 exit 1
fi
if [ -z "$text" ]; then
 text=$title
fi
if [ -z "$timeout" ]; then
 timeout=60000
fi
 
for pid in $pids; do
 # find DBUS session bus for this session
 DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS \
 /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`
 # use it

 #icon hack:
 if [ -z $icon ]; then
 DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
 notify-send -u low -t $timeout "$title" "$text"
 else
 DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
 notify-send -u low -t $timeout -i "$icon" "$title" "$text"
 fi
done
File 3: /home/guest/runguest.sh

Code: Select all

sudo -u guest /usr/local/bin/dbus-workaround.sh "$1" "$2" 6000
Using this I get a popup message through notify-send when I plug in. Just replace the bit about running notify-send with your GUI and you are good to go.
Again, on a serious note. Be aware of the HUUUGE security implications here.
How do i become super user?
Wear your underpants on the outside and put on a cape.

User avatar
brokenman
Site Admin
Site Admin
Posts: 5439
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v3.2rcX all desktops
Location: Brazil
Contact:

Re: Autostarting an application just by inserting USB Stick

Post#4 by brokenman » 03 May 2015, 00:34

Out of interest I wanted to see. I have reduced it to the following:

File 1: /etc/udev/rules.d/98-usb-run.rules

Code: Select all

ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z]1", ATTRS{model}=="silicon-power   ", RUN+="/home/guest/runguest.sh"
File 2: /home/guest/runguest.sh

Code: Select all

#!/bin/bash
xhost local:guest
export DISPLAY=:0.0
sudo -u guest /usr/bin/gnome-mplayer
That's all that is needed. The important part is the DISPLAY variable being exported, and running the GUI app as guest.
How do i become super user?
Wear your underpants on the outside and put on a cape.

Post Reply