简体   繁体   中英

"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.

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.

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. Making su a background process should do it, '&" but it didn't work.

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?

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:

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:

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. 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). I know that sleep doesn't output anything, but that is done with a command that does output to stdout in mind.

After running this, if you do 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 & . You can safely kill it through adb and only sleep 30 will remain.

You can replace sleep 30 with any other command. I guess you could run your bash script instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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