0

To debug a possible permission related issue while invoking an FTP, testing a shell script by invoking it from an oracle JAVA procedure. The script contain some standard commands like whoami(to find out the session user), ls,.. The script works fine when invoked directly from the server. However when invoked from the DB JAVA procedure, all it recognizes is 'echo' command. Any thoughts in this regard will be of great help. Thank you!

shell script(FileTransfer.sh):

``#!/bin/ksh` 
`echo "from double" >> /usr/users/ais/data/utl_data/Logs1.txt`
whoami >> /usr/users/ais/data/utl_data/Logs1.txt
ls >> /usr/users/ais/data/utl_data/Logs1.txt
echo "from double line 11" >> /usr/users/ais/data/utl_data/Logs1.txt
echo "from double line 13" >> /usr/users/ais/data/utl_data/Logs1.txt

JAVA Procedure

public class FTPTest{
public static int testSH () throws Exception {
   String[] command = { "sh" "/usr/users/ais/data/utl_data/FileTransfer.sh"};
   Process p = Runtime.getRuntime().exec(command);
   try {
     p.waitFor ();
   }
   catch (InterruptedException ie) {
   ;}
   return p.exitValue ();
}
3
  • Sure that the PATH settings for the script when executed through the Java code will point to the location for whoami and the other commands? Commented Jun 10, 2021 at 13:39
  • Add an echo $PATH to the script ... Commented Jun 10, 2021 at 13:40
  • Hi, path----/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/puppetlabs/bin:/usr/users/oracle/.local/bin:/usr/users/oracle/bin But this is again only when invoked from shell. when done from JAVA, i dont get any output for $PATH.. Commented Jun 10, 2021 at 14:30

3 Answers 3

1

As you confirmed in the comments to your question, $PATH is empty when your script is called through your Java code.

This means that you have to provide the fully qualified names for your commands in the script; on my system, the script then should look like this:

#!/bin/ksh 
echo "from double" >> /usr/users/ais/data/utl_data/Logs1.txt`
/usr/bin/whoami >> /usr/users/ais/data/utl_data/Logs1.txt
/bin/ls >> /usr/users/ais/data/utl_data/Logs1.txt
echo "from double line 11" >> /usr/users/ais/data/utl_data/Logs1.txt
echo "from double line 13" >> /usr/users/ais/data/utl_data/Logs1.txt

It worked for echo because that is a command built-in to the shell itself.

Check the path for whoami and ls with which:

which whoami
which ls
Sign up to request clarification or add additional context in comments.

Comments

0

Hi, path----/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/puppetlabs/bin:/usr/users/oracle/.local/bin:/usr/users/oracle/bin But this is again only when invoked from shell. when done from JAVA, i dont get any output for $PATH..

The easiest way is like following:

String[] command = { " sh ", "/usr/users/ais/data/utl_data/FileTransfer.sh" };

String[] envp = { "PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/puppetlabs/bin:/usr/users/oracle/.local/bin:/usr/users/oracle/bin" };

Process p = Runtime.getRuntime().exec(command, envp);

Comments

0

With many other scheduling tools the same 'problem' exists. Easiest is to start the script and have that source the profile (.bash_profile for example) of the user it is running as. That way you have some flexibility you can run the script manually using the same environment too.

The environment and certainly the PATH does matter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.