Operating System - HP-UX
1827715 Members
2628 Online
109968 Solutions
New Discussion

Re: New to UNIX, how do I create a scrip and a cron job?

 
SOLVED
Go to solution
Victor_138
Regular Advisor

New to UNIX, how do I create a scrip and a cron job?

I'm new to the Unix world and need to create a script to shutdown Oracle at 9:00pm and restart it at 5:00am. Also need to create a cron job to execute that script on a daily basis.

Any pointers?
23 REPLIES 23
Rick Garland
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

At the basic level you can use the dbstart and dbshut scripts provided by oracle. Beware, depending on version you may have trouble with these scripts.

There are other issues to be aware of as well such as the type of environment you are in. You are making a blanket request and not all solutions will fit your needs.

As to cron;

00 21 * * * /dbshut
00 05 * * * /dbstart

This will execute dbshut at 2100 hrs daily
This will execute dbstart at 0500 hrs daily.

Again, this is a simple version of the syntax.
Victor BERRIDGE
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

Hi,
I believe you already have a script...
Look in /sbin/init.d/ for something oracle related...
Does you oracle instance start at bootup?
If yes then at which runlevel - most chances to be 3, so look in /sbin/rc3.d/ a Sxxxoracle or something like

All this to say you have the script you now just need to create a cron file
man cron for a start
Then check with the sysadmin if you have the rights to create/use cron


All the best
Victor
Victor_138
Regular Advisor

Re: New to UNIX, how do I create a scrip and a cron job?

I did found an Oracle related file on /sbin/init.d. How can I determine the runlevel of the file?
Victor BERRIDGE
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

To know your actual runlevel:
who -r

To see how your script is launched, you should find in /sbin/rc2. a Kxxxfile linked to your oracle file in /sbin/init.d the same goes for rc3.d but this time you have to look for a Sxxxfile.


All the best
Victor
Victor_138
Regular Advisor

Re: New to UNIX, how do I create a scrip and a cron job?

On /sbin/rc2.d I found S910oracle linked to oracle in /sbin/init.d but on /sbin/rc3.d was nothing that I could relate to Oracle.

How this works? Do I have to create the file in /sbin/init.d and link it to /sbin/rc2.d?

I'm kind of lost here.

lawrenzo
Trusted Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

Ok you are looking in the /sbin/init.d/oracle script to determine what options are required to stop and start oracle.

the rc.2 directory means that when the system is booted oracle starts at run level 2, the system is up at run level 3 (/sbin/rc.3/) and a whole bunch of other programs / process will be started at this level as the server is booted.

You want to stop and start oracle via cron, once you find the options from the oracle script add the following to cron:

00 21 * * * /sbin/init.d/oracle stop
00 05 * * * /sbin/init.d/oracle start.

This should do the trick.

hello
Nguyen Anh Tien
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

1, Allow oracle user run crontab by modifying /var/adm/cron/cron.allow
root
oracle
:wq!#then save and exit
2, get attached file for scripts
3, su - oracle
[server:/] su - oracle
exit crontab
#crontab -e

[server:/home/oracle] crontab -e
# minute hour date month weekday command
00 21 * * * sh /home/oracle/scripts/Ora_shutdown.sh
00 05 * * * sh /home/oracle/scripts/Ora_start.sh

that's all
tienna
HP is simple
Victor BERRIDGE
Honored Contributor
Solution

Re: New to UNIX, how do I create a scrip and a cron job?


>On /sbin/rc2.d I found S910oracle linked to oracle in
>/sbin/init.d but on /sbin/rc3.d was nothing that I could relate to Oracle.
>
>How this works? Do I have to create the file in
>/sbin/init.d and link it to /sbin/rc2.d?

at run level 2 when the system is booting it looks sequentialy for files to start and
called S0XXXfile and gives the start option
at level 1 when system i beeing brought down
It look for Kxxxfile and give stop argument

I believe your oracle file should be in rc2.d for Kxxx and rc3 for SYYY

But its up to you...

Here is an example of what we do for start/stop scripts for oracle:

#!/usr/bin/ksh
export ORACTI_EXPLOIT=/opt/oracle/admin/exploit
export PATH=$PATH:/usr/bin:$ORACTI_EXPLOIT/proc


case "$1" in
start_msg)
echo "Start Oracle"
;;
stop_msg)
echo "Stop Oracle"
;;
start)
$ORACTI_EXPLOIT/proc/db_start
su - oracle -c $ORACTI_EXPLOIT/proc/net_start
;;
stop)
su - oracle -c $ORACTI_EXPLOIT/proc/net_stop
$ORACTI_EXPLOIT/proc/db_shut
;;
*)
echo "usage: $0 {start|stop}"
#rval=1
;;
esac

----------------------------------------
Here is an extract of a cronfile the way I do...
Notice I put conrfiles in a structure /sm/cron with big comments on the syntax
and usage since I have poor memory...

This leaves a way to modify the the cronfile by getting it at its location.
TO load it is simply crontab cronfile.root
--------------------------------------

# /sm/cron/cronfile.root
# crontab file for the super-user.
#
# ==========================================================================
# F O R M A T
# ==========================================================================
# Minute Hour Month_Day Month Weekday Command
# (0-59) (0-23) (1-31) (1-12) (0-6)*0=sun run-string
# * = in any crontab field represents all legal values.
# Therefore, to schedule the ps(1) cmd to execute at 5:10pm on every
# friday and monday during june, july, august, you would make an entry
# in your crontab fithe that looks like this :
# 10 17 * 6,7,8 1,5 ps>>psfile 2>&1
# The 2>&1 redirects any error msg to the file psfile.
# ==========================================================================

# archive annuelle de l export le 3 dimanche de janvier chaque annee
#00 04 * 1 0 /sm/bin/archive_annuelle >>/sm/log/archive_ann.er 2>&1

# log kernel diagnostic messages every 10 minutes
05,15,25,35,45,55 * * * * /usr/sbin/dmesg - >>/var/adm/messages
#05,15,20,35,45,55 * * * * /usr/bin/ulimit >>/var/adm/ulimit.cron 2>&1
#05,15,21,35,45,55 * * * * /usr/bin/ulimit -a >>/var/adm/ulimit.cron 2>&1
07,17,27,37,47,57 * * * * /var/adm/root/clean_tmp>>/var/adm/cleantmp.log 2>&1

# Sample nightly invocation of calendar. If you wish to run calendar,
# just uncomment the next line.
#0 4 * * * exec /usr/bin/calendar -
# ==========================================================================
# effacer les disques temporaires
# ==========================================================================
#
00 23 * * 1,2,3,4,5 /sm/cron/bin/effacer>>/sm/cron/log/efface.log 2>&1
05 23 * * 1,2,3,4,5 /sm/cron/bin/clean_xprt_s>/sm/cron/log/cleanXs.log 2>&1

# ============================ E N D =======================================

NOw user who are allowed to use cron or at cmd ar defined in files in /var/adm/cron:
-r--r--r-- 1 bin bin 18 Nov 14 2000 at.allow
-r--r--r-- 1 bin bin 31 Feb 12 2003 cron.allow
-rw-r--r-- 1 root root 1041016 May 18 10:47 log
in cron .allow and at.allow you give the users you are allowed:

:/var/adm/cron $ more cron.allow
root
adm
vbe
uucp
prod
rdm
dupont



-----


Courage!!

All the best
Victor
Suraj Singh_1
Trusted Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

I don't really see why would you require to shut and start Oracle every evening and morning.

As far as oracle startup script exists in /sbin/init.d and is linked to /sbin/rc2.d.. it should suffice.

Regards
What we cannot speak about we must pass over in silence.
Victor_138
Regular Advisor

Re: New to UNIX, how do I create a scrip and a cron job?

The need is to perform cold backups.
Victor_138
Regular Advisor

Re: New to UNIX, how do I create a scrip and a cron job?

3, su - oracle
[server:/] su - oracle
exit crontab
#crontab -e

I got this:

kcspecdp prdcop >exit crontab
ksh: crontab: bad number
logout

After entering this kcspecdp # crontab -e VI gets started.

What I'm doing wrong?
Geoff Wild
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

If it's a backup - why not have the backup stop/start Oracle for you instead?

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Rick Garland
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

Try this as your script;

su - oracle -c $ORACLE_BIN/dbshut
# This is an oracle provided script to stop database(s)
# verify the database is down by checking procs
# example, ps -ef | grep -e pmon -e smon
# do the backup
su - oracle -c $ORACLE_BIN/dbstart
# This is an oracle provided script to start database(s)
# verify database is up by checking procs
# example, ps -ef | grep -e pmon -e smon


Depending on your backup application, there is usually some sort of pre- and post-exec procedure that you can configure so that the oracle stop/start script is run in conjuction with the backup itself. All that is needed in the cron is a job that fires off the backup.

00 21 * * * /$HOME/bin/do_backup
You do your backup at 2100 hrs daily, 7 days a week. The script is called 'do_backup' and has all the stuff in it. This can eliminate the need to have 2 cronjobs and can reduce the downtime of the oracle database(s) by starting the databases as soon as the backup is completed.

Beware, depending on the version of oracle you are using, there were some troubles with the dbshut/dbstart scripts as supplied. The fix was simple but just so you have the knowledge of.
Victor_138
Regular Advisor

Re: New to UNIX, how do I create a scrip and a cron job?

Think I finally got it.

Is there a way to verify if the scripts were executed?
Victor_138
Regular Advisor

Re: New to UNIX, how do I create a scrip and a cron job?

Geof,

The backup will be done thru a Windows box running Networker. It has the ability to execute pre and post job commands but don't know if that only works on the Windows side, I'll give that a try to see if it works.
Rick Garland
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

You can check the cron log file to see if the cron job went off. One thing you might want to put in your script is a LOG feature.

Example; LOG=$HOME/my_backup_log
In your script have the command outputs redirected to $LOG.

some_command >> $LOG

Can add a `date` feature in there as well to help you keep track


Rick Garland
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

The cron log is in /var/adm/cron/log
Victor_138
Regular Advisor

Re: New to UNIX, how do I create a scrip and a cron job?

What does this mean?

! *** cron started *** pid = 1098 Fri May 20 07:15:58 SAT 2005
> CMD: sh /u01/dba/oracle/scripts/Ora_shutdown.sh
> oracle 13042 c Tue May 24 23:00:01 SAT 2005
< oracle 13042 c Tue May 24 23:00:01 SAT 2005 rc=1
> CMD: sh /u01/dba/oracle/scripts/Ora_start.sh
> oracle 13300 c Wed May 25 05:00:01 SAT 2005
< oracle 13300 c Wed May 25 05:00:01 SAT 2005 rc=1

Did the cron job worked?
Cem Tugrul
Esteemed Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

Rick Garland
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

There are errors in the job rc=1.

A cronjob fired off by oracle at 2300 hrs on May 24 with PID of 13042 but for some reason got a error of rc=1.

> CMD: sh /u01/dba/oracle/scripts/Ora_shutdown.sh
oracle 13042 c Tue May 24 23:00:01 SAT 2005
oracle 13042 c Tue May 24 23:00:01 SAT 2005 rc=1

A cronjon fired off by oracle at 0500 hrs on May 25 with PID of 13300 but again got rc=1.


> CMD: sh /u01/dba/oracle/scripts/Ora_start.sh
> oracle 13300 c Wed May 25 05:00:01 SAT 2005
< oracle 13300 c Wed May 25 05:00:01 SAT 2005 rc=1
A couple of places to check. Look at the alert.log for oracle. From the $ORACLE_HOME you should be able to do a 'find . -name alert.log -print' command to find the alert log. (This can be put elsewhere so check with DBAs). View the alert log, if the database went down and started up again then these entries will be in alert log.

Another thought is that when you put jobs into cron for execution you must set all the environment variables. The cron does not carry over any of these env settings.

In your scripts put in the 'set -xv'. If you can run manually try it. Else let the next cron job run it and see what the output says.

For your cronjob, have it send output to your email.

Example:

00 23 * * * /u01/dba/oracle/scripts/Ora_shutdown.sh | mailx -s "My oracle stop cron" you@address.com

00 05 * * * /u01/dba/oracle/scripts/Ora_start.sh | mailx -s "My oracle start cron" you@address.com


Victor_138
Regular Advisor

Re: New to UNIX, how do I create a scrip and a cron job?

Cem,could not log in.
Geoff Wild
Honored Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

Victor - here CEM's link in NA: http://www1.itrc.hp.com/service/james/search.do?todo=search&admit=-682735245+1117031311187+28353475&searchtext=rc+error&from=forums&origin=0&wpa=forums1.itrc.hp.com%3A80&searchcategory=ALL&hpl=1&searchcriteria=allwords&rn=25&source=7000&chkServStor=on&presort=rank&esc=europe-support3.external.hp.com

As far as Networker - not too sure if you can get it to execute a script on the Unix server or not - I would think you could....

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Cem Tugrul
Esteemed Contributor

Re: New to UNIX, how do I create a scrip and a cron job?

Thank's Geoff...
Our greatest duty in this life is to help others. And please, if you can't