转 How To Stop A Running Job Using DBMS_JOB
There is no procedure within the dbms_job package to stop a running job.
You will need to determine which Oracle session and process is associated with this running job and then kill it.
You can run the following query to see job processes:
select sid, job, instance from dba_jobs_running where job=&job_id;
Find the OS process id for the session id under which job is running :
set linesize 120
set pagesize 120
column spid heading 'OSpid' format a8
column pid heading 'Orapid' format 999999
column sid heading 'Sess id' format 99999
column serial# heading 'Serial#' format 999999
column status heading 'Status' format a8
column pga_alloc_mem heading 'PGA alloc'
column username heading 'oracleuser' format a12
column osuser heading 'OS user' format a12
column program heading 'Program' format a28
SELECT
p.spid,
p.pid,
s.sid,
s.serial#,
s.status,
p.pga_alloc_mem,
s.username,
s.osuser,
s.program
FROM
v$process p,
v$session s
WHERE s.paddr(+) = p.addr
AND s.sid=&session_id
Order by p.pga_alloc_mem desc;
Then issue the following with the appropriate sid and serial# for that process to kill the job:
OSpid Orapid Sess id Serial# Status PGA alloc oracleuser OS user Program
-------- ------- ------- ------- -------- ---------- ------------ ------------ ----------------------------
23528122 76 4192 23693 ACTIVE 248614872 db opcore oracle@pb02 (J002
ALTER SYSTEM KILL SESSION '4192 ,23693 ';
Bringing Down a DBMS_JOB Job
1. Find the job you want to bring down
In order to do anything you first need to find the job that is giving you a headache. Go ahead and run the above query. This will give you the basic information, job, sid, serial#, and spid, for the following actions to bring down the job.
2. Mark the DBMS_JOB job as broken
Use the following command for the job that you have to deal with.
All this command does is mark the job so that if we get it to stop, it won't start again. Let's make one thing perfectly clear, after executing this command the job is still running.
As a side note, if you are trying to shut down a database with jobs that run throughout the day, they may hinder your attempts to bring down the database cleanly. This is a wonderful command to make sure no jobs are executing during the shutdown process. Just be aware that you will need to mark the jobs as unbroken when the database comes back up, more on that later.
3. Kill the Oracle session
Since the job is still running and it isn't going to end soon, you will need to kill the Oracle session that is executing the job. Use the following command for to kill the job.
4. Kill the O/S process
More often than not the previous step will still leave the job attached to the database and still running. When this happens you will need to go out to the operating system level and get rid of the process that has spawned from the running job. In order to do this you must login to the database box and issue the following command, depending on the type of operating system you have.
For Windows, at the command prompt:
For UNIX at the command line:
Note that 'orakill' is an Oracle command, while 'kill' is a Unix command.
5. Remove the BROKEN status of the DBMS_JOB job:
How to disable an entry from DBMS_SCHEDULER?
SOLUTION
Use the DISABLE procedure to disable a job from the scheduler queue.
DBMS_SCHEDULER.DISABLE ( name IN VARCHAR2, force IN BOOLEAN DEFAULT FALSE);
Parameter Description:
name - the name of the object being disabled; can be a comma-delimited list
force - whether to ignore dependencies
Examples:
In this Document
APPLIES TO:Oracle Database - Enterprise Edition - Version 11.2.0.4 and laterInformation in this document applies to any platform. *** checked for relevance on 24-Nov-2015 *** GOALHow to disable database jobs ( DBMS_SCHEDULER , AUTOTASKS and DBMS_JOBS)before upgrade from 11.2.0.3 to 11.2.0.4? SOLUTIONSince you are on a version 11gR2 then by setting the parameter job_queue_processes to 0 all job processes are stopped, SQL> show parameter job_queue_processes
SQL> alter system set job_queue_processes=0 scope=both;
http://docs.oracle.com/cd/E11882_01/server.112/e40402/initparams112.htm#REFRN10077
SQL>exec DBMS_SCHEDULER.DISABLE('');
SQL>exec DBMS_SCHEDULER.ENABLE('');
SQL> EXECUTE DBMS_AUTO_TASK_ADMIN.DISABLE;
SQL> EXECUTE DBMS_AUTO_TASK_ADMIN.ENABLE;
SQL>exec DBMS_AUTO_TASK_ADMIN.DISABLE('<client name>' , null, null);
SQL>exec DBMS_AUTO_TASK_ADMIN.ENABLE('<client name>' , null, null); Where the <client name> can be obtained from the output of select client_name from DBA_AUTOTASK_CLIENT;
SQL> EXEC DBMS_JOB.BROKEN (<job_id> , TRUE,SYSDATE);
|