Run query every 5 minutes

All we need is an easy explanation of the problem, so here it is.

Please find the below query:

select count(*) from v$session;

This has to be run on the database every 5 minutes. What is the right way to achieve this?

The query has to be run every 5 minutes on the same sql*plus screen. Instead of pressing "/" every minute, I wanted to understand if there is a way.

Database/Server details:

SQL> select * from v$version;
Oracle8i Enterprise Edition Release - 64bit Production

SQL> !uname -a
HP-UX ap12121 B.11.11 U 9000/800 ap12121 unlimited-user license

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I don’t know of a way to do what you’re asking from within sqlplus, but you can combine a shell script with the "watch" command to achieve something close to what you’re looking for:

Create a simple shell script ( to call sqlplus and run the query:

sqlplus username/password << EOF
select count(*) from v$session;

Then use the watch command (or HP-UX equivalent, assuming there is one) to execute the script:

watch -n 300

Or use cron to schedule the script to run every 5 minutes and direct the output to a log file:

0/5 * * * * >> sessions.log

then tail the log file:

tail -f sessions.log

Method 2

How about using a PL/Sql block:

    v_sessions NUMBER;
    WHILE true LOOP
        SELECT COUNT (*) INTO v_sessions FROM v$session;
        DBMS_OUTPUT.PUT_LINE ('Sessions: ' || v_sessions);
        DBMS_SESSION.SLEEP (300); -- 5 minutes

If it really is 8i, use DBMS_LOCK.SLEEP (300) instead.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply