从Linux在Android上启动持久性后台进程 [英] Launching a persistent background process on Android from linux
问题描述
说我有一个简单的可执行linux程序,该程序在循环中无限期运行,直到被明确杀死为止.我希望能够以这样一种方式部署它,即使我从我的Android设备断开USB电缆连接后,它仍可以继续运行.当我尝试运行这样的程序时,
Say I have a simple executable linux program that runs indefinitely in a loop till it is explicitly killed. I want to be able to deploy it in such a way that it continues to run even after I disconnect the USB cable from my Android device. When I try running the program like this,
$adb shell
<android_shell>$ /path/to/dir/myprog &
我能够断开电源线,将其重新连接后再做
I am able to disconnect the cord and when I connect it back and do a
$ps | grep myprog
我仍然可以看到它正在运行.
I can still see it running.
但是,当我尝试以此方式运行时,
However, when I try running it this way,
$adb shell /path/to/dir/myprog &
当我断开电源线时,该过程被终止,并且我再也无法通过ps
找到它.
the moment I disconnect my cord, the process is killed and I am not able to find it anymore with ps
.
1)这两种执行命令的方式有什么区别?
1) What is the difference in these two ways of executing the command?
2)有什么方法可以从台式机终端运行命令,从而实现我想做的事情?
2) Is there a way I can run commands from the desktop terminal in a way to achieve what I'm trying to do?
推荐答案
android_shell> $/path/to/dir/myprog&
android_shell>$ /path/to/dir/myprog &
此过程正在设备的后台运行. ps在设备内部.
this process is running in your device's background. ps inside device.
$ adb shell/path/to/dir/myprog&
$adb shell /path/to/dir/myprog &
此进程正在开发PC的后台运行,很明显,adb进程与adbd守护程序的套接字连接通过断开电缆而被杀死.
This process is running in your development PC's background, obviously the adb process's socket connection to the adbd daemon gets killed by removing cable.
解决方案:-
使用nohup
.
adb shell "nohup /path/to/dir/myprog &"
正如LieRyan所说的"
很重要.
As LieRyan mentioned "
is important.
nohup
-
到shell的挂断信号可以使用&
杀死您的进程背景. nohup
捕获SIGHUP挂断并忽略,因此它永远不会到达应用程序.
在adb shell情况下,&
可以工作,但是我遇到了&
的问题,由于某种未知原因,进程被杀死了.但是当时无法弄清楚原因(导致adb shell被杀死的原因).使用nohup
,我从来没有遇到任何问题.
A hangup signal to shell can kill your process background with &
. nohup
catches the SIGHUP hangup and ignores, so that it never reaches the application.
In adb shell case, &
will work,But I had faced issue with &
and process get killed due to some unknown reason. But could not dig the reason(what caused adb shell kill) at that time.With nohup
I never faced any problem.
这篇关于从Linux在Android上启动持久性后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!