2

I have a java program:

public class ProcessMain {
    public static final void main(String[] args) throws Exception {        

        Scanner keyboard = new Scanner(System.in);
        boolean exit = false;
            do
            {   if(keyboard.hasNext()){
                    String input = keyboard.next();
                    System.out.println(input);
                    if( "abort".equals(input)){
                    ABORT();
                    exit = true;
                    }
                }else{
                    System.out.println("Nothing");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }while (!exit);
        }

    private static void ABORT(){
        System.out.println("ABORT!!!!");
    }
}

In Linux, a script:

rm testfifo
mkfifo testfifo
cat > testfifo &
echo $!


java -cp "Test.jar" com.example.ProcessMain < testfifo

Terminal A runs the script, "Nothing" can be printed every 5 seconds. And then Terminal B execute echo "abort" >testfifo, but the program cannot display ABORT, it still displays Nothing every 5 seconds.

Please help!!

2
  • Do you want to read also other input from the fifo? If not what do you want to achieve? Maybe there is a better way to do. Commented May 10, 2016 at 8:27
  • @SubOptimal Here is what I want to achieve: The program should be a long running process. After a while, I have to input "abort" to stop the process. Thanks. Commented May 10, 2016 at 8:29

2 Answers 2

2

If you only need an external trigger to stop current processing. You might create a semaphore file and stop as soon it is created by another process.

See the following snippet.

// it will check for the file in the current directory
File semaphore = new File("abort.semaphore");
semaphore.deleteOnExit();
System.out.println("run until exist: " + semaphore);
while (!semaphore.exists()) {
    System.out.println("Nothing");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
System.out.println("ABORT!!!!");

As long the file abort.semaphore does not exist, the program will print to the console and wait five seconds.

edit On Linux you might use a signal handler and send an SIGABRT to the running process.

the following snippet uses an internal proprietary API

import sun.misc.Signal;
import sun.misc.SignalHandler;

public class SigAbrt {

  private static volatile boolean abort = false;

  public static void main(String[] args) throws Exception {

    Signal.handle(new Signal("ABRT"), new SignalHandler () {
      public void handle(Signal sig) {
        System.out.println("got a SIGABRT");
        abort = true;
      }
    });

    for(int i=0; i<100; i++) {
      Thread.sleep(1000);
      System.out.print('.');
      if (abort) {
          System.out.println("ABORT");
          break;
      }
    }
  }
}

run it

session one

java SigAbrt

session two

// first find the PID of SigAbrt
jps

example output of session two

2323  Jps
4242  SigAbrt

now send a SIGABRT to the SigAbrt process

kill -s SIGABRT 4242

example output of session one

...........got a SIGABRT
.ABORT
Sign up to request clarification or add additional context in comments.

2 Comments

This can solve my problem! Is there any disadvantage on monitoring a file instead of linux shell pipe?
@WilliamLAM Is there any disadvantage on monitoring a file depending on the frequency you check, the speed of the file system where the file is checked there might be a performance impact. Depending on the former and if you are able to, try to create it on an in-memory filesystem (e.g. a directory which is mount with tmpfs, this might show you some examples mount -t tmpfs). I add for Linux an other solution. Your process could also listen on a socket or ... There are other possibilities aswell. The easiest is the semaphore one. You need to make the decission.
0

Program not printing on console might be because of your testfifo file is empty.

Try this:

printf "Hello\nMy\nFriend\nabort" > testfifo

java -cp "Test.jar" com.example.ProcessMain < testfifo

It will work.

1 Comment

It works at the start of the program. But I would like to abort the program after it runs a while. Please help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.