1834461 Members
2540 Online
110067 Solutions
New Discussion

cron wont run ..

 
SOLVED
Go to solution
someone_4
Honored Contributor

cron wont run ..

I have a script that I wrote that will run from the command line. But it wont run when I schedule it as a cron. I have looked at the search and found some info.
I have put the space with crontab -e
/path/ ./script
I made sure it was the right shell at top #!/usr/bin/sh .. On the cron log I see rc=1.
Any ideas?

Thanks
Richard
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: cron wont run ..

Hi Richard:

The most usual problem with scripts that execute from a command line but not from a crontab is the absence of proper environmental variable and/or PATH.

Unless you provide any variables, particularly including your PATH as you would have when your $HOME/.profile is sourced at login, it is common for a script to fail.

You can source (read) your profile ahead of calling your script. This can/should be done in the crontab entry. Alternatively, you could source your profile within the script itself to provide the necessary environmental variables you need. Thirdly, you could create a new file which contains and exports the common variables needed for your application, and source (read) that within your standard profile, within standard scripts; and/or in a crontab stream.

To source (read) a file within a script, put a dot (".") in front of the script name. For instance, to source a file called /myscript you would do:

. /myscript #...note the space between the dot and the script name.

...JRF...
Mark Vollmers
Esteemed Contributor

Re: cron wont run ..

Hi Richard-

Are you running the cron job as root? if so, did it send you a mail when it ran? There might be some information about why it failed. Also, are there any environment vairables that are used in the script? it seems to me that there have been problems passing those variables to a cron job and you have to do some special coding to get them in.

Mark
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"
A. Clay Stephenson
Acclaimed Contributor

Re: cron wont run ..

Hi Richard,

This almost always is a result of the fact that cron runs in a very sparse environment.

Let's say that you are running a cronjob as user oracle, you expect that things like ORACLE_SID, TNS_ADMIN, etc. are set because they are in oracle's .profile. Wrong because .profile is not sourced and thus not only are these environment variables not set but even the PATH is very limited.

The best way to do this is to create a file like /usr/local/bin/oraenv.sh and let it set
and export any needed environment variables. Do not put an exit statement in this file. You the add . /usr/local/bin/oraenv.sh in oracle's .profile and also let it be sourced by your cron script. You can also simply do all the exports explicitly in your cron script but the first method is better because you need only make changes in one place.

I've obviously used oracle as an example but you get the idea.

REgards, Clay
If it ain't broke, I can fix that.
Sachin Patel
Honored Contributor

Re: cron wont run ..

Hi Richard,
As other says it could be variable problem or path problem. use full path inside the script for any command. for example.
set remove= "/usr/bin/rm"

If you can paste your script here.

Sachin
Is photography a hobby or another way to spend $
someone_4
Honored Contributor

Re: cron wont run ..

Ok here is my script.
I do have a few variables in here.

Richard
someone_4
Honored Contributor

Re: cron wont run ..

ohh and why you guys are looking at the script.
If you see someone that I did wrong or know a better way to do it. please tell me, dont hold back. Im here to learn from the masters.

Richard =)
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: cron wont run ..

Hi Richard,

I see several possible problems but they all relate to what's been said.

1) Your hostname file is a relative path. You need to either cd to some desired location or
specify an absolute path.

2) For EVERY command that is not a shell built-it, the golden rule in cron is specify the absolute path or better set and export PATH within your script. For example, do a type ping, a type bdf, etc.

/usr/sbin/ping
/usr/bin/bdf

At the very least, you should then
PATH=/usr/sbin/ping:/usr/bin/:${PATH}
export PATH

This may not be all be you get the idea, remember the cron environment is intentionally very sparse and stupid; you have to tell it everything. It can barely spell cron on its own.

Clay
If it ain't broke, I can fix that.
someone_4
Honored Contributor

Re: cron wont run ..

Great thanks for the info I got it to work with the changes posted.
I didnt realize that cron was "dumb"
If anyone is still reading this post are there any books or websites that I can go to for more info on profiles and shells? ..
Thanks for your help.

Ps: Mr "King" James .. I didnt mean to give you a 6 .. I was trying to give you a 7.


Richard =)

A. Clay Stephenson
Acclaimed Contributor

Re: cron wont run ..

Hi Richard,

Your script now looks much better but one minor criticism. You execute /usr/sbin/ping
but you have already added /usr/sbin/ to your PATH. You can now simply execute ping. I would say use the absolute pathname or set PATH. You're not wrong this is just better form.

As for online docs, I have not been able to find any decent tutorials. You can and should
man sh-posix | lp to get a printed copy. This is very detailed and quite good but short on examples.
My favorite shell programming book is 'Unix Shell Programming' by Stephen G. Kochan & Patrick H. Wood. The O'reilly 'Learning the Korn Shell' is pretty good also. Both of these are available from amazon.com. Don't worry about differences between POSIX shell and Korn shell at this point. The differences are minor.

The other thing to do is convince your boss to start a UNIX library. This is a must. The very best place to start is:
http://www.oreilly.com

Regards, Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: cron wont run ..

Hi Richard:

You asked about "favorite" books on Shell programming.

I particularly like "UNIX Shell Programming" by Lowell Jay Arthur & Ted Burns, 3rd ed. copyright 1994, John Wiley & Sons, IBSN 0-471-59941-7.

...and for a quick reference and a good overview of the major shells, I would add HP's own "Shells: User's Guide":

http://docs.hp.com/hpux/onlinedocs/B2355-90046/B2355-90046.html

Lastly, don't forget the 'sh-poxix' man pages. There is a wealth of good information there.

The question of "favorites" has come up before. Here's another link with come additonal comments:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0xbb5e7e990647d4118fee0090279cd0f9,00.html

I would concentrate on the Posix shell (or ksh, from which it derives). The default shell for root *must* be the Posix shell as must all scripts in the /sbin/rc?.d/ startup directories. I'd avoid the c-shell (csh). It's hardly used, and lacks sophisticated features. Learn Poxix and you can naturally use the ksh and bash shells too.

Finally, read and study existing scripts whenever you can. There's plenty of good tricks and techniques to be learned. Write as often as you can.

Regards!

...JRF...
Admin.SIF
Frequent Advisor

Re: cron wont run ..

Hi,
try /sbin/init.d/cron stop
and /sbin/init.d/cron start

Nora
Sysd. Amin. Inforef