简体   繁体   English

Bash脚本:使用su -c运行后台进程时出现奇怪的问题

[英]Bash scripting: weird issue when running a background process using su -c

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. 然后二进制文件可以很好地加载,但是它是前台进程,显然,在我杀死程序之前,adb shell不会返回。

Now here is the weird thing, if I want to app_process as a background process and add & at the end: 现在这很奇怪,如果我想将app_process作为后台进程并在末尾添加&:

&& 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. 该程序运行50秒钟,但此后,adb shell返回命令行,程序终止。 (my program has a while (true) loop waiting for a socket connection, so it will never terminate). (我的程序有一个while(true)循环等待套接字连接,因此它将永远不会终止)。

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. 在50秒钟的睡眠期间,如果我执行adb shell和ps,我会看到“ app_process”,但是50秒钟之后,它不再存在,并且命令行返回到在计算机上接收输入。

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. 看起来好像是作为后台进程运行,使“ su”返回adb的shell,shell退出,程序终止。 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. 对于此类问题,通常的答案是使用nohup(1)并重定向stdin,stdout和stderr,以避免使任何描述符对终端开放。 Try this: 尝试这个:

exec nohup app_process /system/bin com.device.client.Main\
/data/local/device.conf >/tmp/out 2>/tmp/err </dev/null &

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM