Bat script pausing while performing long action - windows

I'm running a bat script on a Windows 2008 server. The script is launching a jar file which is creating indexes in a database. Each index may take several minutes to create and during some of those creations, the command prompt pause. I have to hit enter so the script can continue running. I'm guessing this can be due to index creation being to long, the command prompt go to 'sleep' and it has to be woken up, be I couldn't find any meaningful information about such a behavior.
Is there any way to prevent the command prompt to go to 'sleep' like that?
Here the part of the script lauching the jar :
echo [INFO] Starting build SQL Index 4P8...>>%logFile%
set msg=[INFO] Creating SQL INDEX FILE...
echo %msg%
echo %msg%>>%logFile%
set UserName=
set PassWord=
"..\jre\bin\java.exe" -jar %toolFile% -port=%portNumber% -u=%UserName% -p=%PassWord%
if ERRORLEVEL 1 (
echo [FAILED] Failed to create SQL INDEX FILE, please see Java Logs...
echo [FAILED] Failed to update SQL INDEX FILE, please see Java Logs...>>%logFile%
exit 1
)
set msg=End of the application, please see logs !
echo %msg%
echo %msg%>>%logFile%
It's not really relevant though, as the pauses occur during the jar execution.

Related

How to return exit status of Matlab to Jenkins console output?

So far,I have tried using Jenkins to trigger matlab and do some test.But I want to let Jenkins know that if we have some error in matlab test,and communicate with Jenkins.So the console output will be failure.
Matlab version:2016b
So I put exit(1) in the last line of matlab script.But Jenkins console output still reveal SUCCESS.And I use %errorlevel% in batch command line,the result was 0 not what I expect.
I have tried use Start /wait in the beginning.Because I use Windows 10.But isn't help.
Below is my Jenkins batch command:
start /wait matlab.exe -wait -r -sd "D:\matlab" MyScript;exit -logfile OutputPrint.txt echo ExitCode is %errorlevel%
I expect that if some test cases in test manager didn't pass.It could inform Jenkins and the console output will be failure.
Thanks in advance for any help!
It seems that official support for providing an exit or error status from matlab was introduced in R2019a. I say this, because I see it in the online reference in R2019a, but it is missing in R2018b:
https://www.mathworks.com/help/releases/R2019a/matlab/ref/quit.html
vs
https://www.mathworks.com/help/releases/R2018b/matlab/ref/quit.html
However when I try it and do not give the 'force' input, it does seem to work for older versions of matlab, e.g. for R2016b:
"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -wait -r "exit(10)"
echo %ERRORLEVEL%
This gives me 10, which is what I expected. In your code, I see a couple of things that may give issues. I recommend to start with something simple as in my code and then add additional components if necessary and verify the errorlevel is still ok.
I see you're using start, this may block the exit code from reaching Jenkins
I see
-r -sd "D:\matlab" MyScript;exit
since the command to execute should come directly after -r, I think you meant:
-sd "D:\matlab" -r MyScript;exit
Finally because of the command "echo" after the matlab command, the errorlevel will be from the echo command. So if you want Jenkins to get the errorlevel from the MATLAB command, but you do want to run something after matlab, you should probably capture it in a separate variable and "reset" the errorlevel just before exiting the script, e.g.:
"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -wait -r "exit(10)"
SET MATLABERROR=%ERRORLEVEL%
echo ExitCode is %ERRORLEVEL%
SET ERRORLEVEL=%MATLABERROR%

Executing windows batch command in Jenkins keeps failing with Exit-1 state

I am testing out "Jenkins ver. 2.89.4" in Windows10 Machine and I have configured a simple job to test out few things. Under Build section in Jenkins, I have used "Execute Windows Batch Command" and used the following two commands. Both the below commands executes fine in the command prompt but however the Jenkins Build job keeps getting failed with Exit 1 state.
date
echo "SampleBuild job completed successfully."
Couldn't able to get the reason for the failure. And the following is what we see in console output.
Started by user Administrator
Building in workspace C:\ProgramData\Jenkins\workspace\SampleBuildJob
[SampleBuildJob] $ cmd /c call C:\WINDOWS\TEMP\jenkins6658106255890809140.bat
C:\ProgramData\Jenkins\workspace\SampleBuildJob>date
The current date is: Fri 02/23/2018
Enter the new date: (mm-dd-yy)
C:\ProgramData\Jenkins\workspace\SampleBuildJob>echo "SampleBuild job completed successfully."
"SampleBuild job completed successfully."
C:\ProgramData\Jenkins\workspace\SampleBuildJob>exit 1
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
Can anyone tell me, what am I missing?
Try to add (call ) to end of your Batch Command.
This will clear the errorlevel environment variable, there for force the build result to be successful.
If you want to check a specific command's result to determine the build result. Let's say you want to check dates command.
(dates) || (exit %errorlevel% )
This will fail the build if error happens in the first command.
or
(dates) && (echo "command executed succesfully!")
This will show the message only when the first command successfully executed.
Then with the changed command, you do not need (call ) any more.
This windows batch script will work:
echo %DATE%
echo %Time%
echo "command executed succesfully!"
The windows batch script show the same error to me.
Finally i have added exit 0 at the end it worked

capture exit code from a script flow

I need help with some scripts I'm writing.
Scenario:
Script A is executed by a scheduling process. This script takes the arguments passed to it, parses them in some way and runs script B feeding it with those arguments;
Script B does sudo -u user ssh user#REMOTEMACHINE, runs some commands (in the remote machine) and finally runs script C (also in the remote machine). I am passing those commands using a HERE DOCUMENT. Also, I'm passing the previous arguments to this script too.
This "flow" runs correctly and the job completes successfully.
My problems are:
Since this "flow" is ran by a scheduling process, I need to tell it if the job completed successfully or not. I'm doing this via exit codes, so what I want is to have a chain of exit codes, returning back from the last script to the first, in case of errors. I'm not able to perform this, because exit codes works correctly for the single scripts (I tried executing them singularly and look for the exit codes), but they are not sended back to the parent script. In my opinion, the problem is that ssh is getting the exit code from the child script, which in fact ended successfully, because there was no error executing it: it's the command inside of it that gone wrong.
While the process works correctly, I still get this line:
ssh: Could not resolve hostname : Name or service not known
But actually the script completes successfully.
I hope you understand what I wrote, I can eventually post my scripts here.
Thanks
O.
EDIT:
This are the scripts. There could be some problem with variable names because I renamed it quikly to upload the files.
Since I can't upload 3 files because of my low reputation, I merged them in a single file
SCRIPT FILE
I managed to solve the problem.
I followed olivier's advice and used the escape char to make the variable expanded by the remote machine.
Also I implemented different exit codes based on where the error occured.
At last, I modified the first script as follows, after launching sudo -u for the second script:
EXITCODEOFTHESECONDSCRIPT=$?
if [ $EXITCODEOFTHESECONDSCRIPT = 0 ]
then
echo ""
echo "Export job took $SECONDS seconds."
echo ""
exit 0
else
exit $EXITCODEOFTHESECONDSCRIPT
fi
This way I am able to exit the main script MAINTAINING the exit code provided from the second script.
In fact, I found that the problem was that the process worked well, even in case of errors, but the fact that I was giving more commands after the second script fail (the echo command was enough) provided other exit codes that overwrited the one I wanted.
Thanks to all !

Does SQLPlus exit after running a script?

We are using CA Workload Control Center (Autosys) to run a windows batch file which calls another batch file which calls SQLPlus with the following:
sqlplus username/password %1 %2 %3
Since this is all being done remotely I cannot see what is actually happening on the server.
I am wondering if the sqlplus file exists but has invalid syntax (like referring to non-existent table) will the sqlplus prompt exit and return back to the batch file?
The reason I ask is because I am looking at the log file from Autosys and it shows that SQLPlus call was made, it prints out ERROR AT LINE 2: Invalid Table, but then it does not show any other activity with the batch script after that where there are multiple echoes and file copies etc. It seems as though it is not exiting SQLPlus perhaps?
Is there a parm I need to pass to SQLPlus to tell it to exit SQLPlus and return back to the calling script after running a SQL script if it fails?
Edit: We are using "WHENEVER SQL ERROR" inside of our SQL files as well and the log file does show this:
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
But I am still expecting that it should continue with the rest of the Batch script but its not, above is the last that Autosys shows in the log
See SQLPlus instruction WHENEVER SQLERROR, it describes in Oracle docs:
http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12052.htm
Found the solution SO Post:
You can do sqlplus -l test/Test#mydatabase #script.sql; the -l flag means it will only try to connect once, and if it fails for any reason will exit instead of prompting. Look at the output of sqlplus -?, or see the documentation.

Oracle Script File Execution in *.bat file not completing

I have the following *.bat file executing itself on a schedule. This file consists of different tasks, primarily involves in having a the backup dump file exported to the Test DB Machine every day.
#echo off
#echo %time%
del C:\Factory\index.sql
echo exporting indexes...
Imp jksbschema/password file=C:\DBDUMP\jksb.dmp full=yes log=C:\Factory\log\dump_index.log indexfile=index.sql
#echo %time%
echo connecting to Oracle...
echo Delete and Re-Creation of User
sqlplus "sys/password as sysdba" #C:\Factory\script\step_1.sql
echo importing dump file begins...
#echo %time%
Imp jksbschema/password file=C:\DBDUMP\jksb.dmp full=yes log=C:\Factory\log\dump.log Compile=n INDEXES=n
echo Applying security priviledges
#echo %time%
sqlplus "sys/password as sysdba" #C:\Factory\script\step_2.sql
#echo %time%
ssr 0 "CONNECT" "REM CONNECT" C:\Factory\index.sql
echo Loading Indexes....`enter code here`
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql
#echo %time%
exit
shutdown.exe -r -t 00
Since its a long operation, I execute itself on every night, expecting it to be ready next morning. I also include a command restart the machine once the entire process is over. All the command finishes without asking an input expect the following command:
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql
after the above command executes, the batch file asks for an input to exit the process as the snapshot below says
Can anybody help in automatically finish the job and restart itself, rather than ask for an input after the index.sql file is loaded?
Create a file named quit.txt, inside it put the text "quit".
Where you see the lines that execute sqlplus, like this one:
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql
change them to this:
sqlplus "jksbschema/password as sysdba" #C:\Factory\index.sql < quit.txt
Old stuff, but however: Let "exit;" be the last command of your SQL script. This tells sqlplus to quit and finish execution.

Resources