如何杀掉一个用户下的所有进程并drop掉这个用户

Copy the sample code below into a file named kill_drop_user.sql.
Open SQL*Plus and connect as user SYS to your database
SQL> CONNECT sys/[email protected] AS SYSDBA
Create a user called TEST with password TEST
SQL> GRANT connect, resource TO test IDENTIFIED BY test;
Create the procedure kill_drop_user
SQL> @"C:\scripts\kill_drop_user.sql"
Open three (3) SQL*Plus sessions and connect as user TEST.
Execute the PL/SQL script
SQL> SET serveroutput ON size 1000000 SQL> SET timing ON SQL> EXEC kill_drop_user(‘TEST‘);
CAUTION

This sample code is provided for educational purposes only, and is not supported by Oracle Support. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
SAMPLE CODE

CREATE OR REPLACE PROCEDURE kill_drop_user (in_username IN VARCHAR2,
                                            sleep_interval IN NUMBER DEFAULT 10)
AS
  PRAGMA AUTONOMOUS_TRANSACTION;

cannot_drop_user EXCEPTION;
  PRAGMA EXCEPTION_INIT(cannot_drop_user, -1940);

user_count    NUMBER := -1;

BEGIN

SELECT count(*) INTO user_count FROM dba_users WHERE username = in_username;
  IF user_count = 0 THEN
    DBMS_OUTPUT.PUT_LINE(‘User ‘ || in_username || ‘ does not exist.‘);
    RETURN;
  END IF;

FOR i IN (SELECT sid, serial# FROM v$session WHERE username = in_username) LOOP
    EXECUTE IMMEDIATE ‘alter system kill session ‘ || ‘‘‘‘ || i.sid || ‘,‘ || i.serial# || ‘‘‘ immediate‘;
    DBMS_OUTPUT.PUT_LINE(‘Killing user ‘ || i.sid || ‘, ‘ || i.serial#);
  END LOOP;

LOOP
    BEGIN
      DBMS_OUTPUT.PUT_LINE(‘Attempting to drop user ‘ || in_username || ‘...‘);
      EXECUTE IMMEDIATE ‘DROP USER ‘ || in_username || ‘ CASCADE‘;

EXIT WHEN SQLCODE <> -1940;
    EXCEPTION
      WHEN cannot_drop_user THEN
        --DBMS_OUTPUT.PUT_LINE(SQLERRM);
        DBMS_OUTPUT.PUT_LINE(‘Waiting ‘ || sleep_interval || ‘ seconds for resource clean-up...‘);
        DBMS_LOCK.SLEEP(sleep_interval);
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE(‘Inner: ‘ || SQLERRM);
    END;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE(‘Exiting loop with SQLCODE: ‘ || SQLCODE);
  DBMS_OUTPUT.PUT_LINE(‘User ‘ || in_username || ‘ has been dropped.‘);

EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(‘Outer: ‘ || SQLERRM);
END;
/
SAMPLE OUTPUT

SQL*Plus: Release 10.1.0.4.0 - Production on Tue Sep 6 15:34:47 2005
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> CONNECT sys/[email protected] AS SYSDBA
Connected.
SQL> GRANT connect, resource TO test IDENTIFIED BY test;

Grant succeeded.

SQL> @"C:\scripts\kill_drop_user.sql"
Procedure created.

SQL> SET serveroutput ON size 1000000

SQL> SET timing ON

SQL> EXEC kill_drop_user(‘TEST‘);
Killing user 152, 6915
Killing user 153, 326
Killing user 154, 156
Attempting to drop user TEST...
Waiting 10 seconds for resource clean-up...
Attempting to drop user TEST...
Exiting loop with SQLCODE: 0
User TEST has been dropped.

PL/SQL procedure successfully completed.

Elapsed: 00:00:10.71

SQL> -- verify user has been dropped
SQL> CONNECT test/[email protected]
ERROR:
ORA-01017: invalid username/password; logon denied

Warning: You are no longer connected to ORACLE.
SQL>

转自MOS
如何杀掉一个用户下的所有进程并drop掉这个用户

Copy the sample code below into a file named kill_drop_user.sql.
Open SQL*Plus and connect as user SYS to your database
SQL> CONNECT sys/[email protected] AS SYSDBA
Create a user called TEST with password TEST
SQL> GRANT connect, resource TO test IDENTIFIED BY test;
Create the procedure kill_drop_user
SQL> @"C:\scripts\kill_drop_user.sql"
Open three (3) SQL*Plus sessions and connect as user TEST.
Execute the PL/SQL script
SQL> SET serveroutput ON size 1000000 SQL> SET timing ON SQL> EXEC kill_drop_user(‘TEST‘);
CAUTION

This sample code is provided for educational purposes only, and is not supported by Oracle Support. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
SAMPLE CODE

CREATE OR REPLACE PROCEDURE kill_drop_user (in_username IN VARCHAR2,
                                            sleep_interval IN NUMBER DEFAULT 10)
AS
  PRAGMA AUTONOMOUS_TRANSACTION;

cannot_drop_user EXCEPTION;
  PRAGMA EXCEPTION_INIT(cannot_drop_user, -1940);

user_count    NUMBER := -1;

BEGIN

SELECT count(*) INTO user_count FROM dba_users WHERE username = in_username;
  IF user_count = 0 THEN
    DBMS_OUTPUT.PUT_LINE(‘User ‘ || in_username || ‘ does not exist.‘);
    RETURN;
  END IF;

FOR i IN (SELECT sid, serial# FROM v$session WHERE username = in_username) LOOP
    EXECUTE IMMEDIATE ‘alter system kill session ‘ || ‘‘‘‘ || i.sid || ‘,‘ || i.serial# || ‘‘‘ immediate‘;
    DBMS_OUTPUT.PUT_LINE(‘Killing user ‘ || i.sid || ‘, ‘ || i.serial#);
  END LOOP;

LOOP
    BEGIN
      DBMS_OUTPUT.PUT_LINE(‘Attempting to drop user ‘ || in_username || ‘...‘);
      EXECUTE IMMEDIATE ‘DROP USER ‘ || in_username || ‘ CASCADE‘;

EXIT WHEN SQLCODE <> -1940;
    EXCEPTION
      WHEN cannot_drop_user THEN
        --DBMS_OUTPUT.PUT_LINE(SQLERRM);
        DBMS_OUTPUT.PUT_LINE(‘Waiting ‘ || sleep_interval || ‘ seconds for resource clean-up...‘);
        DBMS_LOCK.SLEEP(sleep_interval);
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE(‘Inner: ‘ || SQLERRM);
    END;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE(‘Exiting loop with SQLCODE: ‘ || SQLCODE);
  DBMS_OUTPUT.PUT_LINE(‘User ‘ || in_username || ‘ has been dropped.‘);

EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(‘Outer: ‘ || SQLERRM);
END;
/
SAMPLE OUTPUT

SQL*Plus: Release 10.1.0.4.0 - Production on Tue Sep 6 15:34:47 2005
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> CONNECT sys/[email protected] AS SYSDBA
Connected.
SQL> GRANT connect, resource TO test IDENTIFIED BY test;

Grant succeeded.

SQL> @"C:\scripts\kill_drop_user.sql"
Procedure created.

SQL> SET serveroutput ON size 1000000

SQL> SET timing ON

SQL> EXEC kill_drop_user(‘TEST‘);
Killing user 152, 6915
Killing user 153, 326
Killing user 154, 156
Attempting to drop user TEST...
Waiting 10 seconds for resource clean-up...
Attempting to drop user TEST...
Exiting loop with SQLCODE: 0
User TEST has been dropped.

PL/SQL procedure successfully completed.

Elapsed: 00:00:10.71

SQL> -- verify user has been dropped
SQL> CONNECT test/[email protected]
ERROR:
ORA-01017: invalid username/password; logon denied

Warning: You are no longer connected to ORACLE.
SQL>

转自MOS

Copy the sample code below into a file named kill_drop_user.sql.
Open SQL*Plus and connect as user SYS to your database
SQL> CONNECT sys/[email protected] AS SYSDBA
Create a user called TEST with password TEST
SQL> GRANT connect, resource TO test IDENTIFIED BY test;
Create the procedure kill_drop_user
SQL> @"C:\scripts\kill_drop_user.sql"
Open three (3) SQL*Plus sessions and connect as user TEST.
Execute the PL/SQL script
SQL> SET serveroutput ON size 1000000 SQL> SET timing ON SQL> EXEC kill_drop_user(‘TEST‘);
CAUTION

This sample code is provided for educational purposes only, and is not supported by Oracle Support. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
SAMPLE CODE

CREATE OR REPLACE PROCEDURE kill_drop_user (in_username IN VARCHAR2,
                                            sleep_interval IN NUMBER DEFAULT 10)
AS
  PRAGMA AUTONOMOUS_TRANSACTION;

cannot_drop_user EXCEPTION;
  PRAGMA EXCEPTION_INIT(cannot_drop_user, -1940);

user_count    NUMBER := -1;

BEGIN

SELECT count(*) INTO user_count FROM dba_users WHERE username = in_username;
  IF user_count = 0 THEN
    DBMS_OUTPUT.PUT_LINE(‘User ‘ || in_username || ‘ does not exist.‘);
    RETURN;
  END IF;

FOR i IN (SELECT sid, serial# FROM v$session WHERE username = in_username) LOOP
    EXECUTE IMMEDIATE ‘alter system kill session ‘ || ‘‘‘‘ || i.sid || ‘,‘ || i.serial# || ‘‘‘ immediate‘;
    DBMS_OUTPUT.PUT_LINE(‘Killing user ‘ || i.sid || ‘, ‘ || i.serial#);
  END LOOP;

LOOP
    BEGIN
      DBMS_OUTPUT.PUT_LINE(‘Attempting to drop user ‘ || in_username || ‘...‘);
      EXECUTE IMMEDIATE ‘DROP USER ‘ || in_username || ‘ CASCADE‘;

EXIT WHEN SQLCODE <> -1940;
    EXCEPTION
      WHEN cannot_drop_user THEN
        --DBMS_OUTPUT.PUT_LINE(SQLERRM);
        DBMS_OUTPUT.PUT_LINE(‘Waiting ‘ || sleep_interval || ‘ seconds for resource clean-up...‘);
        DBMS_LOCK.SLEEP(sleep_interval);
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE(‘Inner: ‘ || SQLERRM);
    END;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE(‘Exiting loop with SQLCODE: ‘ || SQLCODE);
  DBMS_OUTPUT.PUT_LINE(‘User ‘ || in_username || ‘ has been dropped.‘);

EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE(‘Outer: ‘ || SQLERRM);
END;
/
SAMPLE OUTPUT

SQL*Plus: Release 10.1.0.4.0 - Production on Tue Sep 6 15:34:47 2005
Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> CONNECT sys/[email protected] AS SYSDBA
Connected.
SQL> GRANT connect, resource TO test IDENTIFIED BY test;

Grant succeeded.

SQL> @"C:\scripts\kill_drop_user.sql"
Procedure created.

SQL> SET serveroutput ON size 1000000

SQL> SET timing ON

SQL> EXEC kill_drop_user(‘TEST‘);
Killing user 152, 6915
Killing user 153, 326
Killing user 154, 156
Attempting to drop user TEST...
Waiting 10 seconds for resource clean-up...
Attempting to drop user TEST...
Exiting loop with SQLCODE: 0
User TEST has been dropped.

PL/SQL procedure successfully completed.

Elapsed: 00:00:10.71

SQL> -- verify user has been dropped
SQL> CONNECT test/[email protected]
ERROR:
ORA-01017: invalid username/password; logon denied

Warning: You are no longer connected to ORACLE.
SQL>

转自MOS

时间: 2024-08-02 13:52:03

如何杀掉一个用户下的所有进程并drop掉这个用户的相关文章

LINUX下查杀进程,包括干掉远程访问用户的端口进程

查杀进程 以及用户端口代码 netstat -an |more q退出 netstat -anp |more q退出 显示进程号 杀进程 kill -9 进程号 SSH登陆LINUX后会被直接干掉

普通用户下启动Nginx服务

[普通用户下启动nginx服务] 1.创建普通用户 #useradd huazhixu #tail -5 /etc/passwd #echo 123456 | passwd --stdin huazhixu 2.切换进普通用户 #su - huazhixu 创建普通用户启动的nginx配置文件和站点目录等 $mkdir -p /home/huazhixu/conf/ $cp /usr/local/nginx/conf /home/huazhixu/conf  -ap $cp /usr/local

linux下批量kill进程的方法

--kill某个用户下的所有进程(用户为test)--pkill  # pkill -u test--killall  # killall -u test--ps  # ps -ef | grep test | awk '{ print $2 }' | xargs kill -9--pgrep # pgrep -u test | xargs kill -9 --kill某个程序的所有进程(程序为postgresql) --ps # ps -ef|grep postgres|grep -v gre

Linux下Oracle启动、建立表空间、用户、授权、数据库导入导出

1.1进入到sqlplus启动实例 [[email protected] ~]$ su - oracle                                 --“切换到oracle用户”[[email protected] ~]$ lsnrctl start                               --“打开监听”[[email protected] ~]$ sqlplus /nolog                                --“进入到

在Linux下利用crond实现一个定时任务并完成一个守护(精灵)进程

一.利用crond实现一个定时任务       在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep cron].cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.cron的配置文件称为"crontab",是"cron table"的简写. crontab支持两种状态: a.直接编写计划任务: b.使用目录的方式,放在目录里面的都会定时执行,定时目录可在/etc/crontab中设定. 为当前用户创建cron服

spool命令、创建一个表,创建并且copy表,查看别的用户下的表,rowid行地址 索引的时候使用,表的增删改查,删除表,oracle的回收站

  1.spool命令 spool "D:\test.txt" spool off SQL> host cls 2.创建一个表 SQL> --条件(1):有创建表的权限,(2):有表空间 SQL> desc t4; 名称                                      是否为空? 类型 ----------------------------------------- -------- ------------------------

一个用户下表、批量授予权限给另一个用户

工作中经常会用到一个用户对另外一个用户下表的操作,遇到批量的授权或回收权限可以用如下语句执行就可以了,一般是授予 select\update\delete\insert 也可以用 grant all 表示所有 对存储过程的授权为 grant execute on 过程 to 用户 如果加 with grant option 当前被授权的用户则可以再授予给其他用户 以下是示例,实际工作中根据情况进行修改即可 --批量授予与收回 --授予 查询 插入 权限 declare cursor cur_a

ORACLE授权用户查询另一个用户下的表与视图

实际应用中,会遇到在某个用户下需要查询另一个用户下的表数据或视图的情况,然而在没有授权时,会提示无权限操作的错误.那就需要通过授权处理后,再能进行查询操作,下面我们来看看是怎么处理的. 一.系统权限说明: 1.用户权限 CREATE SESSIOIN 连接到数据库 CREATE TABLE    在用户的方案中创建表 CREATE SEQUENCE 在用户的方案中创建序列 CREATE VIEW     在用户的方案中创视图 CREATE PROCEDURE在用户的方案中创建存储过程,函数或包

Oracle 如何删除掉一个用户下的所有对象

create or replace procedure drop_all as cursor cur_obj is select uo.OBJECT_NAME, uo.OBJECT_TYPE from user_objects uo where uo.OBJECT_NAME not in ('DROP_ALL') and uo.OBJECT_TYPE not in ('LOB'); /* cursor cur_tablespace is select ut.TABLESPACE_NAME fro