Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launching a persistent background process on Android from linux

Tags:

linux

android

adb

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?

like image 758
shaveenk Avatar asked Apr 05 '16 23:04

shaveenk


1 Answers

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.

like image 61
Rilwan Avatar answered Oct 17 '22 01:10

Rilwan