Created
July 15, 2016 02:09
-
-
Save kitmenke/a4d965f4a8d7d34e0f03105367996b9a to your computer and use it in GitHub Desktop.
Groovy script which uses log4j, runs the list command, and gets the output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Run a groovy script | |
# argument should be path to .groovy file | |
HIVEDIR="/usr/hdp/current/hive-client/lib" | |
# Include log4j properties in the classpath so we can do logging | |
JAVACP="log4j.properties" | |
# Need groovy-all jar to run groovy scripts | |
JAVACP="$JAVACP:$HIVEDIR/groovy-all-2.1.6.jar" | |
JAVACP="$JAVACP:$HIVEDIR/log4j-1.2.16.jar" | |
java -cp $JAVACP groovy.ui.GroovyMain $1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
log4j.rootLogger=DEBUG, CONSOLE, ROLLINGFILE | |
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender | |
#log4j.appender.CONSOLE.Threshold=INFO | |
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout | |
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n | |
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender | |
#log4j.appender.ROLLINGFILE.Threshold=DEBUG | |
log4j.appender.ROLLINGFILE.File=script.log | |
log4j.appender.ROLLINGFILE.MaxFileSize=10MB | |
log4j.appender.ROLLINGFILE.MaxBackupIndex=10 | |
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout | |
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.log4j.* | |
import groovy.util.logging.* | |
@Log4j | |
class Main { | |
def execute() { | |
// configure log4j | |
PropertyConfigurator.configure("log4j.properties") | |
//log.debug 'This is a debug message.' | |
log.info 'Running script..' | |
try { | |
def stdout = new StringBuilder() | |
def stderr = new StringBuilder() | |
def proc = 'ls'.execute() | |
proc.consumeProcessOutput(stdout, stderr) | |
// wait for 10 seconds or kill | |
proc.waitForOrKill 10000 | |
log.info "Standard out is ${stdout}" | |
log.error "Standard error is ${stderr}" | |
log.info "Return code is ${proc.exitValue()}" | |
} catch (Exception e) { | |
log.error 'Error running script', e | |
} | |
} | |
} | |
def main = new Main() | |
main.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment