unix commands for dba

Upload: gogiabrijesh6150

Post on 30-May-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Unix Commands for DBA

    1/17

    :::::::::::::::::::::::::: UNIX COMMANDS FOR DBA ::::::::::::::::::::::::::::::

    vi EDITOR

    *****************************************************************************

    *

    General rules when using vi :

    Do not edit with the CAPS LOCK key on.

    You can precede most commands by a number to indicate the number of times to execute acommand. For example, 5dd erases five lines.

    vi has two modes: 1) Insert & 2) Command mode.

    When youre typing text, you are in Insert mode. Before you can issue any commands or movethe cursor, you must enter Command mode by pressing the ESC key. To leave the Commandmode and begin inserting text, you must either press i for Insert or a for Append. Insert allowsyou to enter text before your cursor location, whereasAppend allows you to enter text after your cursor location.

    To invoke vi to create or edit a particular file, type the following:

    vi filename

    Cursor-Movement Commands

    To move Press

    Left hRight lUp kDown jTo the end of file GTo line n nG

    Entering Text

    To perform this Press

    Insert iAppend a

  • 8/9/2019 Unix Commands for DBA

    2/17

    Editing Text

    To perform this Press

    Delete one character xDelete one line ddCopy n lines n yyPaste p

    Saving and ExitingTo perform this Type

    Save a current file :w Save with a different filename :w filename

    Save and exit : wq or ZZExit without save :q!

    Miscellaneous Commands

    To do this TypeDisplay current line number CTRL-GRefresh the screen CTRL-LRead an outside file into the document :r/path_to_file/filename Search /search_string Global search and replace :1,$s/search_string/replacement_string/g

    (The above reads, start at line 1 (:1

    ), search forsearch_string

    , replace it withreplacement_string, and do so globally(g).)

    Execute a Unix command from within the editor :! command Repeat a command .Undo a command u

    *****************************************************************************

    *

    last

    last username

    shows the last time the specified user logged in.

    "pwd" command

    displays the current directory:

  • 8/9/2019 Unix Commands for DBA

    3/17

    root> pwd

    /u01/app/oracle/product/9.2.0.1.0

    "ls" command

    lists all files and directories in the specified directory. If no location is defined it acts on thecurrent directory:

    root> ls

    root> ls /u01

    root> ls al

    The "-a" flag lists hidden "." files. The "-l" flag lists file details.

    head

    head n filename

    displays the first n lines of a file.

    tail

    tail n filename

    displays the last n lines of a file.

    tail n filename displays the last n lines of a file. To continually see the end of a logfile thatsbeing added to, use tail ffilename; this will keep displaying the end of the file until you pressCTRL-C.

    The f option is commonly used to monitor export or import logs, whereas the n option is oftenused for alert logs.

    CAT COMMAND

    catfilenamedisplays to the screen the contents of an entire file.

    The output of the file will scroll past the screen if the file is large. Pipe this file with more todisplay the contents onescreen at a time(for example, cat long_file.txt | more).

    Also use with /dev/null to erase the contents of log files (for example, cat /dev/null >alertdemo.log).

  • 8/9/2019 Unix Commands for DBA

    4/17

    grep

    grep istring filename

    searches the file for occurrences of the string, with the I making the search case-insensitive. Forexample,

    grep i ORA-00600 alertdemo.log

    searches the alert log for any ORA-600 errors and displays them to the screen. This can also beused with ps -ef to find Oracle background processes

    (for example, ps ef | grep ora).

    Cat command to find Error Lines in Files

    You can return the error lines in a file using:

    root> cat alert_LIN1.log | grep -i ORA-

    The "grep -i ORA-" command limits the output to lines containing "ORA-". The "-i" flagmakes the comparison case insensitive. A count of the error lines can be returned usingthe "wc" command. This normally give a word count, but the "-l" flag alteres it to give aline count:

    root> cat alert_LIN1.log | grep -i ORA- | wc -l

    "cd" command

    is used to change directories:

    root> cd /u01/app/oracle

    Issuing cd without a directory specified takes you to the $HOME directory.

    to move to a users home directory, use cd ~username

    "touch" command

    is used to create a new empty file with the default permissions:

    root> touch my.log

  • 8/9/2019 Unix Commands for DBA

    5/17

    touchfilenameattempts to create an empty file in your current directory. Use this to determinewhether you have write permissions on a directory. For example, after the oradata directory hasbeen created, touch a file there as the oracle user to confirm it has the necessary permissions.

    "rm" command

    is used to delete files and directories:

    root> rm my.log

    root> rm -R /archive

    The "-R" flag tells the command to recurse through subdirectories.

    "find" command

    can be used to find the location of specific files:

    root> find / -name dbmspool.sql

    root> find / -print | grep -i dbmspool.sql

    find / -namefilename

    searches for the file in every directory starting at / and working through each subdirectory. Anylocation can be given instead of /, with the current location (.) being a common option.

    When the command is executed and the search attempts to view a directory you dont havepermissions for, you will receive an error but the search will continue.

    To suppress these error messages add 2>/dev/null to the end of the command asfollows:

    find . -name login.sql 2>/dev/null.

    How to find a "word" or pattern in all files in a directory & subdirectories

    find . -name "*" -exec grep -l {} \; -print

    for example I want to search for word oracle

    find . -name "*" -exec grep -l oracle {} \; -print

    The "/" flag represents the staring directory for the search. Wildcards such as "dbms*" can be

    used for the filename.

    "which" command

  • 8/9/2019 Unix Commands for DBA

    6/17

    can be used to find the location of an executable you are using:

    oracle> which sqlplus

    The "which" command searches your PATH setting for occurences of the specified executable.

    "chmod" command

    is used to alter file permissions after the file has been created:

    Read permission = 4; write permission = 2; and execute permission = 1.

    root> chmod 777 *.log

    Owner Group World Permission========= ========= ========= ======================7 (u+rwx) 7 (g+rwx) 7 (o+rwx) read + write + execute6 (u+wx) 6 (g+wx) 6 (o+wx) write + execute5 (u+Rx) 5 (g+Rx) 5 (o+Rx) read + execute4 (u+r) 4 (g+r) 4 (o+r) read only2 (u+w) 2 (g+w) 2 (o+w) write only1 (u+x) 1 (g+x) 1 (o+x) execute only

    Character eqivalents can be used in the chmod command:

    root> chmod o+rwx *.log

    root> chmod g+r *.log

    root> chmod -Rx *.log

    The "chown" command is used to reset the ownership of files after creation:

    root> chown -R oinstall.dba *

    The "-R" flag causes the command ro recurse through any subdirectories.

    CHGRP

    chgrp newgroup filename

    changes the group for the file specified. For example,

    chgrp dba test.sql

  • 8/9/2019 Unix Commands for DBA

    7/17

    changes the group of test.sql to dba.

    clear

    clear clears the screen of all previous output and moves the prompt to the top of the screen.

    "useradd" command

    is used to add OS users:

    root> useradd -G oinstall -g dba -d /usr/users/my_user -m -s /bin/ksh my_user

    The "-G" flag specifies the primary group. The "-g" flag specifies the secondary group. The "-d" flag specifies the default directory.

    The "-m" flag creates the default directory.

    The "-s" flag specifies the default shell.

    "usermod" command

    is used to modify the user settings after a user has been created: root> usermod -s /bin/csh my_user

    "userdel" command

    is used to delete existing users:

    root> userdel -r my_user

    The "-r" flag removes the default directory.

    "passwd" command

    is used to set, or reset, the users login password:

    root> passwd my_user

    "who" command

    can be used to list all users who have OS connections:

    root> who

    root> who | head -5

    root> who | tail -5

    root> who | grep -i ora

  • 8/9/2019 Unix Commands for DBA

    8/17

    root> who | wc -l

    The "head -5" command restricts the output to the first 5 lines of the who command. The "tail -5" command restricts the output to the last 5 lines of the who command. The "grep -i ora" command restricts the output to lines containing "ora".

    The "wc -l" command returns the number of lines from "who", and hence the number ofconnected users.

    Process Management (PS)

    The "ps" command lists current process information: root> ps

    root> ps -ef | grep -i ora

    The option elf shows additional information. Specific processes can be killed by specifying the process id in the kill command: root> kill -9 12345

    To displays the top 20 CPU users on the system.

    $ ps -e -o pcpu -o pid -o user -o args | sort -k 1 | tail -21r

    %CPU PID USER COMMAND

    78.1 4789 oracleora_dbwr_DDDS2

    8.5 4793 oracleora_lgwr_DDDS2

    2.4 6206 oracleoracleDDDS2 (LOCAL=NO)

    0.1 4797 oracleora_smon_DDDS2

    0.1 6207 oracleoracleDDDS2 (LOCAL=NO)

    etc. etc. etc. etc.

    The PID column can then be matched with the SPID column on the V$PROCESS viewto provide more information on the process:

    SELECT a.username,

    a.osuser,

    a.program,

    spid,

    sid,

    a.serial#

    FROM v$session a, v$process b

    WHERE a.paddr = b.addr

    AND spid = '&pid';

    uname and hostname

  • 8/9/2019 Unix Commands for DBA

    9/17

    The "uname" and "hostname" commands can be used to get information about the host: root> uname -a

    OSF1 oradb01.lynx.co.uk V5.1 2650 alpha

    root> uname -a | awk '{ print $2 }'

    oradb01.lynx.co.uk

    root> hostname

    oradb01.lynx.co.uk

    File Exists Check

    The Korn shell allows you to check for the presence of a file using the "test -s" command.In the following script a backup log is renamed and moved if it is present:

    #!/bin/ksh

    if test -s /backup/daily_backup.log

    then

    DATE_SUFFIX=`date +"%y""%m""%d""%H""%M"`

    mv /backup/daily_backup.log /backup/archive/daily_backup$DATE_SUFFIX.log

    fi

    FIND OLD FILES

    find /backup/logs/ -name FILENAME* -mtime +

    find /backup/logs/ -name daily_backup* -mtime +21 exec ls ltr {} ;

    Remove Old Files

    The find command can be used to supply a list of files to the rm command:

    find /backup/logs/ -name daily_backup* -mtime +21 -exec rm -f {} ;

    Run Commands As Oracle User From Root

    The following scripts shows how a number of commands can be run as the "oracle" user the"root" user:

    #!/bin/kshsu - oracle

  • 8/9/2019 Unix Commands for DBA

    10/17

    EOF

    This is often necessary where CRON jobs are run from the root user rather than the oracle user

    Compress Files

    In order to save space on the filesystem you may wish to compress files such as archived redologs. This can be using either the gzip or the compress commands. The gzip command results ina compressed copy of the original file with a ".gz" extension. The gunzip command reverses thisprocess:

    gzip myfile

    gunzip myfile.gz

    The compress command results in a compressed copy of the original file with a ".Z" extension.The uncompress command reverses this process:

    compress myfile

    uncompress myfile

    CRON

    There are two methods of editing the crontab file. First you can use the "crontab -l > filename"option to list the contents and pipe this to a file. Once you've editied the file you can then apply itusing the "crontab filename":

    Login as root

    crontab -l > newcron Edit newcron file.

    crontab newcron

    Alternatively you can use the "crontab -e" option to edit the crontab file directly.

    The entries have the following elements:field allowed values----- --------------minute 0-59hour 0-23

    day of month 1-31month 1-12day of week 0-7 (both 0 and 7 are Sunday)user Valid OS usercommand Valid command or script.

    The first 5 fields can be specified using the following rules:* - All available values or "first-last".

  • 8/9/2019 Unix Commands for DBA

    11/17

    3-4 - A single range representing each possible from the start to the end of the range inclusive.1,2,5,6 - A specific list of values.1-3,5-8 - A specific list of ranges.0-23/2 - Every other value in the specified range.The following entry runs a cleanup script a 01:00 each Sunday. Any output or errors from the

    script are piped to /dev/null to prevent a buildup of mails to root:0 1 * * 0 /u01/app/oracle/dba/weekly_cleanup > /dev/null 2>&1

    How to schedule a Job in Unix :

    Use cronjobcrontab -l ( list current jobs in cron)crontab -e ( edit current jobs in cron )_1_ _2_ _3_ _4_ _5_ $Job_Name1 - Minutes (0-59)

    2 - Hours ( 0-24)3 - day of month ( 1- 31 )4 - Month ( 1-12)5 - A day of week ( 0- 6 ) 0 -> sunday 1-> mondaye.g. 0 0 1 * 5 Means run job at Midnight on 1st of month & every friday

    Useful Files

    Here are some files that may be of use:

    Path Contents

    /etc/passwd User settings

    /etc/group Group settings for users.

    /etc/hosts Hostname lookup information.

    /etc/system Kernel parameters for Solaris.

    /etc/sysconfigtab Kernel parameters for Tru64.

    symbolic links

    How to find the symbolic links that point to the old path in your oracle_home and

    appl_top.

    This command is useful in cloning after restore from source to target that symbolic link are notpointing to source.

    ls -al `find . -type l` | grep $OLD_PATH

  • 8/9/2019 Unix Commands for DBA

    12/17

  • 8/9/2019 Unix Commands for DBA

    13/17

    (for example, echo $ORACLE_SID or echo $ORACLE_HOME).

    fuser

    fuserfilename

    shows the processes accessing a specified file.

    Glance

    glance -m

    invokes the HP-UX system-monitoring tool. Use this to check system performance on HPservers.

    groups

    groups

    displays all the groups you are a member of.

    diff

    difffilename1 filename2

    compares two filenames and displays the differences between the two.

    dmesg

    dmesg

    shows all the messages generated from the operating system boot. This is useful when yourelooking for problems with the server or when youre trying to find out more about the system. Itis common to pipe this command through more (for example, dmesg | more).

    ln (linking)

    ln s /location_of_actual_file/filename /where_file_should_appear

    creates asoft linkfor a file. This makes it seem as if the file appears in one location even thoughit reallyexists in another location. For example,

    ln s $ORACLE_BASE/admin/demo/pfile/ initdemo.ora

    $ORACLE_HOME/dbs/initdemo.ora

  • 8/9/2019 Unix Commands for DBA

    14/17

    creates a link so the initdemo.ora file appears to exist in the $ORACLE_HOME/dbs directory.The s makes this a soft link so that a ls l will denote the file is a link and provide its reallocation. Removing the link will notdelete the physical file. Removing the actual file will leavethe link intact, but it will be invalid.

    mailx

    [email protected] to send an email to the user at the email address specified (for example,joe@acme_test.com). Next, you will be prompted for a subject and then need to press Enter.Next you write your message. To send the message, press CTRLD.pinepine invokes a Unix-based email system.

    page

    pagefilename

    displays the contents of a file one screen at a time. To keep scrollingone screen at a time, press the spacebar. To advance one line at a time, press Enter. To stopviewing a file, press CTRL-C.

    sar

    saris System Activity Report and can show a variety of operating system statistics. This isfrequently used for monitoring the server and it has several options.

    script

    scriptfilename.txt

    creates a log of everything that is displayed to the screen until you press CTRL-D.This is similar to spooling output in SQL*Plus. This is good to run when applying patches.

    su

    su username

    prompts you for a password to log in as the specified user. The hyphen indicates that theusers .profile will be executed and new environment variables will be established when you login. If the hyphen is not specified, the user will inherit the environment of the previous user. Thereis normally not a good reason to log in as a user without setting up the proper environment, sogenerally you should use the hyphen.

    talk

    mailto:[email protected]:[email protected]:[email protected]
  • 8/9/2019 Unix Commands for DBA

    15/17

    talkusername tty

    requests an online chat session with the user with a specific tty. You can obtain this informationusing the who command. This is a handy means of communicating with another user logged in

    to the same server. The communication will continue until you quit with a CTRL-C. To refreshthe screen display, use CTRL-L.

    tar

    tar cvf -`find . print ` >filename.tar

    compresses a directory, its subdirectories, and all the files into one file calledfilename.tar. ThisTAR file can be compressed and transported via cp or ftp to a different location. Once it has beenmoved to its location, thedirectory, its subdirectories, and all the files can be recreated by issuing

    tar xvffilename.tar

    in its new location. This is a common way to move directory trees from one server to another.Oracle patches also are often in TAR files.

    top

    top

    invokes a Unix server monitoring utility. This is a handy utility to have as it gives a goodsnapshot of system performance and load.

    truss

    truss -pPID

    traces all the system calls for a given process ID. This command is handy when you have to seeexactly what a process is doing at the Unix level.

    uptime

    uptime

    displays the current time, how long the server has been running since the last reboot, the numberof users on the server, and the load average for the present time, five minutes ago, and 15minutes ago.

    wc (word count)

  • 8/9/2019 Unix Commands for DBA

    16/17

    wc lfilename

    gives a word count of the number of lines in a file. Use it in conjunction with grep to count thenumber of lines found. For example, to see the number of Oracle errors in your alert log, issue

    this command

    grep -i ORA- | wc l

    This will show you the number of lines with ORA- in them.

    which

    which command

    determines which executable you are going to use when you issuecommand. For example,

    which sqlplus

    might show you

    /u01/app/oracle/product/8.1.6/bin/sqlplus.

    whereis

  • 8/9/2019 Unix Commands for DBA

    17/17