I have a shell script that looks does the following:

# /mnt/sdcard/touch.sh
sleep 10

while [ 1 ]; do
    input swipe 700 380 1020 600 1000
    input tap 960 540
    input swipe 1220 700 900 480 1000
    input tap 960 540
    input swipe 1220 700 900 480 1000
    input tap 960 540
    input swipe 700 380 1020 600 1000
    input tap 960 540

If I execute it from Terminal IDE (a terminal emulator), it only works while Terminal IDE is in the foreground, so I thought I'd make an actual app to execute it and keep it running in the background. Here's what I've got now:

package com.mycompany.myapp;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        try {
            Runtime.getRuntime().exec(new String[]{"/system/bin/sh", "/mnt/sdcard/touch.sh", });
        catch (IOException e) {/*...*/}

But it doesn't do anything.

So how would I execute this, then leave it running in the background so that the taps and swipes happen while another app is in the foreground? You get the idea. :D

  • This question shouldn't be closed as offtopic. He just tried to solve it via writing app, but his question isn't about it at all. – janot Aug 12 '14 at 6:44

It worked by just doing sh /mnt/sdcard/touch.sh & after having started a shell with adb shell. Then, when I closed the terminal forcibly while the process was in the background, the process remained running in the background of my phone as expected. I was able to kill the process later by getting with adb shell the using ps to find the process ID and kill -s KILL <ID> to kill it.

Basically, turns out I didn't have to make an Android app just to make my script run in the background. Clash on...

  • I'm curious what various versions of Android behavior is. I wrote a script to log battery levels that runs in an endless loop - and I find if I start forked like you did & - and pull the USB cable... that it in fact halts... but resumes if I return USB cable. – RoundSparrow hilltx Dec 6 '14 at 17:45
  • had the same needs, answer helped me greatly! but... oddly enough, only within adb shell I could get to do it with no root. when running the exact same script within an android terminal app, I could only achieve the same effect (to actually get some touching working) after activating root with su! any clues on why this could be? – cregox Apr 21 '17 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.