Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 4794  / 3 Years ago, mon, september 20, 2021, 11:07:00

Trying to run a script when my phone is plugged in via USB. I made a udev rule in /etc/udev/rules.d/85-lazydroid.rule that looks like this:



ATTRS{idVendor}=="22b8", ATTRS{idProduct}=="428c", RUN+="/home/joel/.lazydroid"


And the script .lazydroid looks like this:



#!/bin/bash
exec adb forward tcp:8080 tcp:8080 &
exec chromium-browser 127.0.0.1:8080 --new-window &


The script itself runs fine, but the thing is, I can't get the script to run upon insertion of the phone.



It has the right ID according to lsusb | grep Motorola



Bus 002 Device 042: ID 22b8:428c Motorola PCS


Any ideas?



[EDIT]
Okay, now I know the udev rule is running, as it creates the symlink. I made some changes to the rule, see below:



SUBSYSTEM=="usb", ATTRS{idVendor}=="22b8", ATTRS{idProduct}=="428c", SYMLINK+="phone", RUN+="/bin/sh /home/joel/.lazydroid.sh"


But the script still won't run. And if I run it separately it still runs perfectly.


More From » usb

 Answers
6

You can test if your rule will be hit or not by running



udevadm test /path/to/sysfs/dev


You can find the devices sysfs node by using this:



udevadm info -q path -n /dev/sda  #To find sysfs node for first HDD


So to wrap that all together it'll be:



udevadm test $(udevadm info -q path -n /dev/sda) 2>&1 | more


Grep that for your script's name or read it line by line if you want. If your script is called but isn't executing then remember you don't have a typical environment in a udev script and so you must call all programs by their full path or otherwise recreate the environment to your liking. Try replacing adb and chromium-browser with their absolute paths (which adb and which chromium-browser)



Also the second exec in your bash script wont execute like you expect as the environment doesn't specify a window system for Chromium to launch in. I think I understand what you were trying to do here, but udev is designed to be non-interactive.



In response to your comment. There are a few abstract screens on every unix system referred to as displays. X11, which is the window manager (think explorer.exe, sort of)
for Ubuntu occupies one of them (7 or 8 I think, I work via ssh mostly). When you run a graphical program from the command line (say gedit) it will check the DISPLAY environment variable to determine which display it will draw itself on.



There's more to it that this, and I've never personally gotten a firm understanding of what the 'other stuff' is going on back there, but I'd try to do a few diagnostic things from your script:



mkdir /tmp/udev-script
/usr/bin/printenv > /tmp/udev-script/environment.log
/bin/echo "My script was run!" > /tmp/udev-script/script.log
DISPLAY=:8 # or :7, play around with that
export DISPLAY # Promote shell variable to environment variable
exec /path/to/chromium 2> /tmp/udev-script/chromium.log 1>&2 &
exit # This is important for udev, see sources


source1(udev)

source2(man udevadm)

source3(EXEC)

source4(DISPLAY)


[#39350] Tuesday, September 21, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alkeake

Total Points: 467
Total Questions: 94
Total Answers: 126

Location: Tajikistan
Member since Tue, Jun 15, 2021
3 Years ago
alkeake questions
Mon, Jul 5, 21, 09:24, 3 Years ago
Sun, Apr 23, 23, 03:29, 1 Year ago
Thu, Dec 22, 22, 20:30, 1 Year ago
;