Shell script do nothing

hi experts,

could u pls help me with below sh script.

test.sh
*

su - oracle
sqlplus -s username/pwd@dbname
DELETE FROM table1 a WHERE id < (SELECT MAX(id) FROM table1 b WHERE a.msisdn= b.msisdn);
commit;
exit;

*

i tried to run with " nohup test.sh & " but error below appear :

[root@mcnbadbucm tmp]# nohup test.sh &
[1] 8383
[root@mcnbadbucm tmp]# nohup: appending output to `nohup.out'
nohup: cannot run command `test.sh': No such file or directory

[1]+  Exit 127                nohup test.sh

please help as i need to put this script in cronjob

Depending on how your PATH environment variable is set, you might need ./test.sh instead of test.sh . Either way, you need to have execute permissions set on test.sh as set by:

chmod +x test.sh

If you're going to run it from cron , you'll need to specify an absolute pathname to test.sh and every file referenced in test.sh (whether it is a data file or the name of a utility) will need to use an absolute pathname unless you use something to set up the environment of the script to be run in the directory containing your data files and has PATH set appropriately for all of the commands being invoked by your script.

Regarding running SQLPlus via su (which might explain the "does nothing" element of your question), here's an example, substituting values as necessary!:

[root@ora11 tmp]# cat testScript
su - oracle -c '
ORAENV_ASK=NO
ORACLE_SID=DB1
. oraenv
sqlplus / as sysdba << !
  select 33 from dual;
!
'
[root@ora11 tmp]# ./testScript
The Oracle base for ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1 is /u01/app/oracle

SQL*Plus: Release 11.2.0.1.0 Production on Tue Jan 21 01:26:45 2014

Copyright (c) 1982, 2009, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>
	33
----------
	33

SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

You likely don't have to use su at all, if you're root, and this won't work if you're not root.

thanks all.

i am able to execute and run with cronjob after declaring my Oracle HOME
and add cronjob as below :

30 05 * * * nohup /tmp/a.sh >> a.txt &

but no log file generated. please advise

When you use cron to run a job, cron will run it in the background on its own and the cron job will not be stopped whether you are logged in when the script starts or not, whether you log out while the script is running or not, and (in all likelihood) will not run in the directory where you issued the job for cron to run.

Try this instead:

30 5 * * * /tmp/a.sh >> /absolute/path/to/a.txt