Skip to content

Instantly share code, notes, and snippets.

@hferentschik
Last active June 17, 2016 11:45
Show Gist options
  • Select an option

  • Save hferentschik/f94a13a81f2f6e1be3e48d9c59e302c3 to your computer and use it in GitHub Desktop.

Select an option

Save hferentschik/f94a13a81f2f6e1be3e48d9c59e302c3 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.Map;
class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader( is );
BufferedReader br = new BufferedReader( isr );
String line = null;
while ( !Thread.currentThread().isInterrupted() && ( line = br.readLine() ) != null ) {
System.out.println( type + ">" + line );
}
}
catch ( IOException ioe ) {
ioe.printStackTrace();
}
System.out.println( "output thread ending" );
}
}
public class ProcessLauncher {
public static void main(String args[]) throws Exception {
try {
ProcessBuilder pb = new ProcessBuilder( "ruby", "spawner.rb" );
pb.redirectErrorStream( true );
Process proc = pb.start();
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
outputGobbler.start();
int exitVal = proc.waitFor();
System.out.println( "process returned:" + exitVal );
outputGobbler.join();
proc.destroy();
System.out.println( "done" );
}
catch ( Throwable t ) {
t.printStackTrace();
}
}
}
$stdout.sync = true
$stderr.sync = true
i = 0
while i < 10000
$stdout.puts "Iteration #{i}"
sleep 1
i = i + 1
end
$stdout.puts "Bye from #{Process.pid}"
require "tempfile"
require 'rbconfig'
class SpawnTest
def self.spawn_process
if os == :windows
p1 = spawn('ruby', 'loop.rb', [:out, :err] => ['process.log', "w"], :new_pgroup => true)
else
p1 = spawn('ruby', 'loop.rb', [:out, :err] => ['process.log', "w"], :pgroup => true)
end
# Detach from the processes so they will keep running
puts p1
Process.detach(p1)
end
def self.os
@os ||= (
host_os = RbConfig::CONFIG['host_os']
case host_os
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
:linux
when /solaris|bsd/
:unix
else
raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
end
)
end
end
if __FILE__ == $0
SpawnTest.spawn_process
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment