I wanted to perform some actions in my Java program whenever a particular interface comes up (network interface ppp0 e.g.). However I figured out it was way too clumsy to keep polling the interface to verify its status.
After finding out if-up.local
file in Linux, now I can run a bash script whenever the interface comes up. Works fine, however considering my running Java program, how can I make this script interact with my Java code?
I am considering the following options:
- Write a listening socket and from this script send message to this Java socket.
- Invoke a C program, and through JNI run my java code.
I am looking for intuitive and less resource intensive approach.
Probably the simplest way is your first option. Just open a TCP socket and make a request to it from the if-up.local script you found. Using wget/curl for example.
A more complex alternative is to somehow use a netlink socket from java. I think that NETLINK_ROUTE will give you updates on interface creation events, see the first example in that link.
For this second option, either write some JNI or try to find a java library for using unix sockets. In a quick search I’ve found this one, although I’ve never used it, so I don’t know if it would work for your needs.
A third idea, similar to the first one, instead of a socket, use a FIFO pipe (mkfifo command), you could read it from java as a regular file, and write to it from the bash script with simply a *echo > fifo_file*.