简体   繁体   English

“如何使用 su -c 将单个命令作为后台进程运行

[英]"How to run a single command as a background process using su -c

Running by hand,用手奔跑,

adb shell
su
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 &

Performs as expected.按预期执行。

However, trying to run this from a bash script using the following command does not work as expected.但是,尝试使用以下命令从 bash 脚本运行它并不能按预期工作。

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 &'"

It works without &, but with & it just won't start the binary.它在没有 & 的情况下工作,但是有了 & 它就不会启动二进制文件。 exec app_process has to be a background process. exec app_process 必须是后台进程。

It's possible that the shell executes the binary but then just dies, because it does not wait on anything else, but I don't know how to fix it. shell 可能会执行二进制文件,然后就死了,因为它不等待其他任何东西,但我不知道如何修复它。 Making su a background process should do it, '&" but it didn't work.使 su 成为后台进程应该这样做, '&" 但它没有用。

When running without &, we get:在没有 & 的情况下运行时,我们得到:

D/su ( 1728): 0 /system/bin/sh executing 0 
D/su ( 1728): chmod 666 /dev/graphics/fb0
D/su ( 1728): && export CLASSPATH=/data/local/device.jar
D/su ( 1728): && export LD_LIBRARY_PATH=/data/local
D/su ( 1728): && exec app_process /system/bin com.device.client.Main /data/local/device.conf using shell /system/bin/sh : sh

When adding &, we get:添加 & 时,我们得到:

D/su ( 1746): 0 /system/bin/sh executing 0 
D/su ( 1746): chmod 666 /dev/graphics/fb0
D/su ( 1746): && export CLASSPATH=/data/local/device.jar
D/su ( 1746): && export LD_LIBRARY_PATH=/data/local
D/su ( 1746): && exec app_process /system/bin com.device.client.Main /data/local/device.conf & using shell /system/bin/sh : sh

but nothing gets loaded!但没有加载!

How can I run my set of commands, ensuring that app_process will run in the background?如何运行我的命令集,确保 app_process 将在后台运行?

I don't have adb (its an old debugger right) but a good syntax for passing a set of commands to the su command is:我没有adb (它是一个旧的调试器),但是将一组命令传递给su命令的好语法是:

su -c bash <<END_BASH
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 &
END_BASH

Since this is an old question, I'd like to just document what worked for me for a specific command, sleep 30 as suggested in a comment by shellter:由于这是一个老问题,我只想记录对特定命令有用的内容,如 shellter 在评论中建议的那样sleep 30

adb shell "su -c 'cd /data/local/tmp && nohup sleep 30 > /dev/null &'"

The cd /data/local/tmp part is necessary because nohup complains that / is not writeable. cd /data/local/tmp部分是必要的,因为 nohup 抱怨/不可写。 The redirection to dev/null is necessary to avoid bloating of the nohup file created by nohup (since you want to detach, you don't really care about stdout).重定向到dev/null是必要的,以避免由nohup创建的nohup文件膨胀(因为你想分离,你并不真正关心 stdout)。 I know that sleep doesn't output anything, but that is done with a command that does output to stdout in mind.我知道sleep不会输出任何内容,但是这是通过一个将输出到标准输出的命令来完成的。

After running this, if you do ps -ef | grep sleep运行后,如果你执行ps -ef | grep sleep ps -ef | grep sleep you'll see two processes running, one of them is sush -c cd /data/local/tmp && nohup sleep 30 > /dev/null & . ps -ef | grep sleep你会看到两个进程在运行,其中之一是sush -c cd /data/local/tmp && nohup sleep 30 > /dev/null & You can safely kill it through adb and only sleep 30 will remain.你可以通过 adb 安全地杀死它,只剩下sleep 30

You can replace sleep 30 with any other command.您可以用任何其他命令替换sleep 30 I guess you could run your bash script instead.我想您可以改为运行 bash 脚本。

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

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