Check database connectivity using Shell script
Use a script like this:
#!/bin/shecho "exit" | sqlplus -L uid/pwd@dbname | grep Connected > /dev/nullif [ $? -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)
#! /bin/sh if echo "exit;" | sqlplus UID/PWD@database-schemaname 2>&1 | grep -q "Connected to"then echo connected OKelse echo connection FAILfi
Not knowing whether the "Connected to" message is put to standard output or standard error, this checks both. "qrep -q" instead of "grep... >/dev/null" assumes Linux.
You can avoid the SQL prompt by doing:
sqlplus uid/pwd@database-schemaname < /dev/null
SqlPlus exits immediately.
Now just grep the output of the above as:
if sqlplus uid/pwd@database-schemaname < /dev/null | grep 'Connected to'; then # have connectivity to Oracleelse # No connectivityfi