Monday, October 12, 2009

UNIX Commands for DBAs.

Hi,

Below I'm going to show some good commands for DBAs under Unix environment.

Basic File Navigation
The "pwd" command displays the current directory:

$> pwd
/u01/app/oracle/product/9.2.0.1.0

The "ls" command lists all files and directories in the specified directory. If no location is defined it acts on the current directory:

$> ls
$> ls /u01
$> ls -al

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

The "cd" command is used to change directories:
$> cd /u01/app/oracle

The "touch" command is used to create a new empty file with the default permissions:
$> touch my.log

The "rm" command is used to delete files and directories:
$> rm my.log
$> rm -R /archive

The "-R" flag tells the command to recurse through subdirectories.
The "mv" command is used to move or rename files and directories:
$> mv (from) (to)
$> mv my.log my1.log
$> mv * /archive
$> mv /archive/* .

The "." represents the current directory
The "cp" command is used to copy files and directories:
$> cp [from] [to]
$> cp my.log my1.log
$> cp * /archive
$> cp /archive/* .

The "mkdir" command is used to create new directories:
$> mkdir archive

The "rmdir" command is used to delete directories:
$> rmdir archive

The "find" command can be used to find the location of specific files:
$> find / -name dbmspool.sql
$> find / -print | grep -i dbmspool.sql

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

The "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.

File Permissions

The "umask" command can be used to read or set default file permissions for the current user:
$> umask 022

The umask value is subtracted from the default permissions (666) to give the final permission:
666 : Default permission
022 : - umask value
644 : final permission

The "chmod" command is used to alter file permissions after the file has been created:
$> chmod 777 *.log

Character eqivalents can be used in the chmod command:
$> chmod o+rwx *.log
$> chmod g+r *.log
$> chmod -Rx *.log

The "chown" command is used to reset the ownership of files after creation:
$> chown -R oinstall.dba *

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

OS Users Management
The "useradd" command is used to add OS users:
$> 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.

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

The "userdel" command is used to delete existing users:
$> userdel -r my_user

The "-r" flag removes the default directory.
The "passwd" command is used to set, or reset, the users login password:
$> passwd my_user

The "who" command can be used to list all users who have OS connections:
$> who
$> who | head -5
$> who | tail -5
$> who | grep -i ora
$> 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
The "ps" command lists current process information:
$> ps
$> ps -ef | grep -i ora

Specific processes can be killed by specifying the process id in the kill command:
$> kill -9 12345

Uname and Hostname
The "uname" and "hostname" commands can be used to get information about the host:
$> uname -a
Linux vmlinux3.example.com 2.6.9-42.0.0.0.1.ELsmp #1 SMP Sun Oct 15 14:02:40 PDT 2006 i686 i686 i386 GNU/Linux

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

vmlinux3.example.com

$> hostname
vmlinux3.example.com

Error Lines in Files
You can return the error lines in a file using:

$> cat alert_ORATEST10g.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:

$> cat alert_ORATEST10g.log | grep -i ORA- | wc -l

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

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 {} ;

Remove DOS CR/LFs (^M)
Remove DOS style CR/LF characters (^M) from UNIX files using:

sed -e 's/^M$//' filename > tempfile

The newly created tempfile should have the ^M character removed.

Run Commands As Oracle User From $
The following scripts shows how a number of commands can be run as the "oracle" user the "$" user:

#!/bin/ksh
su - oracle <
ORACLE_SID=LIN1; export ORACLE_SID
rman catalog=rman/rman@w2k1 target=/ cmdfile=my_cmdfile log=my_logfile append
EOF

This is often necessary where CRON jobs are run from the $ 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

General Performance
$ vmstat 5 3
$ sar -u 10 8
$ mpstat 10 2

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

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';

Hope it helps!

Regards,
Marcello Travassos.

No comments:

Post a Comment