Page 1 of 1

Start script on switch success

Posted: 07 Dec 2011, 12:54
by claes508
Hello,

Sorry if this is a simple question. I want to start my own script after a successful switch. How should I do it? I would like to avoid tampering with udev rule so it get overwritten on next update.

:shock: Classee

Posted: 07 Dec 2011, 21:42
by Josh
That should not be too hard.

You can easily add your own udev rule file, like 90-my.rules or whatever, to the rules folder; it will never be overwritten.
In the file, check for the target ID, just like the usb_modeswitch rules do for the default IDs.

You can copy the line that says SYSTEM!="usb" ....
Don't use the line that checks the interface class.
Copy the line with the end marker (to the end, obviously).
Both lines make sure that your rule is only tested when a USB device is ADDED or CHANGED.

You should be able to run all kinds of programs with the RUN parameter, but be aware that the environment for programs run by this is very limited. So you have to provide PATH and others in the script.


Posted: 08 Dec 2011, 16:22
by claes508
Josh wrote:That should not be too hard.

You can easily add your own udev rule file, like 90-my.rules or whatever, to the rules folder; it will never be overwritten.
In the file, check for the target ID, just like the usb_modeswitch rules do for the default IDs.

You can copy the line that says SYSTEM!="usb" ....
Don't use the line that checks the interface class.
Copy the line with the end marker (to the end, obviously).
Both lines make sure that your rule is only tested when a USB device is ADDED or CHANGED.

You should be able to run all kinds of programs with the RUN parameter, but be aware that the environment for programs run by this is very limited. So you have to provide PATH and others in the script.
Hi,

That sounds like a good idea. Same structure as your /lib/udev/rules.d/40-usb_modeswitch.rules

Code: Select all

ACTION!="add", GOTO="modeswitch_rules_end"
SUBSYSTEM!="usb", GOTO="modeswitch_rules_end"

LABEL="modeswitch_rules_begin"  ## not needed in this simple case...
# Huawei E173
ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1436", RUN+="myscript"
# ... more modems ...

LABEL="modeswitch_rules_end"

Posted: 08 Dec 2011, 21:49
by Josh
Mind that if your program is relying on the connectivity that the modem provides, you may want to add a delay ("sleep") to your script. It is likely that the setup will take a moment after the "new" device appeared.

But you probably knew that already ...




Posted: 09 Dec 2011, 17:15
by claes508
Yes, definitely :D

A Sleep 10 should suffice, and then run my modem checks and preparations; ATZ0, AT+CSQ, ATI and make sure PIN is correct.