echo map reduce output in shell script using oozie - linux

The .sh script has the following content inside it
echo hbase org.apache.hadoop.hbase.mapreduce.RowCounter TABLE_NAME
The above script will be called by oozie which will capture whatever output the above command emits. The problem is, the above command is executed and no output gets echoed since all the output is happening in the background. How to capture those background output and echo it using shell script?

First thing first, you need to have <capture-output/> in the Oozie Shell action.
Also to capture the output in Oozie shell action, the script need to be something like the following:
set var=`hbase org.apache.hadoop.hbase.mapreduce.RowCounter TABLE_NAME`
echo "capture_var=$var"
After you do this, the variable will be available to be passed/used within Oozie using:
${wf:actionData('shellscriptoozieactionname')['capture_var']}

Related

How does a subshell's executed lines get printed to the main shell without running the source command?

Let's say I have an executable shell script called foo.sh. Inside it is a simple echo "Hello World". From my understanding, when I run this via ./foo.sh, a subshell is invoked which executes the echo "Hello World" line.
Why, then, do I see the output of the echo command in my main shell/terminal? I would think you'd have to do a "source ./foo.sh" instead of the simple "./foo.sh" to see the output in your current shell.
Can any of you help clarify?
The standard output is inherited. Quoting from Bash Reference Manual:
Command Execution Environment
When a simple command other than a builtin or shell function is to be
executed, it is invoked in a separate execution environment that
consists of the following. Unless otherwise noted, the values are
inherited from the shell.
the shell’s open files, plus any modifications and additions specified by redirections to the command
...

Supervise the output of a program using script

How can i write a shell script that can supervise the input and the output of a running script?
I have another script that will generate results. I want something like trigger to run commands when the other script will generate results.

Difference between different ways of running shell script

Recently I have been asked a question. What are the different ways of executing shell script and what is the difference between each methods ?
I said we can run shell script in the following methods assuming test.sh is the script name,
sh test.sh
./test.sh
. ./test.sh
I don't know the difference between 1 & 2. But usually in first 2 methods, upon executing, it will spawn new process and run the same. Whereas in the last method, it won't spawn new process. Instead it runs in the same one.
Can someone throw more insight on this and correct me if I am wrong?
sh test.sh
Tells the command to use sh to execute test.sh.
./test.sh
Tells the command to execute the script. The interpreter needs to be defined in the first line with something like #!/bin/sh or #!/bin/bash. Note (thanks keltar) that in this case the file test.sh needs to have execution rights for the user performing this command. Otherwise it will not be executed.
In both cases, all variables used will expire after the script is executed.
. ./test.sh
Sources the code. That is, it executes it and whatever executed, variables defined, etc, will persist in the session.
For further information, you can check What is the difference between executing a bash script and sourcing a bash script? very good answer:
The differences are:
When you execute the script you are opening a new shell, type
the commands in the new shell, copy the output back to your current
shell, then close the new shell. Any changes to environment will take
effect only in the new shell and will be lost once the new shell is
closed.
When you source the script you are typing the commands in your
current shell. Any changes to the environment will take effect and stay in your current shell.

No output when running jar from shell script

I am running a jar-file in a shell script calling hierarchy. I want to redirect the output from executing the jar file to my logfile "loga.log" but this does only work when I run this script with "sh"-command.
My calling hierarchy basically looks like this:
Script A calls script B with:
/path/to/script/scriptb.sh &> /path/to/logs/loga.log
Script B calls script C with:
/path/to/script/scriptc.sh > /path/to/logs/logb.log
Script C executes jar-file with:
java -Xms512m -Xmx1280m -Djava.awt.headless=true -jar /path/to/jar/thejar.jar > /path/to/logs/stdout.log #2>/path/to/logs/stderr.log
The first script is started by command line with:
sh /path/to/scripts/scripta.sh
and then I get written my output as wanted to the loga.log-file. When I start the script via:
/path/to/scripts/scripta.sh
this doesn't work and the output is displayed in the console. Unfortunately I am not able to use the sh command because these scripts must be triggered by a UC4-job system which calls this script without using sh. Calling the second script using "sh" inside the first one doesn't work eigther.
How can I edit the scripts to be able to log all output to the specified logfile when running the first one via the above command (without sh)?
Thanks in advance,
Martin

Launch shell scripts from Jenkins

I'm a complete newbie to Jenkins.
I'm trying to get Jenkins to monitor the execution of my shell script so i that i don't have to launch them manually each time but i can't figure out how to do it.
I found out about the "monitor external job" option but i can't configure it correctly.
I know that Jenkins can understand Shell script exit code so this is what i did :
test1(){
ls /home/user1 | grep $2
case $? in
0) msg_error 0 "Okay."
;;
*) msg_error 2 "Error."
;;
esac
}
It's a simplified version of my functions.
I execute them manually but i want to launch them from Jenkins with arguments and get the results of course.
Can this be done ?
Thanks.
You might want to consider setting up an Ant build that executes your shell scripts by using Ant's Exec command:
http://ant.apache.org/manual/Tasks/exec.html
By setting the Exec task's failonerror parameter to true, you can have the build fail if your shell script returns an error code.
To use parameters in your shell you can always send them directly. for example:
Define string parameter in your job Param1=test_param
in your shell you can use $Param1 and it will send the value "test_param"
Regarding the output, everything you do under the shell section will be only relevant to the session of the shell. you can try to return your output into a key=value txt file in the workspace and inject the results using EnvInject Plugin. Then you can access the value as if you defined it as a parameter for the job. In the example above, after injecting the file, executing shell echo $Param1 will print "test_param"
Hope it's helpful!

Resources