简体   繁体   中英

Creating short and long sqlplus connections with bash scripts for a X amount of time

I need to create a short/long connections to oracle db using sqlplus client with a bash script for 10 minutes (for the sake of example), the difference between the two is the following:

Long connection:

  • A connection established
  • Every 5 - 10 seconds the connection is executing a query of some kind
  • At the end of the 10 minutes, the client is terminated

Short connection:

  • A connection established
  • Doing a query
  • Connection is closed
  • repeat the procedure for 10 minutes (connections open and close, maintain a 10 connections all the time)

I have a baseline, i need to adjust it for the above mentioned scenarios (2 different bash files):

#!/bin/sh
for i in $(seq 1 10);
do
   echo "CREATE TABLE oracle_BEQ_$i (id NUMBER NOT NULL);
   ! sleep 30
   select * from oracle_BEQ_$i ;
   ! sleep 30
   DROP TABLE oracle_BEQ_$i;" | sqlplus <user>/<password> &
done
wait

This script currently only does the following:

1) Create 10 connections (simultaneously)

2) Run 3 queries

3) When finished, connections are closed

What are the adjustments i need to make for the 2 scenarios i mentioned?

Example 1. You can run script pipe.sh in the background. And in another session, send sql files or sql. With reconnect after run sql.

more pipe.sh

#!/bin/bash

rm /tmp/sqlplus_pipe.sql
mknod /tmp/sqlplus_pipe.sql p

while :
do

$ORACLE_HOME/bin/sqlplus   "system/manager" <<EOF
@/tmp/sqlplus_pipe.sql
EOF

sleep 1
done

run this script in backgroud

   nohup ./pipe.sh  >pipe_log.log 2>&1  & 

In other bash session you can send sql file or sql to this backgroud process.

oracle@esmd:/tmp> cat test2.sql >>/tmp/sqlplus_pipe.sql
oracle@esmd:/tmp> cat test2.sql >>/tmp/sqlplus_pipe.sql

oracle@esmd:/tmp> echo "select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS') from dual;" >>/tmp/sqlplus_pipe.sql
oracle@esmd:/tmp> echo "select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS') from dual;" >>/tmp/sqlplus_pipe.sql


oracle@esmd:/tmp> more test2.sql
select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS') from dual;



oracle@esmd:~> more pipe_log.log

SQL*Plus: Release 11.2.0.3.0 Production on Thu Aug 8 14:50:35 2019

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL>
TO_CHAR(SYSDATE,'DD
-------------------
08-08-2019 14:50:46

SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL*Plus: Release 11.2.0.3.0 Production on Thu Aug 8 14:50:47 2019

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL>
TO_CHAR(SYSDATE,'DD
-------------------
08-08-2019 14:50:48

SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL*Plus: Release 11.2.0.3.0 Production on Thu Aug 8 14:50:49 2019

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL>
TO_CHAR(SYSDATE,'DD
-------------------
08-08-2019 14:50:49

SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

Update. Example 2. You can run script pipev2.sh in the background on the Oracle database server. And in another session, send sql files or sql. Without reconnect after run sql.

 nohup ./pipev2.sh >output.log 2>&1 &

#!/bin/bash

rm /home/trs/db2Toora/sql/sqlplus_pipe.sql
mknod /home/trs/db2Toora/sql/sqlplus_pipe.sql p


$ORACLE_HOME/bin/sqlplus   "system/manager" <<EOF
SET SERVEROUTPUT ON 
    BEGIN
      RUN_SQL;
    END;
/
EOF


CREATE OR REPLACE DIRECTORY TEMP_DIR_CHANGE AS '/home/trs/db2Toora/sql'
/
GRANT READ ON DIRECTORY TEMP_DIR_CHANGE TO SYSTEM
/
GRANT WRITE ON DIRECTORY TEMP_DIR_CHANGE TO SYSTEM
/


    CREATE OR REPALCE PROCEDURE RUN_SQL

is
    sql_text VARCHAR2(2000);
    file_sql_name VARCHAR2(100):='sqlplus_pipe.sql';
    sql_delimiter VARCHAR2(1):=';';
    stop_script VARCHAR2(10):='%QUIT%';
    sql_output VARCHAR2(2000);
    InFile           utl_file.file_type;
    vNewLine         VARCHAR2(4000);
    k  pls_integer :=0;
    BEGIN
    dbms_output.enable;
    while k <>1
    loop
    InFile := utl_file.fopen('TEMP_DIR_CHANGE', file_sql_name,'r');

    LOOP
      BEGIN

      utl_file.get_line(InFile, vNewLine);

      if vNewLine like '%'||sql_delimiter||'%' then
         sql_text:=sql_text||vNewLine;
      dbms_output.put_line(sql_text);
       begin

        execute immediate replace(sql_text,sql_delimiter,'' ) into sql_output;

        EXCEPTION
        WHEN OTHERS THEN
        dbms_output.put_line('!---!--Error--!---!');
        dbms_output.put_line(substr(sqlerrm, 1, 500));
       end;
      dbms_output.put_line(sql_output);
      dbms_output.put_line('---------------------------------------------------------------');
      sql_text:='';
      elsif vNewLine like  stop_script then
      dbms_output.put_line('---!--QUIT--!---');
      EXIT;
      else
      sql_text:=sql_text||vNewLine;
      end if;

      EXCEPTION
        WHEN NO_DATA_FOUND THEN
        EXIT;
      END;


    END LOOP;
      if vNewLine like stop_script then
       exit;
      end if;

end loop;

    utl_file.fclose(InFile);

    END;

Test

oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> cat test2.sql >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql> echo 'QUIT'  >sqlplus_pipe.sql
oracle@esmd:/home/trs/db2Toora/sql>


oracle@esmd:/home/trs/db2Toora/sql> more test2.sql
select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS')
from dual
;
select to_char(sysdate,'DD-MM-YYYY HH24:MI')
from dual
;


nohup: ignoring input

SQL*Plus: Release 11.2.0.3.0 Production on Tue Aug 13 10:11:23 2019

Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

SQL> SQL>   2    3    4  select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS')  from dual;
13-08-2019 10:11:34
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI')  from dual;
13-08-2019 10:11
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS')  from dual;
13-08-2019 10:11:35
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI')  from dual;
13-08-2019 10:11
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS')  from dual;
13-08-2019 10:11:35
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI')  from dual;
13-08-2019 10:11
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS')  from dual;
13-08-2019 10:11:36
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI')  from dual;
13-08-2019 10:11
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS')  from dual;
13-08-2019 10:11:36
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI')  from dual;
13-08-2019 10:11
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI:SS')  from dual;
13-08-2019 10:11:37
---------------------------------------------------------------
select to_char(sysdate,'DD-MM-YYYY HH24:MI')  from dual;
13-08-2019 12:19
---------------------------------------------------------------
---!--QUIT--!---

PL/SQL procedure successfully completed.

SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM