I have a very strange problem, in my terminal, if I load my binary this way:
adb shell "su -c '
chmod 666 /dev/graphics/fb0
&& export CLASSPATH=/data/local/device.jar
&& export LD_LIBRARY_PATH=/data/local
&& exec app_process /system/bin com.device.client.Main /data/local/device.conf'"
Then the binary loads fine but it is foreground process, and adb shell doesn’t return until I kill the program, obviously.
Now here is the weird thing, if I want to app_process as a background process and add & at the end:
&& exec app_process /system/bin com.device.client.Main /data/local/device.conf &'"
It looks like it terminates immediately. I tried adding a sleep:
adb shell "su -c '
chmod 666 /dev/graphics/fb0
&& export CLASSPATH=/data/local/device.jar
&& export LD_LIBRARY_PATH=/data/local
&& exec app_process /system/bin com.device.client.Main /data/local/device.conf & && sleep 50'"
The program runs for 50 seconds, but after that, adb shell returns to the command line and the program is terminated. (my program has a while (true) loop waiting for a socket connection, so it will never terminate).
No error is getting generated or anything. During the 50 seconds of sleep, if I do adb shell and ps, I see “app_process”, but after 50, it is not there anymore, and my command line is back to receiving input on my computer.
I am really not sure what is going on. It looks like running as a background process makes “su” return to adb’s shell, the shell quits, and the program terminates. If this is the case, how could I please fix it?
Thank you very much.
The usual answer for problems like this is to use
nohup(1)and redirect stdin, stdout, and stderr to avoid leaving any descriptors open to the terminal. Try this: