Launching a persistent background process on Android from linux
Asked Answered
S

2

6

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 &

the moment I disconnect my cord, the process is killed and I am not able to find it anymore with ps.

1) What is the difference in these two ways of executing the command?

2) Is there a way I can run commands from the desktop terminal in a way to achieve what I'm trying to do?

Sapindaceous answered 5/4, 2016 at 23:0 Comment(0)
A
5

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

this process is running in your device's background. ps inside device.

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

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.[EDIT]

Solution:- use nohup.

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

[EDIT]

As LieRyan mentioned " is important.

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.

Arezzo answered 6/4, 2016 at 3:30 Comment(5)
The important bit here is actually to quote the &. Without the quote, the & is processed by the PC's shell as meaning to background the local adb process, rather than being passed to Android's shell.Gitagitel
@Rilwan. You did make a compelling argument. However, this is not true. when you execute $adb shell /path/to/dir/myprog &, you are actually invoking the shell and the shell launches the process so the parent will in fact still be the shell. The correct answer is what Lie Ryan mentioned. Without quotes, the terminal was considering my command to be a background process instead of the arguments in my command which is my actual intention. Adding quotes solved the problem. So $adb shell "/path/to/dir/myprog &" totally fixed my issue. nohup was not required in this case.Sapindaceous
@LieRyan if you can put your comment up as an answer, I will accept it.Sapindaceous
@shaveen I editted answer, sorry for the edit at this time .Even thoug i put " in answer, Actually i forgot to mention about it. @LieRyan I totally agree with you.Arezzo
FWIW, it still work on Google's and Samsung's devices (as of Android 11 beta), however it fails on LG V30, Huawei P40 Lite E, Fire Tablet and probably many many others. The solution hangs on those devices and process is still terminated once the device is unplugged.Modestia
E
0
adb push myprog /data/local/tmp/myprog
adb shell chmod a+x /data/local/tmp/myprog
adb shell "/data/local/tmp/myprog >/dev/null 2>&1 &"

push your myprog to /data/local/tmp/, other folder might not work

>/dev/null redirect stdout to null device, 2>&1 redirect stderr to fd 1(stdout). needed if myprog print something

&at the end, run in background

Escribe answered 21/7, 2024 at 19:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.