Em Ae
Em Ae

Reputation: 8734

setting crontab from bash script

I have written the following script but i am getting error sqlcorn.sh: No such file or directory

here is the script

#!/bin/bash
ORACLE_HOME="/opt/app/oracle/product/11.2.0/dbhome_1"
ORACLE_SID="HEER"
ORACLE_USER="USER1"
ORACLE_PASSWORD="USERP"

echo "export ORACLE_HOME=$ORACLE_HOME" >> sqlcronprocedure.sh
echo "export PATH=\$ORACLE_HOME/bin:\$PATH" >> sqlcronprocedure.sh
echo "export ORACLE_SID=$ORACLE_SID" >> sqlcronprocedure.sh
echo "rTmpDir=/tmp" >> sqlcronprocedure.sh

echo "sqlplus -s $ORACLE_USER@$ORACLE_SID/$ORACLE_PASSWORD  > $rTmpDir/deleteme.txt 2>&1 <<EOF" >> sqlcronprocedure.sh
echo "    select 1 from dual;" >> sqlcronprocedure.sh
echo "    execute someproc(1000,14);" >> sqlcronprocedure.sh
echo "EOF" >> sqlcronprocedure.sh

chmod 755 sqlcronprocedure.sh

crontab -l > sqlcron.sh
echo "0,15,30,45 * * * * /sqlcronprocedure.sh" >> sqlcron.sh
crontab sqlcorn.sh

This is my first ever script. so i apologize if things are too obvious to ask

Upvotes: 0

Views: 2143

Answers (2)

Keith Thompson
Keith Thompson

Reputation: 263637

The real problem: You misspelled the file name on the crontab command. Change it from:

crontab sqlcorn.sh

to

crontab sqlcron.sh

Some more comments on your code:

Your multiple echo commands are better written as a "here document". Rather than this:

echo "export ORACLE_HOME=$ORACLE_HOME" >> sqlcronprocedure.sh
echo "export PATH=\$ORACLE_HOME/bin:\$PATH" >> sqlcronprocedure.sh
echo "export ORACLE_SID=$ORACLE_SID" >> sqlcronprocedure.sh
echo "rTmpDir=/tmp" >> sqlcronprocedure.sh

echo "sqlplus -s $ORACLE_USER@$ORACLE_SID/$ORACLE_PASSWORD  > $rTmpDir/deleteme.txt 2>&1 <<EOF" >> sqlcronprocedure.sh
echo "    select 1 from dual;" >> sqlcronprocedure.sh
echo "    execute someproc(1000,14);" >> sqlcronprocedure.sh
echo "EOF" >> sqlcronprocedure.sh

you can do this:

cat <<EOF >sqlcronprocedure.sh
export ORACLE_HOME=$ORACLE_HOME
export PATH=\$ORACLE_HOME/bin:\$PATH
export ORACLE_SID=$ORACLE_SID
rTmpDir=/tmp
EOF

cat <<END_OF_SCRIPT >sqlcronprocedure.sh
export ORACLE_HOME=$ORACLE_HOME
export PATH=\$ORACLE_HOME/bin:\$PATH
export ORACLE_SID=$ORACLE_SID
rTmpDir=/tmp

sqlplus -s $ORACLE_USER@$ORACLE_SID/$ORACLE_PASSWORD  > $rTmpDir/deleteme.txt 2>&1 <<EOF
    select 1 from dual;
    execute someproc(1000,14);
EOF
END_OF_SCRIPT

which is a bit easier to read. (Your version wasn't incorrect, just hard to read and error-prone; you have to get the >> sqlcronprocedure.sh right on eacn and every line.)

Note: You use >> to build sqlcronprocedure.sh, which appends to the existing sqlcronprocedure.sh if it exists. I don't think that's what you want to do; you probably want to create the file from scratch. My code assumes you want to create the file rather than appending to it.

The last part of your script:

crontab -l > sqlcron.sh
echo "0,15,30,45 * * * * /sqlcronprocedure.sh" >> sqlcron.sh
crontab sqlcorn.sh

is fine except for two things.

First sqlcron.sh is not a good name for the file, since it's not a shell script; just call it sqlcron. The system doesn't care, but you should.

Second, the misspelling I mentioned above.

Upvotes: 2

krisdigitx
krisdigitx

Reputation: 7136

you need to specify the full path to sqlcron.sh in your script...

Upvotes: -1

Related Questions