您尚未登录。

#1 2013-10-19 16:59:21

rongmu
会员
注册时间: 2013-10-19
帖子: 15

[已解决]udev 规则在 usb 连接 android 手机时启动 adb 服务,然而服务启动后很快就自动终止

最近刚从 Debian 转到 Arch 上来(GNOME3.10 都出了,debian testing 还停在 3.4 上……),遇到一个问题:

我在之前的系统中建立了下面的 udev 规则,好让手机连接到电脑时就启动 adb 服务,并把手机系统上的 ssh 端口绑定到本地的 222 端口上,以达到通过 usb ssh 进手机的目的。

#/etc/udev/rules.d/89-usb.rules
ACTION=="add", ATTRS{idVendor}=="22b8", ATTRS{idProduct}=="42d9", RUN+="/bin/sh -c '/usr/bin/adb start-server && /usr/bin/adb forward tcp:222 tcp:22'"

Debian 上要获取 adb 有个 android-tools-adb 包,Arch 官方源中没有这类东西,我就手工下了 android sdk,然后把上面规则中的 adb 路径改成现在的:/home/eric/opt/android-sdk-linux/platform-tools/adb。

但实际连上手机时发现 adb 并没有正确地运作起来,用 htop 观察 adb 进程,发现它总是在启动后数秒就突然结束了。之前在 Debian 上是没有问题的。

奇怪的是,如果手动在命令行执行 "sudo /home/eric/opt/android-sdk-linux/platform-tools/adb start-server && sudo /home/eric/opt/android-sdk-linux/platform-tools/adb forward tcp:222 tcp:22",adb 服务是可以正常工作的。

为什么通过 udev 规则启动的 adb 服务总是很快就突然终止?希望高手相救!

最近编辑记录 rongmu (2013-10-20 08:26:28)

离线

#2 2013-10-19 20:49:09

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,466
个人网站

Re: [已解决]udev 规则在 usb 连接 android 手机时启动 adb 服务,然而服务启动后很快就自动终止

AUR 和 archlinuxcn 里有 android-sdk-platform-tools 包。也有 sdk 和 ndk。
你可以使用 strace 来查看它启动后干了什么。可以在 htop 中按 s 键来调用 strace(记得先安装 strace)。

离线

#3 2013-10-20 08:25:58

rongmu
会员
注册时间: 2013-10-19
帖子: 15

Re: [已解决]udev 规则在 usb 连接 android 手机时启动 adb 服务,然而服务启动后很快就自动终止

搜索了很多,原来 udev 中的 RUN 脚本并不适合运行服务,manpage:

This can only be used for very short-running foreground tasks.
Running an event process for a long period of time may block all
further events for this or a dependent device.

Starting daemons or other long running processes is not appropriate
for udev; the forked processes, detached or not, will be
unconditionally killed after the event handling has finished.

最后参考 此帖 中的办法,通过 RUN 自定义的 systemd 服务解决了,我的做法如下:

建两个 systemd 服务

#/etc/systemd/system/adb.service
[Unit]
Description=adb
[Service]
Type=forking
ExecStart=/home/eric/opt/android-sdk-linux/platform-tools/adb start-server
#/etc/systemd/system/adb-ssh-forward.service
[Unit]
Description=adb forward local tcp:222 to remote tcp:22
Requires=adb.service
After=adb.service
[Service]
Type=oneshot
ExecStart=/home/eric/opt/android-sdk-linux/platform-tools/adb forward tcp:222 tcp:22

将原 udev 规则的 RUN 部分改作

RUN+="/usr/bin/systemctl --no-block start adb-ssh-forward.service"

最近编辑记录 rongmu (2013-12-28 22:42:56)

离线

页脚