A deal with executing a Unix command that produces endless output

Some unix command, such as tail -for starting a python web server (i.e. cherrypy), will produce infinite output, i.e. the only way to stop him is Ctrl-C. I am working on a scala application that executes this command, my implementation:

import scala.sys.process._
def exe(command: String): Unit = {
    command !
}

However, since the command creates an infinite output stream, the application hangs there until I finish it or kill the process launched by the command. I am also trying to add at the end of the command to run it in the background, but my application is still freezing.

Therefore, I am looking for another way to execute a command without freezing my application.

+4
source share
3
Seq("sh", "-c", "tail -f /var/log/syslog > /dev/null &") !

. , Randall , scala , "&". , scala, "sh", , . , scala / , Seq .

unix:

sh -c 'tail -f /var/log/syslog > /dev/null &'
+2

ProcessLogger , , .

val proc = 
  Process(command).run(ProcessLogger(line => (), err => println("Uh-oh: "+err)))

destroy.

proc.destroy

, ProcessLogger, destroy , , .

lines ( 2.10, lineStream 2.11) run , , , . Future, , , , - /.

+2

(), , SIGPIPE ().

, /dev/null:

command arg arg arg >/dev/null 2>&1

Addendum: This applies only to Unix-like systems, not Windows.

0
source

Source: https://habr.com/ru/post/1531789/


All Articles