gpt4 book ai didi

linux - Bash linux 强制多个实例等待运行 sqlplus 命令完成

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:12 24 4
gpt4 key购买 nike

我有一个允许同时运行多个实例的 bash 脚本。但是,有一个到 Oracle 数据库的 sqlplus 连接来检索值。如果多个 bash 脚本实例同时获取此值,它们可能会检索到相同的值,因为当第一个实例仍在处理时,第二个实例已经在验证“旧数据”。

我只能想到两个选项,即通过 PIDFILE 锁定函数。缺点是如果用户在删除 pid 文件之前中止操作,则会导致问题。另一个是根据声明用 ps -ef | 计算的最大进程来锁定整个脚本。 grep '脚本名' | wc -l。但这会锁定整个脚本,而不仅仅是数据库部分。

那么还有其他选择吗?这个与数据库的 sqlplus 连接是否有可能以某种方式捕获,以便我可以在其中一个实例执行此函数时将多个正在运行的实例置于暂停状态?

最佳答案

您可以在 tmp 文件(本例中为 /tmp/pidfile)中使用唯一值,并在脚本中止或完成时重置它。

更新:这看起来很可靠,请参阅底部的测试。

#!/bin/bash


bashpid="$$" # Records PID of script. Unused so far
pidfile=/tmp/pidfile # pidfile var
pidlimit=2 # pidlimit

cleanup () { # cleanup operations
exit
}

pidreset () { # trap operations (if reqd)
pkill -a -P "$bashpid"
kill -9 0
}

trap "pidreset" SIGINT # trap (if reqd)


countpid () { # This is the true PID count, only called
# Once allowed in the database block
# This includes self instance

ps aux | grep "$(basename $0)" | grep -v grep > $pidfile

}

countloop () { # This counts PID while waiting for a spot
# (-1 decrement since self is included but
# not yet granted access to database block)
echo "(( ("$(ps aux | grep "$(basename $0)" | grep -v grep | wc -l )") - 1))" | bc > $pidfile

}
databasecall () { # The database script calls

touch $pidfile
pidcount="$(cat $pidfile | wc -l)"
until [[ "$pidcount" -le "$pidlimit" ]]; do
echo "too many processes"
countloop
pidcount="$(cat $pidfile | wc -l)"
sleep 5
done

# ================ Start of Database access Code ================
# Go ahead and do database stuff
# Write this script into the pidfile incrementing its count also

countpid
pidcount="$(cat $pidfile | wc -l)"
echo "We're in"
echo "Scriptcount: $pidcount (Including this one)"
sleep 5
cleanup
# return / whatever
# ================ End of Database access Code ================

}



echo "Main script block" # Main script outside of Database block
sleep .1


databasecall # Call database function


cleanup # Call to housekeeping

测试

它工作正常,但在运行多于 pidlimit 时稳定下来,尽管我不确定这是在哪里发生的,或者它是否是我的测试场景。

bash>echo "imrunning & sleep .5 ; imrunning & sleep .5 ; imrunning & sleep .5 ; imrunning & sleep .5 ; imrunning" > /tmp/initrunning ; chmod +x /tmp/initrunningbash>/tmp/./initrunning Main script blockWe're inScriptcount:        1 (Including this one)Main script blockWe're inScriptcount:        2 (Including this one)Main script blocktoo many processesMain script blocktoo many processesMain script blocktoo many processesWe're inScriptcount:        3 (Including this one)We're inScriptcount:        3 (Including this one)We're inScriptcount:        3 (Including this one)bash

关于linux - Bash linux 强制多个实例等待运行 sqlplus 命令完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40418828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com