Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash中Java程序的管道输入_Java_Linux_Pipe - Fatal编程技术网

Bash中Java程序的管道输入

Bash中Java程序的管道输入,java,linux,pipe,Java,Linux,Pipe,我有一个java程序: 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())

我有一个java程序:

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!!!!");
    }
}
在Linux中,一个脚本:

rm testfifo
mkfifo testfifo
cat > testfifo &
echo $!


java -cp "Test.jar" com.example.ProcessMain < testfifo
rmtestfifo
mkfifotestfifo
cat>testfifo&
回声$!
java-cp“Test.jar”com.example.ProcessMain
终端A运行脚本,每5秒可以打印一次“Nothing”。 然后终端B执行echo“abort”>testfifo,但程序不能显示abort,它仍然每5秒不显示任何内容


请帮忙

程序未在控制台上打印可能是因为您的
testfifo
文件为空

试试这个:

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

java -cp "Test.jar" com.example.ProcessMain < testfifo
printf“Hello\nMy\nFriend\nPort”>testfifo
java-cp“Test.jar”com.example.ProcessMain

如果您只需要一个外部触发器来停止当前的处理,它就会工作。

。您可以创建一个信号量文件,并在另一个进程创建该文件后立即停止

请参阅以下代码段

// 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!!!!");
只要文件
abort.semaphore
不存在,程序将打印到控制台并等待五秒钟

edit在Linux上,您可以使用信号处理程序,并向正在运行的进程发送
SIGABRT

以下代码段使用内部专有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;
      }
    }
  }
}
第二课时

// first find the PID of SigAbrt
jps
会话二的输出示例

2323  Jps
4242  SigAbrt
现在将
SIGABRT
发送到
SIGABRT
进程

kill -s SIGABRT 4242
会话1的输出示例

...........got a SIGABRT
.ABORT

它在程序开始时起作用。但我想在程序运行一段时间后中止它。请帮助。是否还要读取fifo的其他输入?如果不是,你想实现什么?也许有更好的方法。@我想实现的是:这个程序应该是一个长时间运行的过程。过了一会儿,我必须输入“abort”来停止进程。谢谢。这可以解决我的问题!监视文件而不是linux shell管道是否有任何缺点?@WilliamLAM
监视文件是否有任何缺点
根据您检查的频率、检查文件的文件系统的速度,可能会影响性能。根据前者,如果您能够,请尝试在内存文件系统上创建它(例如,使用
tmpfs
装载的目录,这可能会显示一些示例
mount-t tmpfs
)。我为Linux添加了另一个解决方案。您的进程也可以侦听套接字或。。。还有其他的可能性。最简单的是信号灯。你需要做出决定。
...........got a SIGABRT
.ABORT