issue with environment...login to sqlplus - linux

So i am trying to figure out a simple issue which i know is related to my environment setting....as you can see the oracle database (mcq) is up and running and when i login to the server everything is all set and i can login to the database with SQLPLUS.... But my issue comes in when i switch to bash mode by typing bash(default is csh, when things work)....when i switch to bash and unset everything and set ORACLE_SID, HOME, PATH I get into SQLPLUS but as you can see i get "Connected to an idle instance." message, which means i am not connecting into the database....
the reason why i am unsetting everything and wanting this to work is because of the script i am trying to write. I do not want to relay on the things that have already been set, as on some servers we have the env set for oracle and on some we dont...so if i get this to work this will work on both env...
hostname:oramcq 51> ps -ef|grep pmon
oramcq 3810 1 0 Aug14 ? 00:08:29 ora_pmon_mcq
oramcq 121914 121660 0 13:55 pts/0 00:00:00 grep pmon
hostname:oramcq 52>
hostname:oramcq 52> env | grep ORA
dbms_type=ORA
ORACLE_SID=mcq
ORACLE_BASE=/oracle
ORACLE_HOME=/oracle/mcq/112_64
hostname:oramcq 53>
hostname:oramcq 53> env | grep PATH
PATH=/oracle/mcq/112_64/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/sap/mcq/SYS/exe/uc/linuxx86_64:/usr/sap/mcq/SYS/exe/run:/oracle/mcq/11204:.
MODULEPATH=/usr/share/Modules/modulefiles:/etc/modulefiles
RSEC_SSFS_DATAPATH=/usr/sap/mcq/SYS/global/security/rsecssfs/data
RSEC_SSFS_KEYPATH=/usr/sap/mcq/SYS/global/security/rsecssfs/key
LD_LIBRARY_PATH=/usr/sap/mcq/SYS/exe/run:/usr/sap/mcq/SYS/exe/uc/linuxx86_64:/oracle/mcq/112_64/lib
hostname:oramcq 54>
hostname:oramcq 54> sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu Oct 22 13:56:11 2015
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning and Real Application Testing options
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning and Real Application Testing options
hostname:oramcq 55>
hostname:oramcq 55>
hostname:oramcq 55> bash
bash-4.1$
bash-4.1$ ORACLE_SID=""
bash-4.1$ ORACLE_HOME=""
bash-4.1$ PATH=/usr/sbin:/sbin:/bin:/sbin:/usr/bin
bash-4.1$ export ORACLE_SID=mcq
bash-4.1$ export ORACLE_HOME=/oracle/mcq/11204
bash-4.1$ export PATH=$ORACLE_HOME/bin:$PATH
bash-4.1$ which sqlplus
/oracle/mcq/11204/bin/sqlplus
bash-4.1$
bash-4.1$ env | grep ORA
dbms_type=ORA
ORACLE_SID=mcq
ORACLE_BASE=/oracle
ORACLE_HOME=/oracle/mcq/11204
bash-4.1$
bash-4.1$ env | grep PATH
RSEC_SSFS_DATAPATH=/usr/sap/mcq/SYS/global/security/rsecssfs/data
LD_LIBRARY_PATH=/usr/sap/mcq/SYS/exe/run:/usr/sap/mcq/SYS/exe/uc/linuxx86_64:/oracle/mcq/112_64/lib
PATH=/oracle/mcq/11204/bin:/usr/sbin:/sbin:/bin:/sbin:/usr/bin
MODULEPATH=/usr/share/Modules/modulefiles:/etc/modulefiles
RSEC_SSFS_KEYPATH=/usr/sap/mcq/SYS/global/security/rsecssfs/key
bash-4.1$
bash-4.1$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu Oct 22 13:57:20 2015
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to an idle instance.
SQL>

Related

Not to display SQLPLUS prompt

I am trying to connect with sqlplus using a bash script. When I execute the script below SQLPLUS banner displayed.
Below the script:
$ORACLE_HOME/bin/sqlplus "/ as sysdba" <<EOF
set echo off
set heading off
spool bind.txt
select * from DBMS_LOCK_ALLOCATED where name = '$uservar';
spool off
exit
EOF
Output of the script
oracle#DMOTA01:~/script> ./before_bind.sh
SQL*Plus: Release 11.2.0.3.0 Production on Wed Nov 27 11:54:01 2019
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> SQL> 2 3 4 Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
oracle#DMOTA01:~/script>
I don't want to display "SQL*Plus: Release 11............. Real Application Testing options" lines. How i can do it?
You need to add -S to sqlplus to switch it in silence mode:
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" <<EOF
set echo off
set heading off
spool bind.txt
select * from DBMS_LOCK_ALLOCATED where name = '$uservar';
spool off
exit
EOF

Running Linux Functions inside Sqlplus

Can I call user-created shell functions from inside Oracle SQLPLUS using the HOST command? If not, what's the best way to approach the problem?
Essentially, I want to run a shell file:
Shell commands
sqlplus
#file.sql
HOST mylinuxfunction...
#file2.sql
HOST anotherlinuxfunction..
exit
Shell commands
Thank you!
You can surely invoke HOST commands from SQLPlus scripts, but I imagine you are really asking whether you can use the return values from your linux functions in the rest of your SQLPlus scripts. And you also might be wanting to use results from the SQL queries in your linux functions.
If you do not need to pass SQL information to your linux functions, and do not need to access the results from the linux functions in the remainder of your SQL, then what you have will almost work. This would
date
sqlplus / << xxENDxx \
#file.sql
HOST mylinuxfunction...
#file2.sql
HOST anotherlinuxfunction..
exit
xxENDxx
date
Now if you want to get information from your linux function into SQL, you will have to use external tables; a lot of setup, but look here: https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:439619916584 and search for "but here is another interesting approach, available in 10.2.0.5 and up:"
If you want to pass information from Linux commands that are done before invoking SQLPlus into your SQL commands, that would be something like this, that inserts a row into the uptimes table with output from the uptime command that is stored in the BASH variable $UPTIMES:
#!/bin/bash
if [ "$1" = "" ]
then
echo Missing User ID parm
exit 1
else
USER_ID=$1
fi
read -p "Enter Your password for Oracle instance $ORACLE_SID for user $USER_ID: " PW
UPTIMES=`uptime | awk -F, '{print $3, $4, $5}' | awk '{printf "%2.2f,%2.2f,%2.2f\n", $3, $4, $5}'`
sqlplus /nolog << xxENDxx \
connect $USER_ID/$PW
insert into uptimes (date_stamp, one_min, five_min, fifteen_min) values (sysdate, $UPTIMES);
HOST ls -o uptimes.sh
--#file2.sql
select * from uptimes;
HOST du -sh .
exit
xxENDxx
date
Invoking the above gives this:
oracle. (/home/oracle/sql)
Linux> ./uptimes.sh mark.stewart
Enter Your password for Oracle instance ecs03 for user mark.stewart: xxxx
SQL*Plus: Release 12.1.0.2.0 Production on Thu Mar 17 20:09:36 2016
Dev:#> Connected.
Dev:MARK.STEWART#ecs03> Dev:MARK.STEWART#ecs03>
1 row created.
Dev:MARK.STEWART#ecs03> -rwxr-xr-x. 1 oracle 548 Mar 17 20:09 uptimes.sh
Dev:MARK.STEWART#ecs03> Dev:MARK.STEWART#ecs03>
DATE_STAM ONE_MIN FIVE_MIN FIFTEEN_MIN
--------- ---------- ---------- -----------
17-MAR-16 0 .01 .05
17-MAR-16 0 .01 .05
17-MAR-16 0 .01 .05
Dev:MARK.STEWART#ecs03> 146M .
Dev:MARK.STEWART#ecs03> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Thu Mar 17 20:09:36 CET 2016
oracle. (/home/oracle/sql)
Linux>

Issue running a .SH file on server reboot - Ubuntu 12.04 LTS

I am having an issue getting the .sh file to run. I can run it via sudo ./starter.sh when inside my app directory, but relying for it on reboot isn't working.
I am using an Ubuntu 12.04 VM on Windows 7. I have my files on Windows shared with the VM, so I access my files via /mnt/hgfs/nodejs-test
I am running a node.js server with Nginx via my local VM.
As of right now, I can go to http://node.dev and it will properly load up my server.js located in nodejs-test (/mnt/hgfs/nodejs-test) and output hello world to the screen.
So running the site isn't a problem..but getting forever (forever.js installed globally) to kick in on reboot isn't working. I suspect it simply can't execute my SH file.
Here is my starter.sh
#!/bin/sh
if [ $(ps aux | grep $USER | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
then
export PATH=/usr/local/bin:$PATH
forever start --sourceDir /mnt/hgfs/nodejs-test/server.js >> /mnt/hgfs/nodejs-test/serverlog.txt 2>&1
fi
Now I have tried sudo crontab -e (and added my path to the file) as well as just crontab -e and did the same thing. Upon reboot...nothing.
#reboot /mnt/hgfs/nodejs-test/starter.sh
I tried editing that cronjob to this
#reboot /var/www/nodejs-test/starter.sh
because I created a symlink in /var/www/nodejs-test to /mnt/hgfs/nodejs-test
Where can I check to see if an error fires on reboot, or is it possible my reboot cron isn't running at all? I know running the starter.sh DOES work though.
EDIT The /mnt/hgfs/nodejs-test is owned by root (which might be a windows thing given the files exist on my Windows 7 os). My ubuntu user is "bkohlmeier" which I created on installing the VM.
EDIT #2
Nov 10 13:05:01 ubuntu cron[799]: (CRON) INFO (pidfile fd = 3)
Nov 10 13:05:01 ubuntu cron[875]: (CRON) STARTUP (fork ok)
Nov 10 13:05:01 ubuntu cron[875]: (CRON) INFO (Running #reboot jobs)
Nov 10 13:05:02 ubuntu CRON[887]: (bkohlmeier) CMD (/mnt/hgfs/nodejs-test/starter.sh)
Nov 10 13:05:02 ubuntu CRON[888]: (root) CMD (/var/www/nodejs-test/starter.sh >/dev/null2>&1)
Nov 10 13:05:02 ubuntu CRON[877]: (CRON) info (No MTA installed, discarding output)
Nov 10 13:05:02 ubuntu CRON[878]: (CRON) info (No MTA installed, discarding output)
Ok, I found a solution. Whether it is the "right" solution I don't know. Because my system shares files between HOST and GUEST (windows 7 and Ubuntu VM), the /mnt/hgfs/ has to get mounted (I think)..and the reboot happens quick enough I think the mount isn't aware yet.
So I added this via crontab -e
#reboot /bin/sleep 15; /var/www/nodejs-test/starter.sh
and it worked like a charm.

running ddl file through sh

I am having issue when running a ddl script file through sh on one of the servers but executes fine on another. The script is as below
sqlplus mgr/$1#$2 #export_all_tables_mgr.ddl
if [ $? != 0 ];
then
echo 'ERROR exporting MGR data, Refer to .CSV and .CTL files for detail.' | tee -a MGR_ExtractionLog.log
fi
It uses sqlplus to run the ddl file but one the rogue server, it would just connect to sql plus and won't do anything.
oracle#dbsdev55z2 $ export_all_tables_mgr.sh password servicename
SQL*Plus: Release 10.2.0.4.0 - Production on Thu Jan 24 08:39:18 2013
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production With the Partitioning, Data Mining and Real Application Testing options
SQL>
To mention here, I am using Oracle 10g here. export_all_tables_mgr.ddl file is as below
alter session set nls_date_format = 'DDMMYYYYHH24MISS';
#drop_table_temp_extraction_counts.ddl
#create_table_temp_extraction_counts.ddl
WHENEVER SQLERROR EXIT -1
WHENEVER OSERROR EXIT -1
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF
SPOOL MGR_ExtractionLog.log
SELECT TO_CHAR(SYSDATE, 'DD/MM/YYYY HH24:MI:SS'), '- Start extract process' FROM dual;
SPOOL OFF
define TableName=TABLE1
#ExportTable
define TableName=TABLE2
#ExportTable
SPOOL MGR_ExtractionLog.log append
SELECT TO_CHAR(SYSDATE, 'DD/MM/YYYY HH24:MI:SS'), '- End extract process' FROM dual;
SPOOL OFF
EXIT 0
I'm guessing that this is a shell problem, try adding the following as the first line of your shell script to tell it which interpreter to use:
#!/bin/sh

What is the *nix command to view a user's default login shell

What is the *nix command to view a user's default login shell?
I can change the default login shell with chsh, but I don't know how to get what is the user's default shell.
Pseudocode
$ get-shell
/usr/bin/zsh
The canonical way to query the /etc/passwd file for this information is with getent. You can parse getent output with standard tools such as cut to extract the user's login shell. For example:
$ getent passwd $LOGNAME | cut -d: -f7
/bin/bash
The command is finger.
[ken#hero ~]$ finger ken
Login: ken Name: Kenneth Berland
Directory: /home/ken Shell: /bin/tcsh
On since Fri Jun 15 16:11 (PDT) on pts/0 from 70.35.47.130
1 hour 59 minutes idle
On since Fri Jun 15 18:17 (PDT) on pts/2 from 70.35.47.130
New mail received Fri Jun 15 18:16 2012 (PDT)
Unread since Fri Jun 15 17:05 2012 (PDT)
No Plan.
The login shell is defined in /etc/passwd. So you can do:
grep username /etc/passwd
I think what you are looking for is this:
#!/bin/bash
grep "^$1" /etc/passwd | cut -d ':' -f 7
Save that as get-shell somewhere in your path (probably ~/bin) and then call it like:
get-shell userfoo
SHELL variable is used to represent user's current shell
echo $SHELL

Resources