kill all Oracle processes on Linux


ps -ef|grep “ora_”|grep -v grep|grep $ORACLE_SID|awk ‘{print $2}’|xargs kill -9

To begin, the Unix kill command is used to kill a process. The basic format of the kill command is as follows:
kill -9 PID1 PID2 PID3…PIDn
PID1 through PIDn represent the list of process IDs for the processes that you want to kill. The -9 option directs Unix to kill the processes immediately.

ps -ef
If you execute ps -ef on your server, you’ll see a long list of processe for Oracle and for many other things. However, you want to limit your output to only those processes that are related to the Oracle database. The grep command can be used to do this. Oracle background process names always begin with “ora_”, so piping the output of ps -ef through grep “ora_” will remove all but the Oracle background processes. For example:

>ps -ef|grep “ora_”
oracle 13022 1 0 May 07 … ora_db02_vald
oracle 14796 42726 0 09:00:46 0:00 grep ora_
oracle 17778 1 0 May 07 … ora_smon_devp
oracle 18134 1 0 May 07 … ora_snp1_vald
oracle 19516 1 0 May 07 … ora_db04_prod
oracle 21114 1 0 May 07 … ora_snp0_devp
oracle 28436 1 0 May 07 … ora_arch_prod
oracle 17748 1 0 May 07 … ora_smon_prod
oracle 18134 1 0 May 07 … ora_snp1_prod
oracle 12516 1 0 May 07 … ora_pmon_prod
oracle 21714 1 0 May 07 … ora_reco_prod
oracle 21814 1 0 May 07 … ora_dbwr_prod

One thing you’ll notice about this output is that it includes the process that’s running the grep command. Pipe this output through grep -v grep to remove the grep command, so you don’t kill your own process. The -v option makes grep work in a way that’s opposite its usual manner. Whereas grep finds and includes strings, grep -v excludes strings. In this next example, you’ll see that the grep line is now missing from the output:

>ps -ef|grep “ora_”|grep -v grep
oracle 13022 1 0 May 07 … ora_db02_vald
oracle 17778 1 0 May 07 … ora_smon_devp
oracle 18134 1 0 May 07 … ora_snp1_vald
oracle 19516 1 0 May 07 … ora_db04_prod
oracle 21114 1 0 May 07 … ora_snp0_devp
oracle 28436 1 0 May 07 … ora_arch_prod
oracle 17748 1 0 May 07 … ora_smon_prod
oracle 18134 1 0 May 07 … ora_snp1_prod
oracle 12516 1 0 May 07 … ora_pmon_prod
oracle 21714 1 0 May 07 … ora_reco_prod
oracle 21814 1 0 May 07 … ora_dbwr_prod

Next, you should filter out all processes except those for the current ORACLE_SID. That way you delete the background processes only for that one instance instead of for all instances. Do that by grepping for the SID name:
>ps -ef|grep “ora”|grep -v grep|grep $ORACLE_SID
oracle 17748 1 0 May 07 … ora_smon_prod
oracle 18134 1 0 May 07 … ora_snp1_prod
oracle 12516 1 0 May 07 … ora_pmon_prod
oracle 21714 1 0 May 07 … ora_reco_prod
oracle 21814 1 0 May 07 … ora_dbwr_prod

Now that you have an accurate list of processes that you want to kill, you can use the awk command to get the process ID (PID) for each of these processes. The PID is in the second column, so use the awk ‘{print $2}’ command to display only that column:

>ps -ef|grep “ora_”|grep -v grep|grep $ORACLE_SID|awk ‘{ print $2 }’
17748
18134
12516
21714
21814

Now you have a list of process ID numbers for the Oracle background processes. For the last step, you use the xargs command to pipe this list of PIDs to the kill command. For example:

ps -ef|grep “ora_”|grep -v grep|grep $ORACLE_SID|awk ‘{ print $2 }’|xargs kill -9

Advertisements

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