简体   繁体   中英

When invoking a shell script from oracle DB Java procedure, the linux commands inside script are not being recognized

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 ();
}

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

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM