My android app launches a script using Runtime.exec(). The script in turn runs a command that starts a process. I want to capture the pid of that process, so I can keep track of its status and kill it later.
Here's an excerpt from my script:
$exec $options &
echo pid:$!:
I expect the pid to be echoed right away, but it's not. The command executes as expected, but the pid is not echoed until I manually kill the process that was started by the command. So it seems to be not respecting the request to run the command in the background. Am I doing it wrong, or does Android just not support this?
In case it matters, the command ($exec) that I'm running is a call to a binary executable, and $options expands to the arguments for that executable.
I was doing it wrong. I had
which is fine for the launching of the shell script itself, since it's not a shell command. But in order for the
&in the shell script to be interpreted, it must be run...well... in a shell.So I needed to invoke the shell script the same way I invoke shell commands directly. Like this:
With this code and the script below, the binary file runs in the background and the pid is echoed to a file specified as a script arg.