1834557 Members
3048 Online
110069 Solutions
New Discussion

configuring cron job

 
SOLVED
Go to solution
Shivkumar
Super Advisor

configuring cron job

I have writeen one small shell script and want to configure as cron job. what is the best method to configure it as a cron job ?
20 REPLIES 20
Ken O'Neil
Frequent Advisor

Re: configuring cron job

Log in as the user you want to run the script, then run `crontab script_name`. Check `man crontab` for additional info.
Victor Fridyev
Honored Contributor

Re: configuring cron job

Hi,

The main problem when you write a script, which should run in crontab is environment, mainly PATH. When a script runs from crontab, it's PATH contains /bin and /usr/bin. Because of that, all commands should be used with their full path. In order to simplyfy the problem, put in the beginning of the script
. ~/.profile
or to create a file which contains all needed variables and download it.

HTH
Entities are not to be multiplied beyond necessity - RTFM
James R. Ferguson
Acclaimed Contributor
Solution

Re: configuring cron job

Hi Shiv:

First, read the manpages for 'crontab(1)'. This is really quite explanatory.

Make sure that your login is allowed in '/var/adm/cron/cron.allow'.

Personally I prefer to write any current crontab to a file; edit that file; and lastly replace the current crontab file with the editted one. Others will use the direct edit option of 'crontab' using '-e'.

Thus, method-1 is:

# crontab -l > $HOME/crontab
[ edit $HOME/crontab ]
# crontab $HOME/crontab

Method-2 is:

# crontab -e

Method-2 simply invokes 'vi' using a temporary version of your crontab file (under the account you are logged into, of course). When you write/quit the editted version, your crontab is replaced.

My preference in using method-1 is that I mangle the editting in any way, I can simply discard the file, or write (save) what I have and fix it again before I apply it.

Regards!

...JRF...
Shivkumar
Super Advisor

Re: configuring cron job

The scritp names is say "tulip.sh" and i want execute at 2.25am every day. what should be the syntax ? thanks.
James R. Ferguson
Acclaimed Contributor

Re: configuring cron job

Hi Shiv:

25 02 * * * $HOME/tulip/sh

...where "*" means "every" and the fields are:

minute hour monthday month weekday command

Shiv, I encourge you to use the manpages as a resource. They are meant to be reference material and do assume some knowledge, *but* the more you read them, the more comfortable you will become with trying to read them. Usually you will find examples in the discussion which can be related easily to the initial synopsis (syntax).

The structure of the HP-UX manpages is described here (below). As a user and as an administrator, you will spend most of your time in sections 1 and 1M. A great deal of information on configuration files appears in section-4. Section-9 has a very good glossary.

http://www.docs.hp.com/en/B2355-60127/Introduction.9.html

Regards!

...JRF...
Muthukumar_5
Honored Contributor

Re: configuring cron job

try as,

25 02 * * * Full path to script 1>/dev/null 2>&1

Example:

25 02 * * * /home/shiv/tulip.sh 1>/dev/null 2>&1


See crontab man page more.

--
Muthu


Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: configuring cron job

Hi Shiv,

You asked about, The scritp names is say "tulip.sh" and i want execute at 2.25am every day. what should be the syntax ?

25 02 * * *

-Arun

"A ship in the harbor is safe, but that is not what ships are built for"
Yogeeraj_1
Honored Contributor

Re: configuring cron job

hi,

My typical crontab looks as follows:

25 02 * * * /home/shiv/tulip.sh 1>/dev/null 2>&1
#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
#*******************************************************************************
#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************


hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Arunvijai_4
Honored Contributor

Re: configuring cron job

Hi Shiv,

A great introduction about "Cron"

http://www.unixgeeks.org/security/newbie/unix/cron-1.html

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Nguyen Anh Tien
Honored Contributor

Re: configuring cron job

Pls becareful:
- remember to put /dev/null 2 to your cript
1 mean standard input
2 mean default error

SYNTAX

25 02 * * * path/script 1>/dev/null 2>&1

HTH
HP is simple
Arunvijai_4
Honored Contributor

Re: configuring cron job

Hi Shiv,

A great Wiki resource, http://en.wikipedia.org/wiki/Cron

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Senthil Prabu.S_1
Trusted Contributor

Re: configuring cron job

Hi shiva,
man cron will definetly help you better understanding the concept.


25 02 * * * /fullpath/script/ 1>/dev/null 2>&1



refer this link for more details;
http://www.adminschoice.com/docs/crontab.htm


--
Prabu.S
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Frank de Vries
Respected Contributor

Re: configuring cron job

We use this for stopping an application
prior to the backup in the weekend.
And start is afterwards.

Please not the cron is run with /usr/bin/sh
shell (there can be an impact depending
which shell you use), this is the default.

############################################
# Stop / Start WebSphere for Oracle offline backups
#############################################25 17 * * 6 "/etc/cmcluster/devnew/WebSphere_Stop.sh > /tmp/WebSphere_Stop_devnew.cronlog 2>&1"
30 15 * * 2 /etc/cmcluster/devnew/WebSphere_Start.sh > /tmp/WebSphere_Start_devnew.cronlog 2>&1
Look before you leap
Shivam Tiwari
Honored Contributor

Re: configuring cron job

Hi Shivkumar,

Just a quick check if you are configuring cronjob on a MCSG make sure that you make the changes in the

/etc/cmcluster/PRODNAME so that it carries forward to the faliover node otherwise it will always run on the First Node you configured not on the Running Node.

HTH,

Shivam Tiwari
Never say No. Say I will Do it........
Shivkumar
Super Advisor

Re: configuring cron job

To run the script every hour what should be the syntax ? Regards.
James R. Ferguson
Acclaimed Contributor

Re: configuring cron job

Hi Shiv:

To execute a script every *hour* of every *monthday* of every *month* on every *weekday* do:

0 * * * * /pathname/scriptname

The script will run at 0-minutes after the hour.

NOW, go read the 'crontab' manpages, please! :-)

Regards!

...JRF...
Shivkumar
Super Advisor

Re: configuring cron job

James, I am not a complete sys admin guy. Second, man pages are sometimes difficult to understand. Thanks for your help.
James R. Ferguson
Acclaimed Contributor

Re: configuring cron job

Hi (again) Shiv:

When I wrote, "NOW, go read the 'crontab' manpages, please!", I meant my comment to be helpful.

The manpages are a reference not a tutorial, that's true.

However, the more you struggle to read them, the easier it becomes. Keep trying :-))

No points for this please.

Regards!

...JRF...
Yogeeraj_1
Honored Contributor

Re: configuring cron job

hi shiv,

if you see my above reply, the last line help me a lot when assigning the time/frequency when my script has to run, i.e.

#******************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|-------------------------
#******************************************************
#******************************************************
# END OF TABLE day0->Sunday day6->Saturday
#******************************************************

This is very helpful. (maybe if you copy paste these line in notepad, it would be more readable and will make more sense to you) I have it in all my crontabs. It helps a lot and i use it everytime i want to verify that i have written everything well!

Also, referring to comments by above guru, most of one's queries is found in man pages. Though not easily "readable", these are very technical and informative. Every sysadmin loves it!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Victor BERRIDGE
Honored Contributor

Re: configuring cron job

Hi Shiv,

My advice would be do like me and write a Header for all your cronfiles with the summarized syntax e.g:
# /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
...
You may notice I also put the file in a place where all cronfiles should be (my standards...) with the name of the user of this cronfile. So I rarely (almost never) use crontab -e and can then alway compare the curent and the file for modifications and if there are any ask why (especially roots) I am not informed... some modifications are done by software installation (...)
Dont forget to explicitely redirect ouputs (std and error) otherwise cron will keep on sending you mails about them...
If you do read a .profile to set env variable or do a su - , dont forget to add in that .profile a test for interactive session (since you are in batch mode) e.g.
if [[ $- = *i* ]]; then #tty interactive test (VBE - 030306)
# set term if it's not set
...
fi
otherwise cron will in the messages it will send complain its "not a typewriter"...

Now you should be able to write down a crontab script that is easily readable and exempt of most common errors...

All the best
Victor