Sending a POSIX signal from the JVM
Asked Answered
S

2

17

How do I send a POSIX signal from within the JVM? (in Java or Clojure)

I never thought this would be an issue until I tried googling it — there is lots of information about handling signals, but nothing about sending them.

Short of using the JNI or calling the shell to execute "kill", is there any other way to send a signal to a PID?

Searcy answered 30/6, 2010 at 9:2 Comment(2)
see also #7835712Corfam
In particular this comment: https://mcmap.net/q/502780/-how-to-send-sigint-signal-from-java-to-an-external-processMilstone
S
10

Ok. Answering myself: I looked at the suggested libraries, but I am wary of introducing new dependencies on lots of code, especially if I'll only use a small part of it.

It turns out the easiest way is to use JNA and interface with the system (libc) API. In clojure this pretty much amounts to:

(jna-invoke Integer kill pid signo)

after doing a (:use net.n01se.clojure-jna) of course.

Since this software is not intended to ever run on Windows, I'm fine with this solution which should work on all POSIX systems (MacOS and Linux are what I'm interested in).

Searcy answered 30/6, 2010 at 10:53 Comment(0)
B
5

POSIX signals is OS specific functions, but JVM is tried to be OS independent VM. So there are no standard functions for this.

But you can execute shell commands (using Runtime class) or use some lbrary for your needs, like a Posix for Java

Badoglio answered 30/6, 2010 at 9:56 Comment(2)
+1 Posix for Java looks like the cleanest option (if you can deploy JNI libraries). Otherwise, exec'ing kill seems acceptable.Klan
Hmmm ... looked at the Posix for Java docs, and some of those classes have the potential to destabilize your JVM if used carelessly.Tremendous

© 2022 - 2024 — McMap. All rights reserved.