How can I execute SQL*PLUS to execute a script. The script will generate a file, and I would like to move that generated file to a directory.
This is how the process is.
In run.sh file, I give values to parameters of a bash script
./run.sh <schema_name> <user_name> <password> <environment>
These values are passed to the sql.java:
first java goes to the following directory.
./system_scripts
There java starts SQL* PLUS and executes a script (gen.ddl) by using this command:
sqlplus <user_name>/<password>#<environment> gen.ddl
This will create a result file: result.ddl
Move (not copy) that result.ddl file to. Overwrite the previous file if any:
./schema/<schema_name>/scripts
print a message
job done
and done.
How can I do so?
thanks in advance :-)
Related
I am stuck in a scenario where I need to run a jar file which decrypts an encrypted file on regular interval from a particular directory on a windows server. Anyone can help in powershell which executes the jar file ?
I use the following command to run the jar file using command prompt and it works fine on local machine.
java -cp PGPDecrypt.jar pgpDecryptPackage.PGPDecrypt C:\Users\anirudgu\Desktop\abcd\TestFile.pgp C:\Users\anirudgu\Desktop\abcd\anirudguprivatekey.asc C:\Users\anirudgu\Desktop\abcd\outputfile.txt Passcode
This jar takes 4 parameters as input
File Path of encrypted file
File path of private key
File path where we need to put the output i.e. decrypted file
The passphase
The output of the is basically a string : File decrypted successfully and placed at location : C:\Users\anirudgu\Desktop\abcd\outputfile.txt
Can someone please suggest on the powershell side ?
This should work in powershell:
cmd.exe /c 'java -cp PGPDecrypt.jar pgpDecryptPackage.PGPDecrypt C:\Users\anirudgu\Desktop\abcd\TestFile.pgp C:\Users\anirudgu\Desktop\abcd\anirudguprivatekey.asc C:\Users\anirudgu\Desktop\abcd\outputfile.txt Passcode'
The '/C' argument, executes a cmd session and then terminates.
For creating a scheduled script:
https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler
shell script file directory: /some/location/myShellScript.sh
Properties-Type: shell script (application/x-shellscript)
EDIT
content of shell script:
#!/bin/bash
export LD_LIBRARY_PATH=`pwd`
echo `pwd`
./someExecutable ../input/cfg/test1.ini
The test1.ini is generated one step before in the java code,
it provides settings for some testing, which is done in the background. Then the shell script ends up with the file I need for further processing.
/EDIT
When I am running this shell script on linux terminal in its own directory just with "./myShellScript.sh" it works perfectly fine...
The part my shell script shall be executed:
//Do something before
//Shell scripts creates a file
String cmd = /some/location/myShellScript.sh;
ProcessBuilder pb = new ProcessBuilder(cmd);
Process process = pb.start();
int exitValue = process.waitFor();
System.out.println(exitValue);
//Afterwards I am processing the generated file
When running my java program as an executable .jar file, this process gets not executed and the exitValue is 127, but I don't know why...
I tried many things like:
using the Runtime to exec
adding #!/bin/bash or #!/bin/sh on top of the shell script
adding a "sh" parameter to the process command in form of String[]
In my execution directory, I changed the permission with chmod 755 -R * recursively so every associated library used by the shell script is indeed available (also due to the fact, that I can just execute it on the terminal).
I really tried to find a proper answer on the internet but I wasn't successful.
And no, I cannot just do everything in java, the shell script is mandatory and cannot be replaced in this case.
Thanks in advance for helpful suggestions!
The script you are executing is highly sensitive to its working directory. It uses pwd to set the LD_LIBRARY_PATH and it attempts to execute another program via a relative path to that program, providing a relative path as a command-line argument, as well.
The working directory for an execution of the script has no essential relationship with the directory in which the script resides -- it completely depends on how and in what context the script is launched. For example, you report that the script works as you expect "When I am running this shell script [...] in its own directory." But when you run the script from Java you very likely are not running it with its own directory as the working directory, and that will strongly affect this script's behavior.
One solution would be to hardcode the script's installation path into the script itself, and to express all your paths relative to that:
#!/bin/bash
installation_dir=/path/to/the/script/dir
export LD_LIBRARY_PATH=$installation_dir
"$installation_dir"/someExecutable "$installation_dir"/../input/cfg/test1.ini
It's a bit kludgy to hardcode the path, though. You could further improve it by having the script identify its own directory at runtime:
#!/bin/bash
installation_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
export LD_LIBRARY_PATH=$installation_dir
"$installation_dir"/someExecutable "$installation_dir"/../input/cfg/test1.ini
That's obviously Bash-specific, but you were using bash anyway. Alternatively, if the executable your script launches is also sensitive to its working directory, then perhaps you just want the script to change directory (which will be effective only for the script and processes downstream from it, not for its parent process):
#!/bin/bash
cd "$( dirname "${BASH_SOURCE[0]}" )"
export LD_LIBRARY_PATH=`pwd`
./someExecutable ../input/cfg/test1.ini
The 127 exit status means that a command used in the script is not found.
EDIT
Debug the script, when bash is used, add the line below on the second line:
exec > /tmp/debug.txt 2>&1 ; set -x
After the next attempt, analyze the traces generated into the /tmp/debug.txt file.
OLD INTRO
(the script content was not yet provided)
The Java program which executes the myShellScript.sh script has probably not the same PATH environment variable than the one which is set in your environment when you execute the script manually from a terminal.
The following process normally works for my startup scripts. However, when I introduce a command to execute a JAR file, it does not work. This script works while I am logged in. However, it does not work as a startup script.
In /etc/init.d I create a bash script (test.sh) with the following contents:
#!/bin/bash
pw=$(curl http://169.254.169.254/latest/meta-data/instance-id)
pwh=$(/usr/bin/java -jar PWH.jar $pw &)
echo $pwh > test.txt
Make script executable
In /etc/rc.local, I add the following line:
sh /etc/init.d/test.sh
Notes:
I make a reference to the script in /etc/rc.local, because this script needs to run last after all services have started.
Please do not ask me to change the process (i.e., create script in /etc/init.d/ and reference it from /etc/rc.local), because it works for my other startup scripts.
I have tried adding nohup in front of java command, and it still did not work.
Thanks
As written, there is insufficient information to say what is going wrong. There are too many possibilities to enumerate.
Try running those commands one at a time in an interactive shell. The java command is probably writing something to standard error, and that will give you some clues.
I'm having the following situation.
I have a java programm packed in a jar file. If I call java -jar myProgramm.jar everything is working fine. The file is reading some values from build.xml (ant file). This file is in the same directory where myProgramm.jar is located.
In our company we wrap everything in shell scripts to have a uninfied way to call our scripts.
So my shell script myProgrammWrapper.sh looks like this:
#!bin/bash
java -jar $(cygpath -w ~/path/to/tools/myProgramm.jar) "$#"
The cygpath command is there because the sh is executed within cygwin and otherwise the path would not be found.
The"$#" passes the arguments to the program.
Following is the problem:
Our cygwin environment has been setup in a way that I can call myProgrammWrapper.sh from every directory. But of course when I call it from any random location, the build.xml is not found.
Is there a way to reference the build.xml in the shell script. It is located in ~/path/to/tools/ ?
First I was thinking about copying the build file to the current directory and deleting it afterwards. This is working fine but has one fundamental flaw. We are working a lot with ant and have build.xml files. So if someone would execute myProgrammWrapper.sh in a directory where there is already a build.xml file. It would be overwritten.
Maybe the problem can be tackled from the Java side. Any ideas and input is appreciated.
Why not make the Bash script change the working directory to where the XML file is contained?
Try:
(cd $(cygpath -w ~/path/to/tools/) && java -jar $(cygpath -w ~/path/to/tools/myProgramm.jar) "$#")
I have to execute a abc.bat file in a perl script.
If I run the batch file manually it asks for first param at first step. When I provide this at second step it will ask for second param..
I need to call this abc.bat file in a perl script
Something like..
system(‘abc.bat’); or exec()
But how to provide param run time..
The abc.bat batch file is a calling a java myclass which takes two params at runtime.
Below is the batch file
#echo off
setlocal
set classpath=.\my.jar;%classpath%
"%JAVA_HOME%"\bin\java com.myclass
#echo on
Please help .. Thank you.
Can you just use the %ENV variable to do everything directly in perl? Maybe:
$ENV{classpath} = ".\\my.jar;$ENV{classpath}";
system("$ENV{JAVA_HOME}\\bin\\java com.myclass");