Running a shell script in cron...email not sending - help??

I am pretty new to Unix shell scripting, but wondered if anyone could help (in layman's terms if possible!!) :slight_smile:

I have a shell script which ultimately sends an alert to an email address if part of a batch of programs fails. Here's the script that sends the email:

Script: 6check.csh
#!/bin/csh
set alertperson = `cat /export/home/bdodym/alert.person`
if (-f critabend.tag ) then
cat callout6.msg | rmail $alertperson
rm critabend.tag
endif

This is where $alertperson will be forename.surname@address.com designation in the alert.person script

From the command line, all runs fine.

However, if I run through cron, it still seems to be executing the scripts (e.g files are created that I'm asking the script to do generally), but the email is not being sent as I would hope. In the below crontab, 6start.sh has a number of scripts to run through, the last of which is 6check2.csh (as per above)

crontab -l
#!/bin/csh
#############################################
# Minute Hour Month Day Month Weekday Command
# ___________________________________________________________
# 0 01 * * * Job_1
#
51 * * * * /export/home/bdodym/6start.sh

Does anyone know if there is anything additional I need to do add to either the script, or cron, to ensure the email sends? I think (from my VERY limited knowledge of this kind of thing!) that its something to do with the fact cron is running in the background rather than the terminal, as per the command line success. But, this is the final piece of the jigsaw and I just need to ensure I can get it to fit!! :wink:

Have the output of that cronjob redirected to some file like

51 * * * * /export/home/bdodym/6start.sh >> /somelogdir/6start.sh.out 2>&1

and check what's wrong. Additionally you can add a "set -x" into your shell script to see what's going on inside.
Don't forget that cronjobs usually don't have the same environment like when a user would start the script - so you should source the .profile or .kshrc or .bashrc, whatever your environment setup files are.

zaxxon - thankyou for your reply. I was just reading up about "source". So if I were to source it, would I just add the source to 6check.sh (the emailing bit) or 6start.sh too (which, as mentioned just contains a list of scripts to run, as per below...)

#!/bin/sh

6jobstatus.sh
6clean.sh
6check.sh
6check2.csh

Thanks,
Tim

use vi editor to create a file using .sh extension which will have shell scripts to execute.
then execute this file using command
sh filename.sh
e.g. if u have created a shell file sample.sh then u will execute this file using

sh sample.sh

this command...
go and try different kinds of shell scripts you will definitely explore more about UNIX.

Hi sumitbabar

I already have the .sh extensions, my problem is my emails aren't sending when run from cron. All is fine from the command line.
Thanks, Tim

The FAQ section has an extensive posting about troubleshooting cron jobs. http://www.unix.com/answers-frequently-asked-questions/13527-cron-crontab.html

Try to give full permission for that file

or

chmod 755 finename

Ok, thanks.....to which file?

To source or sourcing means to get settings from a shell script "imported" into your current shell. Works like

. ./.profile

So if there is something important set or exported for your environment in .profile, it will be "sourced" by the additional dot in the front of it.

That the files/shell scripts should be executable by the user of that crontab he meant.