java - Trying to run both hadoop MapReduce commands and linux commands in shell script -
i have shell script this.
#!/bin/sh /home/hduser/downloads/hadoop/bin/stop-all.sh echo "running hadoop program" cd /home/hduser/downloads/hadoop sudo rm -r /tmp/* sudo rm -r /app/* cd sudo mkdir -p /app/hadoop/tmp sudo chown hduser:hadoop /app/hadoop/tmp sudo chmod 750 /app/hadoop/tmp hadoop namenode -format /home/hduser/downloads/hadoop/bin/start-all.sh jps hadoop dfs -mkdir -p ~/downloads/hadoop/input hadoop dfs -copyfromlocal /home/hduser/desktop/iris.arff ~/downloads/hadoop/input hadoop jar ~/desktop/try.jar 2 weka.classifiers.trees.j48 ~/downloads/hadoop/input ~/downloads/hadoop/output /home/hduser/downloads/hadoop/bin/stop-all.sh
i invoking script in java program this
public class uiinput { public static void main(string[] args) throws ioexception { // runtime.getruntime().exec("/home/hduser/desktop/initial.sh"); new processbuilder("/home/hduser/desktop/initial.sh"); processbuilder pb = new processbuilder("/home/hduser/desktop/initial.sh"); process process=pb.start(); inputstream = process.getinputstream(); inputstreamreader isr = new inputstreamreader(is); bufferedreader br = new bufferedreader(isr); string line; system.out.printf("output of running %s is:", arrays.tostring(args)); while ((line = br.readline()) != null) { system.out.println(line); } } }
my start-all.sh,stop-all.sh , echo commands getting executed present in script getting executed other commands not.my output like
output of running [] is:no jobtracker stop localhost: no tasktracker stop no namenode stop localhost: no datanode stop localhost: no secondarynamenode stop running hadoop program starting namenode, logging /home/hduser/downloads/hadoop/libexec/../logs/hadoop-hduser-namenode-ubuntu.out localhost: starting datanode, logging /home/hduser/downloads/hadoop/libexec/../logs/hadoop-hduser-datanode-ubuntu.out localhost: starting secondarynamenode, logging /home/hduser/downloads/hadoop/libexec/../logs/hadoop-hduser-secondarynamenode-ubuntu.out starting jobtracker, logging /home/hduser/downloads/hadoop/libexec/../logs/hadoop-hduser-jobtracker-ubuntu.out localhost: starting tasktracker, logging /home/hduser/downloads/hadoop/libexec/../logs/hadoop-hduser-tasktracker-ubuntu.out stopping jobtracker localhost: stopping tasktracker no namenode stop
can me?i when run java code want of commands executed in shell script. thank you
run script below code , see problem details in system out
package test; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; public class commandlineexecutor { public final int exec_ok = 0; public final int exec_failed = 1; public static void main(string[] args) { commandlineexecutor cmd = new commandlineexecutor(); string[] script = new string[]{ "/home/hduser/desktop/initial.sh"}; boolean jointoprocess = true; //main threads waits process finished. int result = cmd.execute(script, jointoprocess); system.out.println( result == 0 ? "script succesfully run" : "script failed" ); } public int execute(string[] cmd, boolean jointoprocess) { runtime runtime = runtime.getruntime(); process proc = null; try { system.out.println("executing cmd: "+concat(cmd)); proc = runtime.exec(cmd); streamprocessor errorstreamprocessor = new streamprocessor(proc.geterrorstream()); streamprocessor outputstreamprocessor = new streamprocessor(proc.getinputstream()); errorstreamprocessor.start(); outputstreamprocessor.start(); } catch (exception e) { e.printstacktrace(system.out); return exec_failed; } try { int result = exec_ok; if(jointoprocess) result = proc.waitfor(); return result; } catch (interruptedexception e) { system.out.println("error @ executing command: " + concat(cmd) ); e.printstacktrace(system.out); } return exec_failed; } public static string concat(string[] array) { stringbuffer buffer = new stringbuffer(); (int = 0; < array.length; i++) { if (i > 0) buffer.append(' '); buffer.append(array[i]); } return buffer.tostring(); } class streamprocessor extends thread { private inputstream inputstream; public streamprocessor(inputstream is) { this.inputstream = is; } public void run() { try { inputstreamreader isr = new inputstreamreader(inputstream); bufferedreader br = new bufferedreader(isr); while (true) { string s = br.readline(); if (s == null) break; system.out.println(s); } } catch (ioexception e) { e.printstacktrace(system.out); } } } }
Comments
Post a Comment