简体   繁体   English

从 oracle DB Java 过程调用 shell 脚本时,不能识别 ZE206A55E424B5E91Z 脚本 74E9769089CCE50

[英]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. 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.该脚本包含一些标准命令,例如 whoami(查找 session 用户)、ls、.. 当直接从服务器调用时,该脚本工作正常。 However when invoked from the DB JAVA procedure, all it recognizes is 'echo' command.然而,当从 DB JAVA 过程调用时,它只识别“echo”命令。 Any thoughts in this regard will be of great help.在这方面的任何想法都会有很大帮助。 Thank you!谢谢!

shell script(FileTransfer.sh): shell 脚本(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 JAVA 程序

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.正如您在对问题的评论中确认的那样,当您的脚本通过 Java 代码调用时, $PATH为空。

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.它适用于echo ,因为这是 shell 本身内置的命令。

Check the path for whoami and ls with which :检查whoamils 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.你好,路径----/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 但这仅在从 shell 调用时再次出现。 when done from JAVA, i dont get any output for $PATH..从 JAVA 完成后,我没有得到任何 $PATH 的 output ..

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.最简单的方法是启动脚本并获取运行它的用户的配置文件(例如 .bash_profile)。 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.环境,当然还有 PATH 确实很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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