使用Shell脚本检查数据库连接 [英] Check database connectivity using Shell script

查看:792
本文介绍了使用Shell脚本检查数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个shell脚本来检查数据库连接。在我的脚本中,我使用命令

  sqlplus uid / pwd @ database-schemaname 



连接到我的Oracle数据库。



现在我想保存这个命令(在它下降到SQL提示)在一个临时文件,然后grep /找到字符串连接到从该文件,以查看连接是否正常。



任何人都可以帮助我捕获输出并退出该提示,并测试连接是否正常。

解决方案

使用如下脚本:

 #!/ bin / sh 
echoexit| sqlplus -L uid / pwd @ dbschemaname | grep Connected> / dev / null
if [$? -eq 0]
then
echoOK
else
echoNOT OK
fi

echoexit确保您的程序立即退出(这将被管道传送到sqlplus)。
-L确保sqlplus不会要求密码,如果凭据不正确(这将使它卡住了)。



(> / dev / null只是隐藏grep的输出,我们不需要,因为在这种情况下通过$?访问结果)


I am trying to write a shell script to check database connectivity. Within my script I am using the command

sqlplus uid/pwd@database-schemaname

to connect to my Oracle database.

Now I want to save the output generated by this command (before it drops to SQL prompt) in a temp file and then grep / find the string "Connected to" from that file to see if the connectivity is fine or not.

Can anyone please help me to catch the output and get out of that prompt and test whether connectivity is fine?

解决方案

Use a script like this:

#!/bin/sh
echo "exit" | sqlplus -L uid/pwd@dbschemaname | grep Connected > /dev/null
if [ $? -eq 0 ] 
then
   echo "OK"
else
   echo "NOT OK"
fi

echo "exit" assures that your program exits immediately (this gets piped to sqlplus). -L assures that sqlplus won't ask for password if credentials are not ok (which would make it get stuck as well).

(> /dev/null just hides output from grep, which we don't need because the results are accessed via $? in this case)

这篇关于使用Shell脚本检查数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆