Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mailx works manually, not in crontab

Status
Not open for further replies.

tradntele

Programmer
Nov 7, 2001
14
US
My shell script wc's a few files (stands for word count, right?) and uses mailx to put the results in the body of an email. It works perfectly when invoked manually at the command line, however, after setting up the same script in crontab, the body of the resulting email comes out blank.

The script basically ftp's in the files, and then runs the following command...

echo "`wc $filea`
`wc $fileb`
`wc $filec`" | mailx -s "Line counts" xxx@yyy.com

So why does it work manually, but not in crontab... what do I need to look for?
 
You need to specify paths more precisely - your crontab script will not have the environment variables set as you do when you are logged in

Alex
 
Thanks Alex. But I do assign the values of the variables earlier in the same script. Wouldn't that make the variables available while in crontab? Or are you saying I have to "hard code" in the path and file names when using crontab?
 
I got bored quite a while ago with hardcoding paths in scripts that run under cron.

I created a file, /usr/local/bin/cronvars.sh, that set environment variables correctly.

All you need to do then is call that script - in a special way - as the first statement in every cron script. Like this:

. /usr/local/bin/cronvars.sh

The leading dot is important, it tells the shell to run the script in the *current* shell, so that changes made to environment variables will be visible in your script. If you omit the dot the script will be run in a new shell and the variables you set in their will not be visible to your cron script. Mike
______________________________________________________________________
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top