2015-05-15 131 views
1

我有一个像这样的shell脚本。试图在shell脚本中运行hadoop MapReduce命令和linux命令

#!/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 

我在我的Java程序调用该脚本这样

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 is = 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); 
       } 
    } 
} 

我start-all.sh,那些获得了执行中存在的脚本越来越stop-all.sh和echo命令但执行其他命令是not.My输出就像

Output of running [] is:no jobtracker to stop 
localhost: no tasktracker to stop 
no namenode to stop 
localhost: no datanode to stop 
localhost: no secondarynamenode to stop 
RUNNING HADOOP PROGRAM 
starting namenode, logging to /home/hduser/Downloads/hadoop/libexec/../logs/hadoop-hduser-namenode-ubuntu.out 
localhost: starting datanode, logging to /home/hduser/Downloads/hadoop/libexec/../logs/hadoop-hduser-datanode-ubuntu.out 
localhost: starting secondarynamenode, logging to /home/hduser/Downloads/hadoop/libexec/../logs/hadoop-hduser-secondarynamenode-ubuntu.out 
starting jobtracker, logging to /home/hduser/Downloads/hadoop/libexec/../logs/hadoop-hduser-jobtracker-ubuntu.out 
localhost: starting tasktracker, logging to /home/hduser/Downloads/hadoop/libexec/../logs/hadoop-hduser-tasktracker-ubuntu.out 
stopping jobtracker 
localhost: stopping tasktracker 
no namenode to stop 

谁能帮我?我当我运行我的Java代码,我想所有的命令在shell脚本执行。 谢谢

+0

如果脚本失败,你可以找到根引起的读取/消费也System.err的使用process.getErrorStream()就像你已经为System.out做的那样。 – mbsau

+0

我试过了,但它没有打印任何东西.. @ mbsau – Codebeginner

+0

我没有看到给出的输出中有任何错误。从技术上讲,所有的命令都应该从脚本执行。脚本执行过程中是否有任何错误? – sras

回答

0

与下面的代码运行脚本并查看系统中的问题的细节进行

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 at executing command: " + concat(cmd)); 
      e.printStackTrace(System.out); 
     } 
     return EXEC_FAILED; 

    } 

    public static String concat(String[] array) { 
      StringBuffer buffer = new StringBuffer(); 
      for (int i = 0; i < 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); 
       } 
      } 

     } 

} 
+0

几个命令都显示此错误/home/hduser/Desktop/initial.sh:13:/home/hduser/Desktop/initial.sh:jps:找不到 /home/hduser/Desktop/initial.sh:14:/home/hduser/Desktop/initial.sh:hadoop:not found – Codebeginner

+0

从root用户运行java。据我所知,java代码没有执行命令的权限 – mfidan