[ 
https://issues.apache.org/jira/browse/FLUME-1661?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13482664#comment-13482664
 ] 

Roshan Naik edited comment on FLUME-1661 at 10/23/12 8:46 PM:
--------------------------------------------------------------

Brock the situation is not as tricky. Try the below code. Here test() works for 
all commands (if 'shell' config is specified). test2() works for simple 
commands (i.e current use cases).  The assumption is that the "/bin/sh -c" 
comes from the 'shell' config.

{code}
    // invoke complex commands through shell  - when the 'shell' config is 
specified
    static void test(String cmd) throws Exception { 
        Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", 
"-c", cmd });

        BufferedReader out = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        Thread.sleep(10L);
        System.out.println("cmd = " + cmd);
        System.out.println("out = '" + readAll(out) + "'");
    }

    // invoke simple commands directly  - when the 'shell' config is NOT 
specified
    static void test2(String cmd) throws Exception {
        Process process = Runtime.getRuntime().exec(cmd);  //we can also use 
ProcesBuilder

        BufferedReader out = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        Thread.sleep(10L);
        System.out.println("out2 = '" + readAll(out) + "'");
        System.out.println();
    }

    static String readAll(BufferedReader reader) throws IOException {
        String result = "";
        String line;
        while((line = reader.readLine()) != null) {
            result += line + "\n";
        }
        return result;
    }
{code}
                
      was (Author: roshan_naik):
    Brock the situation is not as tricky. Try the below code. Here test() works 
for all commands if 'shell' config is specified). test2() works for simple 
commands (i.e current use cases).  The assumption is that the "/bin/sh -c" 
comes from the 'shell' config.

{code}
    // invoke complex commands through shell  - when the 'shell' config is 
specified
    static void test(String cmd) throws Exception { 
        Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", 
"-c", cmd });

        BufferedReader out = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        Thread.sleep(10L);
        System.out.println("cmd = " + cmd);
        System.out.println("out = '" + readAll(out) + "'");
    }

    // invoke simple commands directly  - when the 'shell' config is NOT 
specified
    static void test2(String cmd) throws Exception {
        Process process = Runtime.getRuntime().exec(cmd);  //we can also use 
ProcesBuilder

        BufferedReader out = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        Thread.sleep(10L);
        System.out.println("out2 = '" + readAll(out) + "'");
        System.out.println();
    }

    static String readAll(BufferedReader reader) throws IOException {
        String result = "";
        String line;
        while((line = reader.readLine()) != null) {
            result += line + "\n";
        }
        return result;
    }
{code}
                  
> ExecSource cannot execute (little complicated..) *nix commands
> --------------------------------------------------------------
>
>                 Key: FLUME-1661
>                 URL: https://issues.apache.org/jira/browse/FLUME-1661
>             Project: Flume
>          Issue Type: Improvement
>          Components: Sinks+Sources
>    Affects Versions: v1.2.0
>            Reporter: Yoonseok Woo
>         Attachments: FLUME-1661-1.patch
>
>
> * command line parsing
> ** conf/flume.conf
> {code}
> agent.sources.source1.type = exec
> agent.sources.source1.command = tail -f 
> /some/path/logs/exception/error.log.`date +%Y%m%d%H`
> {code}
> ** result
> {code}
> tail: /some/path/logs/exception/error.log.`date: No such file or directory
> tail: +%Y%m%d%H`: No such file or directory
> {code}
> ** needs to be improved
> {code}
> (ExecSouce.java:242) String[] commandArgs = command.split("\\s+") 
> {code}
> * using special character (e.g. *, `, ', ...)
> ** conf/flume.conf
> {code}
> agent.sources.source1.type = exec
> agent.sources.source1.command = tail -f /some/path/logs/exception/error.log.*
> {code}
> ** result
> {code}
> tail: /some/path/logs/exception/error.log.*: No such file or directory
> {code}
> ** needs to be improved
> {code}
> (ExecSouce.java:243) process = new ProcessBuilder(commandArgs).start();
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to