我有一个程序应在后台运行。我正在尝试使用adb触发它并获得以下行为:
adb shell "app &"
adb shell ps | grep myapp
显示该应用未运行。
adb shell
$app &
$exit
终端无响应的结果。在终止adb进程后,终端将被释放,然后在我通过以下方式进行检查时:
adb shell ps | grep myapp
我看到该应用程序在后台运行。
有人可以解释这种行为吗?如何从命令行运行该应用程序,并使其通过cli在后台运行?
Android Debug Bridge version 1.0.32
Revision 9e28ac08b3ed-android
您的应用程序是与ADB连接时生成的Shell的子级。当您退出外壳程序时,您的应用程序将被终止,因为外壳程序将被终止。您应该将应用程序与外壳分离:
使用nohup:
adb shell "nohup app &"
使用守护程序(在某些Android系统上可用):
adb shell daemonize app
adb shell toybox daemonize app
并且,如果您(如我)在使用nohup挂起adb shell命令时遇到麻烦,并且如果daemonize不可用,则可以使用C自己编写它,如下所示:
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>
int main(int argc, char ** argv)
{
int pid = fork();
if (pid > 0) {
printf("Father dies\n");
return 0;
}
/* redirect_fds(): redirect stdin, stdout, and stderr to /dev/NULL */
(void) close(0);
(void) close(1);
(void) close(2);
(void) dup(0);
(void) dup(0);
while (1)
{
printf("Child runs silently\n");
sleep(1);
/* Do long stuff in backgroudn here */
}
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句