[英]Running multiple parallel sqlplus connections for a certain period of time in bash
I have this code that creates 20 parallel sqlplus instances, doing some queries and exits:我有这段代码可以创建 20 个并行 sqlplus 实例,执行一些查询并退出:
#!/bin/sh
for i in $(seq 1 20);
do
echo "CREATE TABLE table_$i (id NUMBER NOT NULL);
select * from table_$i;
! sleep 30
select * from table_$i;
! sleep 30
DROP TABLE table_$i;" | sqlplus system/password &
done
wait
I need to adjust this code if possible so it would run for an hour with the following conditions:如果可能,我需要调整此代码,以便在以下条件下运行一个小时:
Always stay on 20 connections, if one sqlplus instance is closed (Finished it's process) another one should open, i need to maintain a certain amount of connections for X amount of time.始终保持 20 个连接,如果一个 sqlplus 实例关闭(完成它的进程)另一个应该打开,我需要在 X 时间内保持一定数量的连接。
Is there anything i can add to this code that will achieve what i need?有什么我可以添加到这个代码来实现我需要的吗?
For looping during an hour, see https://stackoverflow.com/a/22735757/3220113对于一小时内的循环,请参阅https://stackoverflow.com/a/22735757/3220113
runsql() {
i="$1"
end=$((SECONDS+3600))
SECONDS=0
while (( SECONDS < end )); do
# Do what you want.
echo "CREATE TABLE table_$i (id NUMBER NOT NULL);
select * from table_$i;
! sleep 30
select * from table_$i;
! sleep 30
DROP TABLE table_$i;" | sqlplus system/password
sleep 1 # precaution when sqlplus fails, maybe wrong password
done
}
for i in $(seq 1 20); do
runsql $i &
done
wait
Explanation:解释:
The main loop at the bottom starts the function runsql
20 times in the background.底部的主循环在后台启动函数
runsql
20 次。
The function runsql
could use $1
everywhere, I copy it to i
for code that looks like the original.函数
runsql
可以在任何地方使用$1
,我将它复制到i
以获得看起来像原始代码的代码。
SECONDS is a counter that is changed every second by the shell, so we do not need to call date
. SECONDS 是一个由 shell 每秒更改的计数器,因此我们不需要调用
date
。
3600 is an hour. 3600是一个小时。
Inside (( .. ))
you can do math without $
in front of variables.在
(( .. ))
您可以在变量前不使用$
进行数学运算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.