Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop child process when parent process stops

In my android application, I create a child process with Runtime.getRuntime().exec().

But may be a situation, where a user kills my application, but the child process is still running. What I want is, when I kill the application, also kill the child process. How I can do it?

like image 359
Yura Shinkarev Avatar asked Feb 21 '26 01:02

Yura Shinkarev


1 Answers

I would

  • Keep track of the processes created by my application.
  • register a shutdown hook with the JVM through Runtime.getRuntime().addShutdownHook() that would be called when the application is shutdown and kill all the processes spawned, that were tracked in previous step.
like image 111
Vikdor Avatar answered Feb 23 '26 03:02

Vikdor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!