Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Process Builder to launch UNIX shell

843789Oct 1 2009 — edited Oct 8 2009
I have a java program creating a UNIX shell file, changing permissions on the file, and launching the .sh file.
(We did this as we kept hitting a 255 character limit in UNIX when we directly send the command using Java.)

Anyways... the code is working correctly for the first job.

The Java service is started on a Solarius Server (in a ksh) , it wakes up every hour and checks to see if it has a job to run.
If it finds a job, it takes the information from an external txt file and then reads in the information.

It then creates a Unix.sh file, changes permissions on that file to 755, and finally it sends an execute command. (/bin/ksh -c /var/tmp/shellfile.sh)

This all works the first time the service is started.... but when one hour goes by and the service wakes up to run the next job,
it creates the Shell file, changes the Permission to 755.... but then failes to execute.

Here is the code (I've only changed a couple text entries to protect my customer...)
try
{
File tmpfile = File.createTempFile("UNXCMD", ".sh");
Writer output = new BufferedWriter(new FileWriter(tmpfile.getAbsolutePath()));
output.write("#!/bin/ksh \n\n");

output.write(cmdtoexe);
logs.writeBytes("INFO : Command --> " + cmdtoexe);
output.close();

System.out.println("UNIX Command to execute : chmod 755 " + tmpfile.getAbsolutePath());

String[] chgperm ={ "chmod", "755", tmpfile.getAbsolutePath() };
ProcessBuilder pbBuilder1 = new ProcessBuilder(chgperm);
pbBuilder1.start();

String[] str ={ "/bin/ksh", "-c", tmpfile.getAbsolutePath() };
ProcessBuilder pbBuilder2 = new ProcessBuilder(str);
pbBuilder2.redirectErrorStream(true);


Process pbs = pbBuilder2.start();

InputStream input = pbs.getInputStream();
InputStreamReader isr = new InputStreamReader(input);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}

pbs.waitFor();

System.out.println("INFO : execution completed : ");
logs.writeBytes("INFO : execution completed : ");

}
catch (Exception ex) { ex.printStackTrace(); }


When I look into the log file I see the following error on the SECOND run of the service.

/bin/ksh: /var/tmp/UNXCMD57104.sh: cannot execute


I can manually run this file from the command line successfully, so I know it is some issue in the Java.

Thanks for any help!!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 5 2009
Added on Oct 1 2009
17 comments
1,869 views