Skip to content

DOS Batch Start-Stop DB

October 12, 2012

Basic DOS Batch script to  Start-Stop Oracle DB.

Open/Run the Command prompt as Administrator.

del db.bat     ( Delete existing Script )

Create a file named db.bat (use copy con command as below) , after pasting below contents in file , press Ctrl+Z key to save the file and Press Enter)

copy con db.bat

REM ########## Start Of Script ###############

@ECHO OFF

set exitStatus=0
set argcActual=0
set argcExpected=2

for %%i in (%*) do set /A argcActual+=1

IF %argcActual% NEQ %argcExpected% (
Echo No of args Passed are : %argcActual% where as needed : %argcExpected%
echo [USAGE]: %~0 arg1 arg2
echo [USAGE]: %~0 dbname start/stop
set exitStatus=1
goto EOBatch
)

REM sc %2 OracleService%1
NET %2 OracleService%1
sc query OracleService%1

REM Put here any code to be run before check for Service

SET TMPFILE=Tmpfile.txt
SET TMPFILE2=Runsrv.txt

REM Pls Change the Service name for Listener as per your services.

SET SRVNM=OracleOraDb11g_home1TNSListener

NET START | FIND /N “%SRVNM%” > %TMPFILE%
SET CountLines=0
FOR /F %%X IN (%TMPFILE%) DO SET /A CountLines=1+CountLines
IF 0==%CountLines% GOTO ServiceIsNotRunning

REM Put here any code to be run if Service Is Running

Echo ############################################
Echo Listener is Already up and Running !!!
Echo ############################################

If “%2″==”stop” (GOTO LSTOP)

GOTO EOBatch  REM While Satrting instance for second time it shl not check lsnr again

:LSTOP
REM TIMEOUT /T 30 /NOBREAK
sc query type= service | find /N “SERVICE_NAME” | FIND /N “OracleService” > %TMPFILE2%
REM  NET START | FIND /N “OracleService” > %TMPFILE2%
SET v_cnt=0
FOR /F %%X IN (%TMPFILE2%) DO SET /A v_cnt=1+v_cnt

IF 0==%v_cnt% GOTO DbIsNotRunning

Echo There is already Another Instance Running So Listener Will not be Brought Down !!!!
GOTO EOBatch

: DbIsNotRunning
Echo !!!! Stopping the Listener !!!!!!!!
net %2 %SRVNM%
Goto EOBatch

:ServiceIsNotRunning

REM Put here any code to be run if Service Is Not Running

Echo ############################################
Echo Listener is Down !!! starting Now
Echo ############################################

net %2 %SRVNM%

GOTO EOBatch

:EOBatch
DEL -P %TMPFILE% 2>nul
SET TMPFILE=
DEL -P %TMPFILE2% 2>nul
SET TMPFILE2=
SET v_cnt=
set exitStatus=
set argcActual=
set argcExpected=

REM Put here any code to be run after check for Service

REM ########## End  Of Script###############

C:\windows\system32>db d041 stop
The OracleServiceD041 service is stopping……
The OracleServiceD041 service was stopped successfully.

SERVICE_NAME: OracleServiced041
TYPE               : 10  WIN32_OWN_PROCESS
STATE              : 1  STOPPED
WIN32_EXIT_CODE    : 0  (0x0)
SERVICE_EXIT_CODE  : 0  (0x0)
CHECKPOINT         : 0x0
WAIT_HINT          : 0x0
############################################
Listener is Already up and Running !!!
############################################
There is already Another Instance Running So Listener Will not be Brought Down !!!!
C:\windows\system32>

C:\windows\system32>db p041 stop
The OracleServiceP041 service is stopping……
The OracleServiceP041 service was stopped successfully.

SERVICE_NAME: OracleServicep041
TYPE               : 10  WIN32_OWN_PROCESS
STATE              : 1  STOPPED
WIN32_EXIT_CODE    : 0  (0x0)
SERVICE_EXIT_CODE  : 0  (0x0)
CHECKPOINT         : 0x0
WAIT_HINT          : 0x0
############################################
Listener is Already up and Running !!!
############################################
!!!! Stopping the Listener !!!!!!!!
The OracleOraDb11g_home1TNSListener service is stopping.
The OracleOraDb11g_home1TNSListener service was stopped successfully.

C:\windows\system32>

C:\windows\system32>db p041 start
The OracleServiceP041 service is starting……
The OracleServiceP041 service was started successfully.

SERVICE_NAME: OracleServicep041
TYPE               : 10  WIN32_OWN_PROCESS
STATE              : 4  RUNNING
(STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE    : 0  (0x0)
SERVICE_EXIT_CODE  : 0  (0x0)
CHECKPOINT         : 0x0
WAIT_HINT          : 0x0
############################################
Listener is Down !!! starting Now
############################################
The OracleOraDb11g_home1TNSListener service is starting.
The OracleOraDb11g_home1TNSListener service was started successfully.

C:\windows\system32>db d041 start
The OracleServiceD041 service is starting…….
The OracleServiceD041 service was started successfully.

SERVICE_NAME: OracleServiced041
TYPE               : 10  WIN32_OWN_PROCESS
STATE              : 4  RUNNING
(STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE    : 0  (0x0)
SERVICE_EXIT_CODE  : 0  (0x0)
CHECKPOINT         : 0x0
WAIT_HINT          : 0x0
############################################
Listener is Already up and Running !!!
############################################
C:\windows\system32>

C:\windows\system32>db.bat d041
No of args Passed are : 1 where as needed : 2
[USAGE]: db.bat arg1 arg2
[USAGE]: db.bat dbname start/stop

C:\windows\system32>db.bat d041 start extra
No of args Passed are : 3 where as needed : 2
[USAGE]: db.bat arg1 arg2
[USAGE]: db.bat dbname start/stop

Other Dos Commands:
======================
(A)

MyFile-DD-MM-YYYY-HH24-MI-SS.log
=============================================
c:\Oracle>echo MyBigFile-%date:~0%-%time:~0,2%-%time:~3,2%-%time:~6,2%.log
MyFile-01-03-2014-21-23-36.log

MyFile-YYYYMMDDHH24MISS.log
=============================================
c:\Oracle>echo MyFile-%date:~6%%date:~3,2%%date:~0,2%%time:~0,2%%time:~3,2%%time:~6,2%.log
MyFile-20140301212504.log

(B)

Compare Files
===============
c:\Oracle>fc MyBigFile3-012050.sav MyBigFile3-012054.sav
Comparing files MyBigFile3-012050.sav and MYBIGFILE3-012054.SAV
FC: no differences encountered

c:\Oracle>echo hello >> MyBigFile3-012054.sav

c:\Oracle>fc MyBigFile3-012050.sav MyBigFile3-012054.sav
Comparing files MyBigFile3-012050.sav and MYBIGFILE3-012054.SAV
***** MyBigFile3-012050.sav
***** MYBIGFILE3-012054.SAV
hello
*****

Advertisements

From → Shell Scripts

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: