I'm trying to execute a script from within my java code which looks like:
Process p = Runtime.getRuntime().exec(cmdarray, envp, dir); // cmdarray is a String array
// consisting details of the script and its arguments
final Thread err = new Thread(...); // Start reading error stream
err.start();
final Thread out = new Thread(...); // Start reading output stream
out.start();
p.waitFor();
// Close resources
The execution of the script is over(it's pid is no more), but java is stuck on waitFor()
method of the process!.
And yes, I'm reading output and error streams in 2 separate threads. Yes, they are being joined at the end(after waitFor()
).
The script basically installs a few RPMs(like 10 or so) and configures them. So the script runs for a little over 60 seconds.
It looks similar to the following:
#!/bin/sh
#exec 3>&1 >/var/log/some_log 2>&1
# If the above line is uncommented, Java recognizes that the
# process is over and terminates fine.
tar xzf a-package-having-rpms.tar.gz
cd unpacked-folder
(sh installer-script.sh) #This installs 10 odd rpms in a subshell and configures them
cd ..
rm -rf unpacked-folder
exit 0
Shockingly enough, if I put the following line in the script(at the top), Java understands the script is over and it terminates the process perfectly.
exec 3>&1 > /var/log/some_log 2>&1
For the record, the script doesn't generate any output. Zero chars!. So putting exec statement there makes no sense!
But still, magically enough, putting exec statement in the script makes java work!. Why??
How can I avoid that illogical exec statement in the script?.
If you are interested in what installer-script.sh looks like then:
#!/bin/sh
exec 3>&1 >>/var/log/another-log.log 2>&1
INSDIR=$PWD
RPMSDIR=$INSDIR/RPMS
cd $RPMSDIR
#
rpm -i java-3.7.5-1.x86_64.rpm
rpm -i --force perl-3.7.5-1.x86_64.rpm
rpm -i --nodeps mysql-libs-5.0.51a-1.vs.i386.rpm
rpm -i --nodeps mysql-5.0.51a-1.vs.i386.rpm
rpm -i --nodeps mysql-server-5.0.51a-1.vs.i386.rpm
rpm -i --nodeps perl-DBD-MySQL-3.0007-2.el5.i386.rpm
rpm -i --nodeps perl-XML-Parser-2.34-6.1.2.2.1.i386.rpm
.
.
.
Now, why exec command in the first script is required for Java to know that the process is over? How can I avoid that exec?, esp. since the first script doesn't produce any output.
Waiting for answers with bated breath!