Run Script Shell File On Embedded Android Device Using System Function In Android NDK


Here I want to run a .sh file via a system call in android NDK.

I can run the command cp,rm

through a system call. but the sh command does not work through a system call.

I also installed busy-box on android

. I am using below code. I set all permissions to .


#include <stdlib.h>
#include <stdio.h>
#include <android/log.h>
#include <jni.h>

#define LOG_TAG "Demo"

#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

void Java_com_ndkdemo_NDKdemoActivity_systemcall(JNIEnv * env, jobject obj) {

    int result;

    result = system("cp /mnt/sdcard/test /Download");

    LOGD("result is cp %d", result);

    result = system("sh ./");

    LOGD("result of is %d", result);




 result is cp 0.
 result of is 0.


Here I am getting 0 in system("sh ./");

but not starting the


cannot get output "Hi"

to console. contains

echo "Hi"


If I execute a direct command at the prompt and not its working tone and its output "Hi"

to the console.

Please help me understand this issue.



source to share

2 answers

Where do you expect to see the "echo"? By default, android sends stdout to / dev / null.

According to the documentation, you can redirect stdout and stderr to the log (so that it appears in logcat) using the following commands:

$ adb shell stop
$ adb shell setprop log.redirect-stdio true
$ adb shell start


You can now see your echo in adb logcat




You might want to try sh -c /full/path/to/



All Articles