Crontab not working

Hi All,

I have a script with deatils as :

[muser@server scripts]$ ls -ld catch_logs.sh 
-rwx--x--x 1 muser muser 752 Jun  5 22:36 catch_logs.sh

User crontab looks likes:

[muser@server scripts]$ crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin

* * * * * /opt/scripts/catch_logs.sh

Now if I run this script manually, I do get an email(configured inside script). But once this script runs via cron, no email is received.

I do not have admin rights on the system so I cannot view any /var/log/* .

Any idea why the script in not sending out email when being triggered by cron ?

For the same reasons that dozens of other threads have asked about...
The environment used by cron (and passed to programs it runs), is not you login shell's environment.

You either need to have your script set up the correct environment or you need to use absolute paths to all of the files your script uses (executables, input files, and output files).

Obviously, if you don't show us your script, we can't help you fix it!

1 Like

The script is:

It basically is monitoring a certain log file for a certain type of error. If there is a new error, it sends an email to email addresses specified in email.users .

If no new error has occurred, do nothing.

#!/bin/bash

LIVE_FILE=/opt/adapt/log/server.log
TMP_FILE=/opt/scripts/server.log.tmp
TMP=/opt/scripts/server.tmp

grep "ephemeral" $LIVE_FILE > $TMP

if diff  $TMP $TMP_FILE >/dev/null
   then
        echo "Error Same"	
	#Do Nothing
else
        #Send Email
        ERROR=`cat $TMP`
        echo "Error occurred on `hostname`
        $ERROR
	* This is an auto-generated email *
        " | /bin/mailx -r "noreply@company.com" -s "Error occurred on `hostname`" `cat email.users`
fi

cp $TMP $TMP_FILE

The cronjob's output, including errors, should go to your mailbox.

Check your mail.

$ mail

Hello Junaid,

Just want to add here on Scott's comment, Cron has an own reserved syslog facility, so you should have a look into /etc/syslog.conf (or the equivalent file in your distro) to see where messages of facility cron are sent. Also you can check /var/spool/mail/user_name too for same. Let us know is this helps.

EDIT: Also while checking your script I found out that line if diff $TMP $TMP_FILE >/dev/null
I think what you are trying to do here to, not to display the differance in standard output, this command will redirect output to /dev/null so each time condition will be FALSE and if I am right here then you can use following then.

Here is an example which I have tested for same.
Input files:

cat chumm1
test test1
test
 
cat chumm2
test test1
test20

Now check this following command out.

if [[ -z $(diff chumm1 chumm2 2&>1 /dev/null)
then
       echo "There are differances.";
else
       echo "there is nothing";
fi

Output will be as follows.

There are differances.

Also why can't you go for a variable named email_users="x@chumma.com y@chumma.com z@chumma.com .

echo "Error occurred on `hostname`
        $ERROR
 * This is an auto-generated email *
        " | /bin/mailx -r "noreply@company.com" -s "Error occurred on `hostname`" $email_users < $ERROR

Could you please try same and let us know if that helps.

Thanks,
R. Singh

Ravinder, the diff is okay; the redirection of stdout does not clear the exit status.
In fact your correction is faulty:
2&>1 should be 2>&1 (redirect stderr to descriptor 1, and should be appended to also go to /dev/null), but is not needed for diff AFAIK.
--
Actually I don't see anything wrong in the script. Note that email.users is expected in the cron user's home directory.

2 Likes

There doesn't appear to be anything wrong with the script. As Don said (post#2), nearly all these things are because the interactive login and cron environments are different; perhaps the variables set in .profile, or the path searched. These things need to be deliberately set at the start of the script to match those in effect when the script is run interactively.

Read again Don's post#2.

1 Like

That was the issue. As soon as I gave the full path, it worked.

Thank You !