:::::::::::::::::::::::::: 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 a command. For example, 5dd erases five lines. • vi has two modes: 1) Insert & 2) Command mode. When you’re typing text, you are in Insert mode. Before you can issue any commands or move the cursor, you must enter Command mode by pressing the ESC key. To leave the Command mode and begin inserting text, you must either press i for Insert or a for Append. Insert allows you to enter text before your cursor location, whereas Append 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 Right Up Down To the end of file To line n
h l k j G nG
Entering Text To perform this…
Press…
Insert Append
a
i
Editing Text To perform this…
Press…
Delete one character Delete one line Copy n lines n yy Paste p
x dd
Saving and Exiting To perform this…
Type…
Save a current file :w
Save with a different filename :w filename Save and exit : wq or ZZ Exit without save :q!
Miscellaneous Commands To do this… Type… Display current line number CTRL-G Refresh the screen CTRL-L Read 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 for replacement_string, and do so globally(g).) Execute a Unix command from within the editor Repeat a command . Undo a command u
search_string,
replace it with
:! command
***************************************************************************** * last last username shows the last time the specified user logged in. "pwd" command displays the current directory:
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 the current 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 that’s being added to, use tail –f filename; this will keep displaying the end of the file until you press CTRL-C. The –f option is commonly used to monitor export or import logs, whereas the –n option is often used for alert logs. CAT COMMAND cat filename displays 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 to display the contents one screen 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).
grep grep –i string filename searches the file for occurrences of the string, with the –I making the search case-insensitive. For example, grep –i ORA-00600 alertdemo.log searches the alert log for any ORA-600 errors and displays them to the screen. This can also be used 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" flag makes the comparison case insensitive. A count of the error lines can be returned using the "wc" command. This normally give a word count, but the "-l" flag alteres it to give a line 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 user’s home directory, use cd ~username "touch" command is used to create a new empty file with the default permissions: root> touch my.log
touch filename attempts to create an empty file in your current directory. Use this to determine whether you have write permissions on a directory. For example, after the oradata directory has been 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 / -name filename searches for the file in every directory starting at / and working through each subdirectory. Any location 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 don’t have permissions 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
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 + execute 6 (u+wx) 6 (g+wx) 6 (o+wx) write + execute 5 (u+Rx) 5 (g+Rx) 5 (o+Rx) read + execute 4 (u+r) 4 (g+r) 4 (o+r) read only 2 (u+w) 2 (g+w) 2 (o+w) write only 1 (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
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
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 of connected 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 oracle ora_dbwr_DDDS2 8.5 4793 oracle ora_lgwr_DDDS2 2.4 6206 oracle oracleDDDS2 (LOCAL=NO) 0.1 4797 oracle ora_smon_DDDS2 0.1 6207 oracle oracleDDDS2 (LOCAL=NO) etc. etc. etc. etc. •
The PID column can then be matched with the SPID column on the V$PROCESS view to 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
• • • • • • • • •
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/ksh su - oracle <
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 redo logs. This can be using either the gzip or the compress commands. The gzip command results in a compressed copy of the original file with a ".gz" extension. The gunzip command reverses this process: 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 it using 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-59 hour 0-23 day of month 1-31 month 1-12 day of week 0-7 (both 0 and 7 are Sunday) user Valid OS user command Valid command or script. The first 5 fields can be specified using the following rules: * - All available values or "first-last".
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 cronjob crontab -l ( list current jobs in cron) crontab -e ( edit current jobs in cron ) _1_ _2_ _3_ _4_ _5_ $Job_Name 1 - 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-> monday e.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 not pointing to source. ls -al `find . -type l` | grep $OLD_PATH
SORT How to Sort files based on Size of file in a Directory Useful in finding out spaces issues ls -l | sort -nrk 5 | more PORT How to check if a Port is listening for any Service netstat -an | grep $PORTNO bdf bdf displays all the filesystems and the amount of disk space on an HP-UX server. Use this command to determine what filesystems you have and how much disk space is available on each. Df df displays your filesystems and the amount of space available on each. It is common to use this with the –k option to display results in kilobytes (for example, df –k). du du -s * shows disk usage for every subdirectory below your current location. This is useful for finding directories with large files to be purged. Echo echo “text string” or echo $ENV_VARIABLE echoes the contents of the text string or the value of a variable to the screen. Echoing strings is common in shell scripts. Echoing variables is useful for verifying your environment.
(for example, echo $ORACLE_SID or echo $ORACLE_HOME). fuser fuser filename shows the processes accessing a specified file. Glance glance -m invokes the HP-UX system-monitoring tool. Use this to check system performance on HP servers. groups groups displays all the groups you are a member of. diff diff filename1 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 you’re looking for problems with the server or when you’re trying to find out more about the system. It is 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 link for a file. This makes it seem as if the file appears in one location even though it really exists in another location. For example, ln –s $ORACLE_BASE/admin/demo/pfile/ initdemo.ora $ORACLE_HOME/dbs/initdemo.ora
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 real location. Removing the link will notdelete the physical file. Removing the actual file will leave the link intact, but it will be invalid. mailx mailx [email protected] prepares 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. pine—pine invokes a Unix-based email system. page page filename displays the contents of a file one screen at a time. To keep scrolling one screen at a time, press the spacebar. To advance one line at a time, press Enter. To stop viewing a file, press CTRL-C. sar sar is System Activity Report and can show a variety of operating system statistics. This is frequently used for monitoring the server and it has several options. script script filename.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 the user’s .profile will be executed and new environment variables will be established when you log in. If the hyphen is not specified, the user will inherit the environment of the previous user. There is normally not a good reason to log in as a user without setting up the proper environment, so generally you should use the hyphen. talk
talk username tty requests an online chat session with the user with a specific tty. You can obtain this information using 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 refresh the screen display, use CTRL-L. tar tar cvf -`find . –print ` > filename.tar compresses a directory, its subdirectories, and all the files into one file called filename.tar. This TAR file can be compressed and transported via cp or ftp to a different location. Once it has been moved to its location, the directory, its subdirectories, and all the files can be recreated by issuing tar xvf filename.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 good snapshot of system performance and load. truss truss -p PID traces all the system calls for a given process ID. This command is handy when you have to see exactly 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 number of users on the server, and the load average for the present time, five minutes ago, and 15 minutes ago. wc (word count)
wc –l filename gives a word count of the number of lines in a file. Use it in conjunction with grep to count the number 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