I want to run script file on android Shell using Native C program.
I tried using system function but it's not working.
result = system("sh ./test.sh");
LOGD("result is %d", result);
system command returns 0 means its not executed script file successfully.
test.sh contains
echo "test...."
Android NDK application could not print test.... when this system call runs.
Even any script can not be started using system call. i checked more than 10 different scripts.
test.sh have 777 permissions
Any help would be appreciated.
where is 'sh'? and what is your '.' current directory when the application runs? try:
result = system("/system/bin/sh /full/path/to/test.sh");
Here i got the answer of this Question on Different post.
Run Shell Script file On Android Embedded Device using System function in Android NDK
thanks to all for Help
Related
I'm getting a permission denied error when my QPython android script tries to launch another qpython script.
I'm assuming that this can be done - right?
I've tried subprocess.call, whic seems like the right thing to do.
But, the script doesn't run, and the log shows the permission denied error.
The obvious thing to do is look at the permissions of the sub script, but I don't know how to do that on my Android phone.
Any comments/suggestion appreciated.
Thanks.
First, how to open a command prompt(bash)
Actually qpython = terminal emulator + Python code editor + Python interpreter.
So of course you can use the console! Why you can't enter bash is because every time you click the console icon in qpython it runs python. Just type something which will cause the interpreter error like "I want bash!" then run it, interpreter will break then go back to bash.
Second, how qpython run your script
It pass your python script to a shell script(qpython.sh), then run the your script by a python interpreter.
Third, about the permissions
Why permission denied? You can't run a python script directly because it's not marked as executable. But you can pass the script's path to python interpreter to run it.
Finally, so how to call a python script by a python script in qpython?
#-*-coding:utf8;-*-
#qpy:2
#qpy:console
import subprocess
print("I am calling myself!")
pysh="/data/data/org.qpython.qpy/files/bin/qpython-android5.sh"
#if U R using android 5
#else
pysh="/data/data/org.qpython.qpy/files/bin/qpython.sh"
callpy=__file__
#the script path you want to run
subprocess.call([pysh,callpy])
Hope this help you!
(I can't add image by myself because I don't have that much reputation, I need someone else to change my image link to image. So if you like my answer, please vote me. Thanks.)
Once you know where the script is, just move to that folder and then use the following command to list the files:
ls -l
the command outputs all the files together with permission (first column).
Now, to change the permission to "execution" you can use:
chmod 755 [your-script-name]
I've noticed that it is possible on Android to change the permissions on a file with chmod, which means we can easily execute anything from an application:
var runtime = Runtime.GetRuntime();
runtime.Exec("chmod 0755 /my/file").WaitFor();
// Then ProcessBuilder to execute it.
Would Google Play Store accept an application that takes advantage of this flaw? I can't find any documentation about it, but I confirm that it works.
Actually, I want to include ffmpeg for tasks that are too slow to be executed using MediaCodec.
(I've also noticed that the Android framework sometimes directly access to a native version of ffmpeg, so maybe I could access it directly from the phone?)
I don't know for sure if it is ok for Google Play.
However i don't think it is security issue. You will exec process with the autorisation of your app.
I hope the following example will help you.
You can try the following command line with your device connect.
adb shell
To have a shell on your devices
Then you can try to look what is inside the files for an app (replace com.your.package by the name of a debbugable apk)
ls /data/data/com.your.package
This command will failde because you have not the good permission.
Now run the following command.
run-as com.your.package
You will now exec your command line with the same permission as your app.
You can now retry the previous ls command. It will work. However it will not work for another package.
So, i think the command you will exec with your code, will be exec with the privilege of your app. So i don't think you can elevate the privilege of you app on a file with this method.
I have a C command line executable that I have successfully compiled for Android. I can copy the executable and launch it with Android Terminal Emulator on my Android ICS phone by doing the following:
execute "export TERMINFO=/etc/terminfo"
execute "mount -o remount rw /sdcard"
launch the executable from the sdcard
Step one is necessary because the command line application makes use of the ncurses library and if i do not set TERMINFO then I get an error when I try launch the application. If I leave off the second step then I get an "Access denied" when I try launch the command line application from the sdcard. So, provided I manually do these steps I can launch the command line executable.
Now what I am wanting to do is to wrap this command line executable inside a standard Android application. The source for Android Terminal Emulator is open source and so I can use that to open an EmulatorView inside my Android application. My question though is how do I go about including the native executable inside the apk, deploying it to the device in a location where my application will have rights to execute it in the EmulatorView. I am a bit concerned about whether I will be able to get over the rights issue so that the native command line executable can be launched.
I do know about the Android NDK, but would prefer not to have to re-write a working command line application so that it can be included as a library. I am specifically looking at keeping the C executable source as it is and executing it from the wrapper application. Does anyone know whether this is possible and if so how I would go about doing it?
If you copied the native file under your data folder where you should have appropriate permission, and then use Runtime.exec to execute your file.
You can create a bin folder (or any name) and copy your file under
Process exeProcess =
Runtime.getRuntime().exec("/data/data/YOUR_PACKAGE/bin/EXECUTABLE_FILE");
You can use the object exeProcess to read the data from your executable file.
i am trying to use an init.d script to execute another script in the background. The init.d script must continue immediatly after executing the other script in the background so my device can boot completely and then the executed script can do its commands. I have googled and found that the & symbol can be used at the end of the script name to execute it in the background. This worked, but my init.d script waited until the background script was finished. So i googled some more, and found that nohup command in use with & will do what i want. Perfect. HOWEVER when i used terminal emulator on android, i typed, nohup and i get "nohup: applet not found"
So as above i have no way of continuing the init.d script without it waiting for the background script. I am asking, Does anyone know how to run a script in the background while continuing the current script on android?
Or, does anybody know if i can add the "nohup" applet to my android device?
Thanks again to all who reply!
You can use daemonize to run your script:
Usage: daemonize [-f logfile] [-a] [-d delay] <program>
I would execute a command on my rooted Android 2.1 device
String path = "/data/data/com.android.providers.settings/databases/settings.db";
Runtime.getRuntime().exec("/system/bin/chmod -f 777 " + path);
But this command does nothing on the targeted file. Any idea?
The RootTools library offers simple methods to check for root and issue root commands:
RootTools.isRootAvailable()
List<String> output = RootTools.sendShell(command);
http://code.google.com/p/roottools/
You need to get the runtime as the root user first. There is a nice ShellInterface class that you can use from the MarketEnabler source available on Google Code. Though keep in mind this source code is released under the GPL.
Essentially what you need to do is determine where your su command is and create a kind of shell using an input stream and output stream for STDIN and STDOUT. With these you can then push your commands to your "terminal". When you are done all your commands, flush your buffer and then wait for the Runtime to complete. Once it is completed, you can then close your runtime interface.
Then take a look at the file you have tried creating/modifying/etc to see if everything worked properly.
Bao Le I believe you are trying to drop shell commands in an Android App, here (Running Android Native Code in your Android app)are few of many ways to run a command from an app.