Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a signal to a Process from Java 9

I hear in java 9 they did a lot of work updating the Process API. Is there now a cleaner way to send a signal to a Process spawned by Java? The signals are used as a simple way to trigger actions in the process.

Before I had to use reflection to get a pid and then use Runtime#exec to send the kill command. I assume you still need to use Runtime#exec because signal may be OS dependent, but I'm curious if anyone knows a better way than this!

Process p = Runtime.getRuntime().exec(...);
...
Runtime.getRuntime().exec("kill -SIGUSR1 " + p.getPid());
like image 930
flakes Avatar asked Oct 18 '22 16:10

flakes


1 Answers

signal may be OS dependant

Yes, windows does not have signals in the same sense posix OSes do.

but I'm curious if anyone knows a better way than this!

jnr-posix provides thin wrappers around libc, including kill.

like image 153
the8472 Avatar answered Oct 21 '22 05:10

the8472