read output of shell script in ANT Java task - java

I'll appreciate any help .
I created the shell script that collects servers IP - members of weblogic cluster to distribute some files across all servers in list.
I prepared a special ANT task for distribution and call this task from another shell script . Each time I call the ANT task I pass the next server IP to copy files on it.
This behaviour brings the installation structure.
Now I'd like to improve this behaviour . I want in ANT to read the output of the shell file to some list that I can loop thru from the ANT task.
Thanks

You can simply use the outputproperty attribute of the exec ant task. See http://ant.apache.org/manual/Tasks/exec.html for the full documentation. With this, you can also handle standard error output.
Once the script output is in an ant property, you can iterate on it using ant contrib foreach task foreach task.

Related

Executing shell script containing java -jar Runtime.exec(), which java will it take, OS level or application level

I am trying to execute a java jar from my jmeter tests via beanshell.
The approach I followed is to create a shell script, execute it through beanshell using Runtime.exec() function.
The question I have is whether execution of this shell script will take java from instance/host/OS level or it will be executed as part of current java that the application is using.
There could be open questions for which I want to provide an answer to before time:
I have a maven project, in which I specify java.
The build runs the jmeter script which has beanshell executing the jar file.
I also do some processing with the output of the jar execution(write specific output to another file, hence I wrote the shell script).
I use this specific output, and use it to add to my request in jmeter before querying.
Any help is appreciated.
In a way, both.
The shell script will be executed as part of your Java program, but it will be executed using the system's default Java executable unless you've specified a Java executable in the exec() method call.
Depending on how you run your application. If you just use
Runtime.getRuntime().exec("java -jar ....");
The Process instance will look for java executable which is available in your OS PATH. If there will no be java executable in the PATH - the call will fail.
Be aware that starting from JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for any form of scripting so consider migrating to Groovy on next available opportunity. For instance you can kick off the process and get the output as simple as:
String response = "your command".execute().text
Also instead of running your .jar in as separate process it might be better to add it to JMeter Classpath and call necessary functions directly from the Groovy code. See Apache Groovy - Why and How You Should Use It article for more information.

Windows Task Scheduler Program Ends Immediately

I have a Java executable (.exe) with a given JRE build in the same folder, which it uses to actually run.
I want to put this executable on Windows Task Scheduler.
I did some tests with some C++ hello world programs, and all went fine. This Java program, running directly (by two clicks or whatever) works all fine too (it is supposed to write to a file and end).
However, when I put the Java program in the Task Scheduler, it exits immediately, with status code 0x0 (success) and nothing is actually performed.
At Windows Task Manager, I see that javaw.exe starts and exits in a glimpse.
What could it be? Something related to Java? Something due to a specific task scheduler flag?
Aditional:
Java executable built with launch4j.
Scheduler set with schtasks /create /tn MyETL /sc hourly /mo 3 /tr C:\ETL\etl.exe
When you run an application with Windows Scheduler, if that application has dependencies to other files via relative path, then you need to set the start in setting for the task. This sets the path from where execution will begin.
Alternatively you can use a command file and have it navigate to the correct directory first.
Just figured out that the problem was that the program was actually being executed in the wrong folder, in order that the output file wasn't where I thought it would.
The output file was being write in the starting folder, not the program's folder.

starting and stopping a jar from jenkins

I've been working on my jenkins server off and on for the last few weeks. Right now it runs fine building and outputting the files to a remote location. However, i'm looking to do some pass or fail tests on the files before they are uploaded. This way i avoid releasing broken versions of my work. As well avoid more bug reports that i do not need to be reading.
The files i'm looking to run are actually plugins for another program. So i need to actually start this program as i can configure all the files before hand. The program is a .jar file which i know i can launch using a bat of shell script. The issue is i don't know at this point how to terminate the .jar program after it has been running. All the solutions i've found require me to modify the jar to terminate itself, or kill the JVM. Both i can't due for varies reasons.
It's a heavy hack, but you can make a shell script start a background thread that checks if the JVM is done, then terminate it.
Rudimentary example in bash:
start_jvm_command >>console.log 2>&1 </dev/null &
while ! grep 'done signal' console.log; do sleep 1; done
pkill -f "regexp for start_jvm_command"
Note: I was using pkill to terminate to avoid wrapper shell scripts.
It been a year since i asked this question but i found a much nicer way to solve this question. Its called JUnit and is a testing lib for java. With it i don't need to launch the entire jar but can test peace by peace at a time. As well if require i can write a test to launch the entire .jar file. In the test i can check if files exist, if functions complete correctly, and if any error is thrown during runtime.

Execute grep on remote site to detect running process

I have searched all over but found no luck yet, I am using linux ubuntu flavour and need to know that how can I check that whether the java jar file is running at remote end or not using pgrep. As the pgrep -f "someProcess" returns the Process ID.
Note: Both systems are linux based, and I only need to done it through terminal. Any ideas would be appreciated.
I've used Ant to do this.
I can't remember which task I used unfortunately....
But we executed a command or script remotely using an ant task.
I think we used ExecTask.
Notice you can specify an output file.
You can read that to process the output of your command.
Ant is pretty awesome in that anything you can do with an Ant script, you can do with Java.
And it's a lot easier than you'd think.
I believe this is a good starting pont to learn how to do it.
Here is the Javadoc overview page for the Ant classes.
Here are some more links to helpful Javadoc pages:
Project
Task (the base class for all Ant tasks)
SSHExec task (looks like this is what you want)
ExecTask
Copy task (this is a nice simple one for "Hello, World" purposes
FTP task
I'm sure there other ways, but as they say here Ant makes things very easy for you.
I can think of one way to do this.
Assuming you have servers A and B, A performs the process check on B. You can use ssh and setup no-password login from A->B and use command below:
ssh root#12.34.56.78 "pgrep [whatever keyword]"
You'll get the same result as executing the command on B.
You could use plink.exe to do this. You can pass commands to plink in the form of a .txt file input.

Eclipse run configuration to run remotely using a script

I would like to use a run configuration (or something similar) to run a class from Eclipse using a shell script. The shell script will do a bunch of fancy stuff to make the job run on a machine with more memory. That part I can do.
What I don't know is how to make Eclipse pass the class name and class path to a shell script - and ideally show the output of the shell script in the console window. This seems like it should be simple. I'm using 3.5.2.
Thanks!
I'm not sure of a clever way to pass your classpath to your script, but the External Tools Configurations can do this. And the output will be printed in your console. There are Variables that can be set up to manage your classpath a bit better than I have done below. Though I've used the working directory as my classes folder, so I only need to use . as my classpath.
Variables can be used throughout to set up common paths etc.

Categories