从 linux 在 Android 上启动持久后台进程

Launching a persistent background process on Android from linux

假设我有一个简单的可执行 linux 程序,它 运行 无限期地循环直到它被明确杀死。我希望能够以这样的方式部署它,即使在我从我的 Android 设备上断开 USB 电缆后它仍能继续 运行。当我像这样尝试 运行ning 程序时,

$adb shell
<android_shell>$ /path/to/dir/myprog &

我可以断开电源线,当我重新连接电源线并执行

$ps | grep myprog

还能看到运行宁。

但是,当我尝试 运行以这种方式设置时,

$adb shell /path/to/dir/myprog &

当我断开电源线的那一刻,进程就被终止了,我再也找不到它了ps

1) 这两种执行命令的方式有什么区别?

2) 有没有一种方法可以让我从桌面终端 运行 命令来实现我想要做的事情?

android_shell>$ /path/to/dir/myprog &

此进程 运行 在您设备的后台。 ps 设备内部。

$adb shell /path/to/dir/myprog &

此进程 运行 在您的开发 PC 后台,显然 adb 进程与 adbd 守护程序的套接字连接会因移除电缆而被终止。[编辑]

解决方法:- 使用 nohup.

adb shell "nohup /path/to/dir/myprog &"

[编辑]

正如 LieRyan 所说 " 很重要。

nohup-

发送给 shell 的挂断信号可以用 & 终止您的进程后台。 nohup 捕获 SIGHUP 挂起并忽略,因此它永远不会到达应用程序。 在 adb shell 的情况下,& 会工作,但我遇到了 & 的问题并且进程由于某种未知原因而被终止。但是无法挖掘原因(导致 adb shell kill 的原因)time.With nohup 我从来没有遇到任何问题。