Unix Script not running in Java using Process Runtime - java

I am developing an application where i required to run some of the scripts of unix from Java Code.
Platform i am using is Unix, Tomcat 5.5..
For that, My Sample Code is as follows :
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("netstat -i|tail -n +3|cut -d ' ' -f1");
System.out.println("exitValue = "+proc.exitValue());
I have given all the rights to tomcat user.
Now, my program output cases are :
Script exitValue()
======= ============
netstat -i 0
netstat -i|tail -n +3 4
sudo netstat -i 1
sudo netstat -i|tail -n +3 1
Above table suggest that only 1st script is executing in unix, all others are failing.
I am not sure, but i am just assuming that i have to run Tomcat Server as a root user..
Can anybody have any other solution, then please reply..
Thanks in advance...

If I remember correctly, pipes ("|") are handled by the shell. Java will probably not handle them at all ...
There are a few workarounds :
run bash with your commands as a parameter :
runtime.exec("bash -c \"netstat -i|tail -n +3|cut -d ' ' -f1\"");
write a bash script that run all those commands and run this script from Java :
#!/bin/bash
netstat -i|tail -n +3|cut -d ' ' -f1
create the pipes in Java : read the output of netstat -i and connect it in Java to tail -n +3 ...

Using | to chain commands in Unix is part of the shell, and Runtime.exec() runs the command directly, not though the shell. A quick fix may be (untested as I don't have a Unix box available at this moment) to prefix the shell as the first command.
Process proc = runtime.exec("/bin/sh netstat -i|tail -n +3|cut -d ' ' -f1");

Got the solution of above problem..
I have just created simple shell script file, and put the script inside that .sh file.
Now at the java side, i am just calling simple shell script file..
Process proc = runtime.exec("sh /usr/tmp/try1.sh");
That's it!!!

Related

not able to execute next statement in a shell script after running a java jar app using nohup

Not able to execute other instruction after if I am running a java jar file (which is present on another host) from my shell script.I tried using nohup but still not able to exit.
following is my script
#!/usr/bin/env bash
sshpass -p "${array[1]}" ssh -oStrictHostKeyChecking=no ${array[0]}#${array[2]} "cd ${array[3]} && echo -ne '\n' | nohup java -jar myapp.jar";
#some other instructions
echo "next statement"
already tried Scripts with nohup inside don't exit correctly but it doesn't worked.
sshpass -p pass ssh -o StrictHostKeyChecking=no xyz#hostname 'cd dir; nohup java -jar myapp.jar'
Try this with replacing the values appropriately

How can I parse java's version number from "java -version" and assign it to a variable on a remote host?

I want to fetch the Java version in Linux in a single command then save the output to a variable.
Using the command below assigns the variable to the local version of java. I want to run java -version on the remote host, parse the version number then save the output to a variable.
ssh -q -t $server "OLDJAVA=`java -version 2>&1 | cut -s -d '"' -f2`"
You need to run the command on the remote host and set a local variable to the output, which ssh will forward:
OLDJAVA=$(ssh -q $server "java -version 2>&1 | cut -s -d '\"' -f2")
The '\"' responds to the need to send an escaped quote to the remote server. (I missed this on the first iteration.) However, it is easier to do the redirection and post-processing on the local machine, since ssh will pass both stdout and stderr back. This results in the somewhat simpler
OLDJAVA=$(ssh -q $server java -version 2>&1 | cut -s -d \" -f2)
In both cases, I removed the -t option because it doesn't seem to be needed. It's possible that the -q option will create more problems than it solves (since you will definitely want to react to an ssh error.)

Monitoring Java-Runtime / Virgo Server with Shell Script

I am trying to create a script that monitors a java runtime process,which makes use of a Virgo service. It should start the process when its not running. As there is no specific service name, i was thinking of grepping the name of the process and use it as an identifier.
Here is the code i thought of:
#!/bin/sh
service=virgo
ps auxw | grep $service | grep -v grep > /dev/null
if [ $? != 0 ]
then
/opt/apromore/ApromoreCode/build.xml start-virgo > /dev/null
else
echo Apromore is running already
fi
and this is the process I try to monitor (value from ps aux)
/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java -classpath usr/share/ant/lib/ant-launcher.jar:/ usr/share/java/xmlParserAPIs.jar:/usr/share/java/xercesImpl.jar -Dant.home=/usr/ share/ant -Dant.library.dir=/usr/share/ant/lib org.apache.tools.ant.launch.Launc her -cp start-virgo
but even when the process is running, i cant identify it with the mentioned script. There is a error called $'\r'. I have no idea how to get rid of it.

Restrict kill commands when running jar file using a shell script

I have a jar file which is a program which accept user input and processes it. I am running this jar file using the below shell script:
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
java -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $#
cdt=`date +'%H:%M:%S %d/%m/%Y'`
The problem I am facing with this is, I want to restrict the user from exiting the application using any of the combinations of the below commands.
For example:
Ctrl + z
Ctrl + c
Ctrl + break
Please help me.
I recommend to you use simple start and stop script for your program;
1) create sh script with name start_myprogram.sh and put into the file ;
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
nohup java -DMY_PROG -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $#
cdt=`date +'%H:%M:%S %d/%m/%Y'`
2) create sh script with name stop_myprogram.sh and put into the file ;
#!/usr/bin/sh
USER=`whoami`
PID=`ps -xfu $USER| grep java | grep MY_PROG | grep -v grep | awk '{ print $2 }'`
if [ -n "$PID" ]
then
kill $PID
else
echo MY_PROG is not running.
fi
3) start your program ./start_myprogram.sh &
4) anytime whenever you want stop your program ./stop_myprogram.sh
*This is maybe not answer of your question but at least you dont need to implement anything more.
I would suggest the following change in the script to get to the desired requirement.
It seems that you need some kind of function which will catch these commands and not let the commands get executed. Shell script can have this kind of functionality by implementing the use of trap.
You can make change in your script like this:
PR=`basename $0`
cdt=`date +'%H:%M:%S %d/%m/%Y'`
cd $HOME/myprogram
#Add these two lines in the code for catching exit commands
trap '' 20
trap ' ' INT
java -cp $HOME/myprogram/ifxjdbc.jar:$HOME/myprogram/jarprogram.jar:. MyProgram $#
cdt=`date +'%H:%M:%S %d/%m/%Y'`
Its very simple to use traps in shell scripts. Hope this works for you.

How do I get the PID of this Java app?

I have a java app on my (Ubuntu) server. If I do this, then it starts correctly:
/usr/bin/java -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main
but I don't know how to get its PID. I don't know how to stop it with an init.d script.
I have a different app, written in Clojure, and for it I was able to write an init.d script that works great. So I tried to refashion that init.d script for my Java app, and this is what I got:
WORK_DIR="/home/jenkins/veta"
NAME="lily"
JAR="lily.jar"
USER="jenkins"
DAEMON="/usr/bin/java"
DAEMON_ARGS=" -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main"
start () {
echo "Starting lily..."
if [ ! -f $WORK_DIR/lily.pid ]; then
/sbin/start-stop-daemon --start --verbose --background --chdir $WORK_DIR --exec $DAEMON --pidfile $WORK_DIR/lily.pid --chuid "$USER" --make-pidfile -- $DAEMON_ARGS
else
echo "lily is already running..."
fi
}
stop () {
echo "Stopping lily..."
/sbin/start-stop-daemon --stop --exec $DAEMON --pidfile $WORK_DIR/lily.pid
rm $WORK_DIR/lily.pid
}
But this doesn't work. Although the PID in $WORK_DIR/lily.pid changes every time I run the script, no process with that PID ever seems to run. If I try:
ps aux | grep java
I don't see this app, nor if I try using the PID.
So is there a way I can use the first command, but somehow capture the PID, so I can store it for later?
I just want a reliable way to stop and start this app. That can be by PID or some other factor. I'm open to suggestions.
UPDATE:
Maybe my question is unclear? Something like jps or ps will give me too many answers. If I do something like "ps aux | grep java" I'll see that there are 5 different java apps running on the server. The start-stop-daemon won't know which PID belongs to this particular app, nor can I figure out what I should feed into my init.d script.
If your system has jdk installed there is an utility called jps which resides in jdk/bin. It will display the list of running java process. Make use of it.
If jdk is not installed in your machine then you have to grep the java process from ps -eaf command.
If you want the pid from the command line, this might work:
myCommand & echo $!
Which I copied from the accepted response to a very similar topic in ServerFault: https://serverfault.com/a/205504

Categories

Resources