Analysis of Linux Shell classic instances-Oracle STARTUP script (I)

Source: Internet
Author: User
Tags unix domain socket

This blog is a summary of the previous Linux Shell common skills and advanced skills series blogs. It will explain and explain the script of Oracle database server startup line by line, to help us better learn and understand the common skills and strengths of Shell scripts.
The Oracle STARTUP script is mainly divided into two functional parts. The first part is to initialize various environment variables to confirm the current version of the Oracle server, to further determine the steps for starting the current server and the various Oracle tools that need to be used, the second part is to read the configuration information of the current server based on the previous judgment results, then, use the Shell commands provided by Oracle to start the database.

LOGMSG = "logger-puser. alert-s"
#1. Signal capture. When the script captures the signal SIGHUP (1), SIGINT (2), and SIGQUIT (3), run the exit command to exit the script.
Trap 'exit '1 2 3
#2. If the value of ORACLE_TRACE variable in the Current Shell environment is T, run the set-x command to start the script tracking function.
Case $ ORACLE_TRACE in
T) set-x ;;
Esac
SAVE_PATH =/bin:/usr/bin:/etc :$ {PATH}; export PATH
SAVE_LLP = $ LD_LIBRARY_PATH
#3. $1, the first parameter of the current script. d directory to call the Shell script oracle of the script, you can know that the value of this parameter is the value of the $ ORACLE_HOME environment variable.
ORACLE_HOME_LISTNER = $1
#4. If this value does not exist, an error message is displayed, and the valid usage of the script is displayed.
If [! $ ORACLE_HOME_LISTNER]; then
Echo "ORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listener"
Echo "Usage: $0 ORACLE_HOME"
Else
LOG = $ ORACLE_HOME_LISTNER/listener. log
#5. export the value of the ORACLE_HOME environment variable. Because the export command is used, the value of this variable will be equally valid in the sub-Shell.
Export ORACLE_HOME = $ ORACLE_HOME_LISTNER
#6. determine whether the $ ORACLE_HOME_LISTNER/bin/tnslsnr file has the executable permission. If it is true, use this command to start the Oracle listener. Note that, because there is an & symbol at the end of the command line, this indicates that the command will be executed in the background.
#7. When the listener is started, the standard output is redirected to the file pointed to by the $ LOG variable in append mode, and the standard error output is also executed to the file.
If [-x $ ORACLE_HOME_LISTNER/bin/tnslsnr]; then
Echo "$0: Starting Oracle Net Listener" >>$ LOG 2> & 1
$ ORACLE_HOME_LISTNER/bin/lsnrctl start >$ LOG 2> & 1 &
#8. Obtain the current Oracle Server version by extracting the returned information of lsnrctl version. The returned result of this command is:
# LSNRCTL for Linux: Version 11.2.0.1.0-Production on 14-DEC-2011 17:23:12
#
# Copyright (c) 1991,200 9, Oracle. All rights reserved.
#
# Connecting to (DESCRIPTION = (ADDRESS = (PROTOCOL = IPC) (KEY = EXTPROC )))
# TNSLSNR for Linux: Version 11.2.0.1.0-Production
# TNS for Linux: Version 11.2.0.1.0-Production
# Unix Domain Socket ipc nt Protocol Adaptor for Linux: Version 11.2.0.1.0-Production
# Oracle Bequeath NT Protocol Adapter for Linux: Version 11.2.0.1.0-Production
# TCP/IP NT Protocol Adapter for Linux: Version 11.2.0.1.0-Production ,,
# The command completed successfully
#9. Use the grep command to filter the above results and output only the rows containing "LSNRCTL for". The result is:
# LSNRCTL for Linux: Version 11.2.0.1.0-Production on 14-DEC-2011 17:25:21
#10. Use the cut command to split the preceding results. The separator is the space character specified by the-d option.-f5 indicates the fifth field after splitting. The result is:
#11.2.0.1.0
#11. Use the cut command to perform secondary splitting of the preceding results, but change the separator to dot (.). The obtained field is the first field, that is, 11.
VER10LIST = '$ ORACLE_HOME_LISTNER/bin/lsnrctl version | grep "LSNRCTL for" | cut-d ''-f5 | cut-d'.'-f1'
Export VER10LIST
Else
Echo "Failed to auto-start Oracle Net Listener using $ ORACLE_HOME_LISTNER/bin/tnslsnr"
Fi
Fi

ORATAB =/etc/oratab
#12. I think the purpose of the code here should be to determine whether the/etc/oratab file exists in the form of a file. However, the following code will make the if judgment always true, so it should be changed to if [! -F $ ORATAB]; then. -F is used to determine whether the variable is a common file. If the file does not exist, the script exits directly. The exit value is 1, indicating that the file fails. It should be noted that in Linux, the general rule is to return 0, indicating that the execution is successful.
If [! $ ORATAB]; then
Echo "$ ORATAB not found"
Exit 1;
Fi

#13. checkversionmismatch is a custom function of the script. It is used to determine whether the version of the client tool sqlplus matches the version of the Oracle server.
Checkversionmismatch (){
If [$ VER10LIST]; then
#14. Use sqlplus-V to obtain the version of sqlplus. Then, use the grep command to filter out and output only the rows containing Release. The result is:
# SQL * Plus: Release 11.2.0.1.0 Production
#15. Based on the preceding results, use the two cut commands to split and finally output: 11. The function of cut is provided in the preceding annotations.
VER10INST = 'sqlplus-V | grep "Release" | cut-d'-f3 | cut-d'. '-f1'
#16. if the version of the server ($ VER10LIST) is earlier than the version of sqlplus (VER10INST), an error message is displayed. Here-lt is used to compare numeric variables, indicating that A is less than B.
If [$ VER10LIST-lt $ VER10INST]; then
$ LOGMSG "Listener version $ VER10LIST NOT supported with Database version $ VER10INST"
$ LOGMSG "Restart Oracle Net Listener using an alternate ORACLE_HOME_LISTNER :"
$ LOGMSG "lsnrctl start"
Fi
Fi
}

Startinst (){
Export ORACLE_SID
#17. Place the bin directory of oracle in the path environment variable, which is convenient for subsequent direct calls.
PATH = $ ORACLE_HOME/bin: $ {SAVE_PATH}; export PATH
#18. LD_LIBRARY_PATH indicates the path of the so file. Here, the lib path on which oracle depends is assigned to this variable so that oracle execution programs can find them at startup.
LD_LIBRARY_PATH =$ {ORACLE_HOME}/lib :$ {SAVE_LLP}; export LD_LIBRARY_PATH
#19. The following variables are the server instance initialization files required for oracle startup.
PFILE =$ {ORACLE_HOME}/dbs/init $ {ORACLE_SID}. ora
SPFILE =$ {ORACLE_HOME}/dbs/spfile $ {ORACLE_SID}. ora
SPFILE1 =$ {ORACLE_HOME}/dbs/spfile. ora

Echo ""
Echo "$0: Starting up database \" $ ORACLE_SID \""
Date
Echo ""
Checkversionmismatch

#20. The following code logic is used to identify whether the current server version is V6 or V7, because the subsequent startup logic requires special processing for these two versions.
#21. First, determine whether $ ORACLE_HOME/bin/sqldba exists in the form of a common file. If yes, use the sqldba command to obtain the version information.
VERSION = undef
If [-f $ ORACLE_HOME/bin/sqldba]; then
SQLDBA = sqldba
VERSION = '$ ORACLE_HOME/bin/sqldba command = exit | awk'
/SQL \ * DBA: (Release | Version)/{split ($3, V ,".");
Print V [1]}''
#22. if the VERSION is 6, nothing is required. Otherwise, the value of the VERSION variable is unified to internal.
Case $ VERSION in
"6 ");;
*) VERSION = "internal"
Esac
Else
#23. Judge again whether $ ORACLE_HOME/bin/svrmgrl exists in the form of a common file. If yes, the SQLDBA command will be svrmgrl and the version will be internal. Otherwise, the SQLDBA command will point to sqlplus. It should be noted that both svrmgrl and sqldba above are used to dynamically express them to the compatibility of previous versions. In fact, in our later versions, sqlplus is basically used.
If [-f $ ORACLE_HOME/bin/svrmgrl]; then
SQLDBA = svrmgrl
VERSION = "internal"
Else
SQLDBA = "sqlplus/nolog"
Fi
Fi
#24. When the variable STATUS is 1, it indicates the normal value. Other values indicate that the oracle process has been pulled up.
#25. First, determine whether the $ ORACLE_HOME/dbs/sgadef $ {ORACLE_SID}. dbf and $ ORACLE_HOME/dbs/sgadef $ {ORACLE_SID}. ora files already exist. $ {ORACLE_SID} indicates a variable. shell will replace the actual value of the variable during execution. All of the values here are enclosed in curly brackets $ {ORACLE_SID }, instead of using $ ORACLE_SID directly, the shell script treats $ ORACLE_SID.ora as a variable.
STATUS = 1
If [-f $ ORACLE_HOME/dbs/sgadef $ {ORACLE_SID}. dbf]; then
STATUS = "-1"
Fi
If [-f $ ORACLE_HOME/dbs/sgadef $ {ORACLE_SID}. ora]; then
STATUS = "-1"
Fi
#26. pmon is an oracle process monitoring process and one of the core processes of the oracle server. Here, the ps command is used to output a list of all processes on the current linux server, and then the grep command is used to filter out the list. The-w option indicates full-word match, finally, use a grep command to filter out the previous grep command. Here, the-v indicates the inverse, that is, the line that does not contain grep.
Pmon = 'ps-ef | grep-w "ora_pmon _ $ ORACLE_SID" | grep-v grep'
If ["$ pmon "! = ""]; Then
STATUS = "-1"
$ LOGMSG "Warning :$ {INST} \" $ {ORACLE_SID} \ "already started ."
Fi
#27. Check whether the numeric variable $ STATUS is-1, that is, the process has been started.
If [$ STATUS-eq-1]; then
$ LOGMSG "Warning :$ {INST} \" $ {ORACLE_SID} \ "possibly left running when system went down (system crash ?). "
$ LOGMSG "Action: Policy Database Administrator ."
#28. Since the oracle server instance has been started, we need to use different tools and close syntax to shutdown the started instance based on the oracle version.
Case $ VERSION in
"6") sqldba "command = shutdown abort ";;
"Internal") $ SQLDBA $ args <EOF
Connect internal
Shutdown abort
EOF
;;
*) $ SQLDBA $ args <EOF
Connect/as sysdba
Shutdown abort
Quit
EOF
;;
Esac
#29. $? Is the built-in variable of the shell script. It is used to determine whether the operation to shut down the oracle server instance is successful. 0 indicates that the operation is successful, and other values indicate that the operation fails.
If [$? -Eq 0]; then
STATUS = 1
Else
$ LOGMSG "Error :$ {INST} \" $ {ORACLE_SID} \ "NOT started ."
Fi
Fi
If [$ STATUS-eq 1]; then
#30. determine whether $ SPFILE, $ SPFILE1, or $ PFILE exists.-e indicates whether the variable after it indicates the existence of the file, and-o indicates the time or relationship between the conditions, | in C.
#31. For the basic oracle version, use different oracle tools to start oracle Server instances. Different tools use different syntaxes. here we need to focus on sqlplus.
#32. When the server is started using the oracle tool, here document in the shell is used, so that a batch of commands can be passed to oracle commands such as sqlplus at one time.
If [-e $ SPFILE-o-e $ SPFILE1-o-e $ PFILE]; then
Case $ VERSION in
"6") sqldba command = startup ;;
"Internal") $ SQLDBA <EOF
Connect internal
Startup
EOF
;;
*) $ SQLDBA <EOF
Connect/as sysdba
Startup
Quit
EOF
;;
Esac
#33. Determine whether the startup is successful by judging the return value of the preceding command.
If [$? -Eq 0]; then
Echo ""
Echo "$0 :$ {INST} \" $ {ORACLE_SID} \ "warm started ."
Else
$ LOGMSG ""
$ LOGMSG "Error :$ {INST} \" $ {ORACLE_SID} \ "NOT started ."
Fi
Else
$ LOGMSG ""
$ LOGMSG "No init file found for $ {INST} \" $ {ORACLE_SID }\"."
$ LOGMSG "Error :$ {INST} \" $ {ORACLE_SID} \ "NOT started ."
Fi
Fi
}

#34. functions used to start oracle AMS instances.
Startasminst (){
Export ORACLE_SID
#34. $ LINE value will be given in subsequent calls. The value is derived from the output of the oratab file and the content is: MyOrcl:/opt/oracle/product/OraHome: Y
#35. Here, the awk command is used to extract the second field. The colon (:) is the delimiter between fields, and the second variable ($2) is the oracle Home Directory of the current instance.
ORACLE_HOME = 'echo $ LINE | awk-F: '{print $2 }'-'
Export ORACLE_HOME

#36. Check whether $ ORACLE_HOME/bin/crsctl has the execution permission.
If [! -X $ ORACLE_HOME/bin/crsctl]; then
$ LOGMSG "$ ORACLE_HOME/bin/crsctl not found when attempting to start"
$ LOGMSG "ASM instance $ ORACLE_SID ."
Else
#37. Execute the $ ORACLE_HOME/bin/crsctl command repeatedly until it is successfully executed, or exit the script after 15 failures.
COUNT = 0
$ ORACLE_HOME/bin/crsctl check css
RC = $?
#38. Check whether the crsctl command is successfully executed. If it is not equal to or not, the command fails to be executed.
While ["$ RC "! = "0"]; do
#39. Use the expr command to add the variable value of COUNT to one. Here you can also use the let Command, such as (COUNT = COUNT + 1 )).
COUNT = 'expr $ COUNT + 1'
If [$ COUNT = 15]; then
#15 tries with 20 sec interval => 5 minutes timeout
$ LOGMSG "Timed out waiting to start ASM instance $ ORACLE_SID"
$ LOGMSG "CSS service is NOT available ."
Exit $ COUNT
Fi
$ LOGMSG "Waiting for Oracle CSS service to be available before starting"
$ LOGMSG "ASM instance $ ORACLE_SID. Wait $ COUNT ."
#40. Sleep for 20 seconds between each execution.
Sleep 20
$ ORACLE_HOME/bin/crsctl check css
RC = $?
Done
Fi
#41. After asm is successfully started, call the startinst function to start the instance.
Startinst
}

In this blog, I only explained how to customize functions in the oracle STARTUP script. In the next blog, I will enter the main part of the script, the code of the subject part will depend on the functions provided in this blog.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.