1835703 Members
2593 Online
110082 Solutions
New Discussion

crontab

 
SILVERSTAR
Frequent Advisor

crontab

Hello,

I have scheduled a job into the cronfile:
54 05 * * 0,1,2,3,4,5 /QA/QA/JOB/manudt.sh >/QA/QA/JOB/manudt.out 2>&1


the result is:
$ cat manudt.out
/QA/QA/JOB
Not a terminal
stty: : Not a typewriter
stty: : Not a typewriter
/QA/QA/JOB/manudt.sh[5]: COBDIR: Parameter not set.
logout

======================

The job is :
. /QA/QA/JOB/.profile
echo $HOME
echo $COBDIR
$MPE_ROOT/$SS_HOME/OGG/UTMANUDT


If I run the job manually it works, the log is:
$ cat manudt.out
/QA/QA/PUB
Not a terminal
/QA/QA/PUB
/opt/lib/cobol
logout


Why using crontab the .profile seems to be not loaded ?

How can I include .profile directly on the crontab command line and so remove .profile in the job?
From "man crontab" :
Users who desire to have their .profile executed must explicitly do so
in the crontab entry or in a script called by the entry.


Thanks
Angelo
7 REPLIES 7
Jean-Luc Oudart
Honored Contributor

Re: crontab

Usually we would use a wrapping script. this to run a specific profile (cron_profile) and then run the job.

Rgds,
Jean-Luc
fiat lux
Vijaya Kumar_3
Respected Contributor

Re: crontab

You can add the entries in .profile to your script after shabang(#!)

you seems to be executing some stty commands it seems.

You can add these .profile settings to your script like
#!/usr/bin/sh
HOME=/home/user

..your script here

thanks
-Vijay
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
Vijaya Kumar_3
Respected Contributor

Re: crontab

may be you are doing a stty settings which are not allowed in cron.

-Vijay
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
Graham Cameron_1
Honored Contributor

Re: crontab

Further to above, does your script (or something called by it) issue any "stty" commands.
These commands are typically in your .profile, and require a terminal to be attached.
From cron, there is no terminal, hence the errors.
You can enclose the stty command(s) in an if statement, so they only get called when a terminal is attached.
Like so:

if [ -t ]
then
stty commands here
fi

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Patrick Wallek
Honored Contributor

Re: crontab

crontab does *NOT* source your .profile when a job is run. What you really should be doing is setting ANY and ALL environment variables you may need in your script, or source another file that has all of the settings you need.

You could just source your .profile, but you would get a lot of junk you don't need, like things are assuming you are working from a terminal.
James Specht
Trusted Contributor

Re: crontab

You do not want to add your .profile to a cronjob. If a response is required in the .profile your job will be stopped. Your best solution is to create an environment file that will load everything your job needs when run from cron. Such as paths to binaries or forcing a terminal setting. Then include this file in your script.

Example:
env.inc
--------------
export PATH=$PATH:/usr/local/mystuff
TERM=vt100
VAR1=1024


myscript
--------------
#!/usr/bin/sh
. /home/user/jdoe/env.inc
.. the rest of your script


--Jim
"Everyone can be taught to sculpt: Michelangelo would have had to be taught how not to. So it is with the great programmers."
Bill Hassell
Honored Contributor

Re: crontab

Whenever a script works fine from a login but fails in cron, the problem is cron does not use your login environment. cron is NOT a login and therefore does not run /etc/profile or .profile, etc. cron simply runs on behalf of the owner. According to man page for cron, there is a VERY limited environment (very short $PATH, etc) and you do not want any interactive terminal commands to be executed in your cron script. Commands like tabs, ttytype, tput, tset, stty are meaningless because there is no terminal attached to cron.

Always write your cron scripts to completely stand alone, that is, set $PATH explicitly (actually a good idea for all scripts), set ENV variables as needed and do not include interactive terminal commands. Unless you carefully rewrite /etc/profile and your local .profile, you will always get errors such as: not a typewriter


Bill Hassell, sysadmin