Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a POSIX signal from the JVM

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?

like image 760
Jan Rychter Avatar asked Jun 30 '10 09:06

Jan Rychter


People also ask

What is a Posix signal?

SIGABRT. The SIGABRT signal is sent to a process to tell it to abort, i.e. to terminate. The signal is usually initiated by the process itself when it calls abort function of the C Standard Library, but it can be sent to the process from outside like any other signal.


1 Answers

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).

like image 174
Jan Rychter Avatar answered Oct 21 '22 08:10

Jan Rychter